Skip to content

sys.exc_clear() clears exception in other stack frames #76498

Closed

Description

@vitiral
mannequin
BPO 32317
Nosy @vitiral, @iritkatriel

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2020-11-22.20:39:24.793>
created_at = <Date 2017-12-13.23:27:44.312>
labels = ['interpreter-core', 'type-bug']
title = 'sys.exc_clear() clears exception in other stack frames'
updated_at = <Date 2020-11-22.20:39:24.792>
user = 'https://github.com/vitiral'

bugs.python.org fields:

activity = <Date 2020-11-22.20:39:24.792>
actor = 'iritkatriel'
assignee = 'none'
closed = True
closed_date = <Date 2020-11-22.20:39:24.793>
closer = 'iritkatriel'
components = ['Interpreter Core']
creation = <Date 2017-12-13.23:27:44.312>
creator = 'Garrett Berg'
dependencies = []
files = []
hgrepos = []
issue_num = 32317
keywords = []
message_count = 4.0
messages = ['308264', '308265', '308269', '381634']
nosy_count = 2.0
nosy_names = ['Garrett Berg', 'iritkatriel']
pr_nums = []
priority = 'normal'
resolution = 'out of date'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue32317'
versions = ['Python 2.7']

Activity

  1. vitiral commented on Dec 13, 2017

    vitiralmannequin
    MannequinAuthor

    # Summary
    In python (2 or 3) it should always be valid to have a bare *raise* statement in an exception block.

    try:
        do_something()
    except SomeException:
        do_cleanup()
        raise # always reraises SomeException with original stack trace
    

    You can see this in the python exception documentation:
    https://docs.python.org/2.7/tutorial/errors.html#raising-exceptions

    # Reproduction of Failure of invariants
    Example python file where this doesn't work:

        from __future__ import print_function
    
        import sys
    
        def doclear():
            """This should basically be a noop"""
            sys.exc_clear()
    try:
        1/0
    except ZeroDivisionError:
        exc = sys.exc_info()[0]
        print("first exc:", exc)
        assert exc is ZeroDivisionError
    
    try:
        1/0
    except ZeroDivisionError:
        exc = sys.exc_info()[0]
        print("second exc (before doclear):", exc)
        doclear()
        exc = sys.exc_info()[0]
        print("second exc (after  doclear):", exc)
        assert sys.exc_info()[0] is ZeroDivisionError  # fails
    

    Running in python2.7 gives:

        first exc: <type 'exceptions.ZeroDivisionError'>
        second exc (before doclear): <type 'exceptions.ZeroDivisionError'>
        second exc (after  doclear): None
        Traceback (most recent call last):
          File "doclear.py", line 23, in <module>
            assert sys.exc_info()[0] is ZeroDivisionError  # fails
        AssertionError

    # Conclusion
    There seems to be a bug in python 2.7 where it doesn't follow it's own spec.
    sys.exc_clear()
    states:

    This function clears all information relating to the current or last
    exception that occurred in the current thread. After calling this function,
    exc_info() will return three None values until another exception is raised in
    the current thread or *the execution stack returns to a frame where another
    exception is being handled*.

    The above code is clear example where sys.exc_clear() is changing the value of
    sys.exc_info() *in a different stack frame*, which is against what is stated in
    the above docs.

    This bug makes it impossible to reraise with a bare raise statement when doing
    cleanup that could use sys.exc_clear(). In other words, calling into functions
    can *change your local state* with regards to what exception is being handled.

  2. added
    interpreter-core
    type-bug
    on Dec 13, 2017
  3. vitiral commented on Dec 13, 2017

    vitiralmannequin
    MannequinAuthor

    I forgot to post this:

    python --version                                                                                                                                                                                                                                                                                            
    Python 2.7.14
    
  4. vitiral commented on Dec 14, 2017

    vitiralmannequin
    MannequinAuthor

    I found a workaround, and probably the reason this has not been detected before:

        import sys
    
        def doclear2()
            try:
                1/0
            except ZeroDivisionError:
                sys.exc_clear()
    try:
        1/0
    except ZeroDivisionError:
        exc = sys.exc_info()[0]
        print("third exc (before doclear):", exc)
        doclear2()
        exc = sys.exc_info()[0]
        print("third exc (after  doclear):", exc)
        assert sys.exc_info()[0] is ZeroDivisionError
    

    The above passes. It seems that being inside a local exception block allows for the spec to hold, only the "local exception" is cleared.

    I still think this is a pretty major bug, but it is good to know what the workaround/reason it's "been working" is.

  5. iritkatriel commented on Nov 22, 2020

    @iritkatriel
    Member

    sys.exc_clear was removed in Python 3:
    https://docs.python.org/3/whatsnew/3.0.html#index-22

  6. transferred this issue fromon Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-coretype-bug

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions