Message405668
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) |
|
Date |
User |
Action |
Args |
2021-11-04 10:08:51 | nedbat | set | recipients:
+ nedbat, Mark.Shannon |
2021-11-04 10:08:51 | nedbat | set | messageid: <1636020531.52.0.942589294345.issue45709@roundup.psfhosted.org> |
2021-11-04 10:08:51 | nedbat | link | issue45709 messages |
2021-11-04 10:08:51 | nedbat | create | |
|