-
-
Notifications
You must be signed in to change notification settings - Fork 30.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
3.10.0b2 traces with-exit before the break that caused the exit #88464
Comments
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)] Shouldn't we get a trace for line 15 (break), then line 12 (with-exit), then line 15 again, then line 16? |
(I'm not sure whether to create other issues for further details) I'm also seeing a return in a with will trace withexit/return for a plain "return" statement, but return/withexit/return for a return with a value ("return 17"). I would expect that a statement causing an exit from a with block would always be traced before the with-exit. |
For context, this behavior was introduced in https://bugs.python.org/issue43933 |
Why this occurs: with cm:
A
break translates to something like: ex = cm.__exit__; cm.__enter__() # with cm
A
ex(...)
goto loop_end # break So, the break is traced after the exit call. However, this doesn't seem consistent with try-finally statements which trace any break/continue/return before the finally block. |
Ned, can you confirm that this works for you? I am closing the issue, but if something is missing, please, reopen it and we will look into it :) |
Thanks for the quick turnaround, this works! |
this appears to have caused a regression in line numbers for |
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:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: