This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author nedbat
Recipients Mark.Shannon, nedbat
Date 2021-11-04.10:08:51
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1636020531.52.0.942589294345.issue45709@roundup.psfhosted.org>
In-reply-to
Content
Python 3.11 seems to have reverted a behavior that was new in 3.10.0b1: exiting a with-statement re-visits the with line on the way out.

--- %<  bug2.py ----------------------
import linecache, sys

def trace(frame, event, arg):
    # The weird globals here is to avoid a NameError on shutdown...
    if frame.f_code.co_filename == globals().get("__file__"):
        lineno = frame.f_lineno
        print("{} {}: {}".format(event[:4], lineno, linecache.getline(__file__, lineno).rstrip()))
    return trace

print(sys.version)
sys.settrace(trace)

import contextlib

def f():
    with contextlib.nullcontext():
        1/0

f()
-------------------------------------

Running with 3.10 shows re-visiting the with:

    $ python3.10 bug2.py
    3.10.0 (default, Oct  4 2021, 17:22:29) [Clang 12.0.0 (clang-1200.0.32.29)]
    call 15: def f():
    line 16:     with contextlib.nullcontext():
    line 17:         1/0
    exce 17:         1/0
    line 16:     with contextlib.nullcontext():
    retu 17:         1/0
    Traceback (most recent call last):
      File "/System/Volumes/Data/root/src/foo/bug1270/bug2.py", line 19, in <module>
        f()
      File "/System/Volumes/Data/root/src/foo/bug1270/bug2.py", line 17, in f
        1/0
    ZeroDivisionError: division by zero

3.11 does not:

    $ python3.11 bug2.py
    3.11.0a1 (default, Oct  6 2021, 07:21:05) [Clang 12.0.0 (clang-1200.0.32.29)]
    call 15: def f():
    line 16:     with contextlib.nullcontext():
    line 17:         1/0
    exce 17:         1/0
    retu 17:         1/0
    Traceback (most recent call last):
      File "/System/Volumes/Data/root/src/foo/bug1270/bug2.py", line 19, in <module>
        f()
        ^^^
      File "/System/Volumes/Data/root/src/foo/bug1270/bug2.py", line 17, in f
        1/0
        ~^~
    ZeroDivisionError: division by zero

Versions before 3.10 also do not visit the with statement:

    $ python3.9 bug2.py
    3.9.7 (default, Sep  7 2021, 22:16:49)
    [Clang 12.0.0 (clang-1200.0.32.29)]
    call 15: def f():
    line 16:     with contextlib.nullcontext():
    line 17:         1/0
    exce 17:         1/0
    line 17:         1/0
    retu 17:         1/0
    Traceback (most recent call last):
      File "/System/Volumes/Data/root/src/foo/bug1270/bug2.py", line 19, in <module>
        f()
      File "/System/Volumes/Data/root/src/foo/bug1270/bug2.py", line 17, in f
        1/0
    ZeroDivisionError: division by zero


Is this a bug in 3.11, or an intentional return to previous behavior?

(BTW: there is no 3.11regression keyword available)
History
Date User Action Args
2021-11-04 10:08:51nedbatsetrecipients: + nedbat, Mark.Shannon
2021-11-04 10:08:51nedbatsetmessageid: <1636020531.52.0.942589294345.issue45709@roundup.psfhosted.org>
2021-11-04 10:08:51nedbatlinkissue45709 messages
2021-11-04 10:08:51nedbatcreate