[New-bugs-announce] [issue45709] 3.11 regression: tracing with-statement on exit from block

Ned Batchelder report at bugs.python.org
Thu Nov 4 06:08:51 EDT 2021


New submission from Ned Batchelder <ned at nedbatchelder.com>:

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)

----------
components: Interpreter Core
messages: 405668
nosy: Mark.Shannon, nedbat
priority: normal
severity: normal
status: open
title: 3.11 regression: tracing with-statement on exit from block
versions: Python 3.11

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue45709>
_______________________________________


More information about the New-bugs-announce mailing list