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-06-03.10:05:01
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1622714702.42.0.954785031393.issue44298@roundup.psfhosted.org>
In-reply-to
Content
Python 3.10 now traces back to with statements when exiting the with block. When the exit is a break statement, the with exit is visited before the break statement is.  This seems confusing.

--- 8< -----------------------------------------

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

def doit():
    for i in range(2):
        with open("test", "w") as f:
            a = 13
            b = 14
            break
    c = 16

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

--- 8< -----------------------------------------

3.10.0b2 (default, Jun  3 2021, 05:27:13) [Clang 12.0.0 (clang-1200.0.32.29)]
call 10: def doit():
line 11:     for i in range(2):
line 12:         with open("test", "w") as f:
line 13:             a = 13
line 14:             b = 14
line 12:         with open("test", "w") as f:
line 15:             break
line 16:     c = 16
retu 16:     c = 16



Shouldn't we get a trace for line 15 (break), then line 12 (with-exit), then line 15 again, then line 16?
History
Date User Action Args
2021-06-03 10:05:02nedbatsetrecipients: + nedbat, Mark.Shannon
2021-06-03 10:05:02nedbatsetmessageid: <1622714702.42.0.954785031393.issue44298@roundup.psfhosted.org>
2021-06-03 10:05:02nedbatlinkissue44298 messages
2021-06-03 10:05:01nedbatcreate