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.

classification
Title: 3.11 regression: tracing with-statement on exit from block
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.11
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Mark.Shannon Nosy List: Mark.Shannon, nedbat, pablogsal
Priority: normal Keywords: 3.11regression

Created on 2021-11-04 10:08 by nedbat, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 29638 merged Mark.Shannon, 2021-11-19 13:14
Messages (7)
msg405668 - (view) Author: Ned Batchelder (nedbat) * (Python triager) Date: 2021-11-04 10:08
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)
msg405669 - (view) Author: Ned Batchelder (nedbat) * (Python triager) Date: 2021-11-04 10:10
BTW, this is the coverage.py issue: https://github.com/nedbat/coveragepy/issues/1270
msg405673 - (view) Author: Mark Shannon (Mark.Shannon) * (Python committer) Date: 2021-11-04 10:37
Probably an oversight when converting to zero-overhead exceptions.
msg406581 - (view) Author: Mark Shannon (Mark.Shannon) * (Python committer) Date: 2021-11-19 13:14
Sorry about the delay in fixing this.
msg406590 - (view) Author: Mark Shannon (Mark.Shannon) * (Python committer) Date: 2021-11-19 15:16
New changeset 337cb480e9dc1d27594ebd87a0045d00ec8b1c3a by Mark Shannon in branch 'main':
bpo-45709: Fix tracing when exception is handled. (GH-29638)
https://github.com/python/cpython/commit/337cb480e9dc1d27594ebd87a0045d00ec8b1c3a
msg406683 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2021-11-20 18:30
Mark, os something left to do here?
msg406708 - (view) Author: Ned Batchelder (nedbat) * (Python triager) Date: 2021-11-21 10:48
I can confirm that this fixes the problem.
History
Date User Action Args
2022-04-11 14:59:52adminsetgithub: 89872
2021-11-21 15:24:51pablogsalsetstatus: open -> closed
resolution: fixed
stage: needs patch -> resolved
2021-11-21 10:48:04nedbatsetmessages: + msg406708
2021-11-20 18:30:01pablogsalsetnosy: + pablogsal
messages: + msg406683
2021-11-19 15:16:58Mark.Shannonsetmessages: + msg406590
2021-11-19 13:14:41Mark.Shannonsetkeywords: - patch

messages: + msg406581
stage: patch review -> needs patch
2021-11-19 13:14:24Mark.Shannonsetkeywords: + patch
stage: needs patch -> patch review
pull_requests: + pull_request27869
2021-11-05 02:56:11zach.waresetkeywords: + 3.11regression
stage: needs patch
2021-11-04 10:37:53Mark.Shannonsetassignee: Mark.Shannon
type: behavior
messages: + msg405673
2021-11-04 10:10:55nedbatsetmessages: + msg405669
2021-11-04 10:08:51nedbatcreate