Skip to content

Commit 337cb48

Browse files
authored
bpo-45709: Fix tracing when exception is handled. (GH-29638)
1 parent 29e5874 commit 337cb48

File tree

3 files changed

+35
-4
lines changed

3 files changed

+35
-4
lines changed

Lib/test/test_sys_settrace.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,6 +1137,38 @@ def func():
11371137
(7, 'line'),
11381138
(7, 'return')])
11391139

1140+
def test_tracing_exception_raised_in_with(self):
1141+
1142+
class NullCtx:
1143+
def __enter__(self):
1144+
return self
1145+
def __exit__(self, *excinfo):
1146+
pass
1147+
1148+
def func():
1149+
try:
1150+
with NullCtx():
1151+
1/0
1152+
except ZeroDivisionError:
1153+
pass
1154+
1155+
self.run_and_compare(func,
1156+
[(0, 'call'),
1157+
(1, 'line'),
1158+
(2, 'line'),
1159+
(-5, 'call'),
1160+
(-4, 'line'),
1161+
(-4, 'return'),
1162+
(3, 'line'),
1163+
(3, 'exception'),
1164+
(2, 'line'),
1165+
(-3, 'call'),
1166+
(-2, 'line'),
1167+
(-2, 'return'),
1168+
(4, 'line'),
1169+
(5, 'line'),
1170+
(5, 'return')])
1171+
11401172

11411173
class SkipLineEventsTraceTestCase(TraceTestCase):
11421174
"""Repeat the trace tests, but with per-line events skipped"""
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Restore behavior from 3.10 when tracing an exception raised within a with
2+
statement.

Python/ceval.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5093,10 +5093,7 @@ MISS_WITH_OPARG_COUNTER(STORE_SUBSCR)
50935093
JUMPTO(handler);
50945094
/* Resume normal execution */
50955095
frame->f_state = FRAME_EXECUTING;
5096-
frame->f_lasti = handler;
5097-
NEXTOPARG();
5098-
PRE_DISPATCH_GOTO();
5099-
DISPATCH_GOTO();
5096+
DISPATCH();
51005097
}
51015098

51025099
exiting:

0 commit comments

Comments
 (0)