Message397365
This construct isn't traced properly:
except ExceptionName as var:
if something:
raise
Here's a reproducer:
-- 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 f(x):
try:
1/0
except ZeroDivisionError as error:
if x:
raise
return 12
print(sys.version)
sys.settrace(trace)
for x in [0, 1]:
try:
print(f(x))
except:
print("oops")
-----------------------------------
When run with 3.10.0b4, it produces this output:
3.10.0b4 (default, Jul 11 2021, 13:51:53) [Clang 12.0.0 (clang-1200.0.32.29)]
call 10: def f(x):
line 11: try:
line 12: 1/0
exce 12: 1/0
line 13: except ZeroDivisionError as error:
line 14: if x:
* line 15: raise
line 16: return 12
retu 16: return 12
12
call 10: def f(x):
line 11: try:
line 12: 1/0
exce 12: 1/0
line 13: except ZeroDivisionError as error:
line 14: if x:
line 15: raise
retu 15: raise
oops
The starred line claims that raise is being run, but it is not run at that point.
The variable on the except clause is important. If you change that line to "except ZeroDivisionError:", then the output is correct:
3.10.0b4 (default, Jul 11 2021, 13:51:53) [Clang 12.0.0 (clang-1200.0.32.29)]
call 10: def f(x):
line 11: try:
line 12: 1/0
exce 12: 1/0
line 13: except ZeroDivisionError:
line 14: if x:
line 16: return 12
retu 16: return 12
12
call 10: def f(x):
line 11: try:
line 12: 1/0
exce 12: 1/0
line 13: except ZeroDivisionError:
line 14: if x:
line 15: raise
retu 15: raise
oops |
|
Date |
User |
Action |
Args |
2021-07-12 22:51:39 | nedbat | set | recipients:
+ nedbat, Mark.Shannon |
2021-07-12 22:51:39 | nedbat | set | messageid: <1626130299.77.0.916237528648.issue44616@roundup.psfhosted.org> |
2021-07-12 22:51:39 | nedbat | link | issue44616 messages |
2021-07-12 22:51:39 | nedbat | create | |
|