Skip to content

Commit 794ff7d

Browse files
authored
bpo-44616: Mark all clean up instructions at end of named exception block as artificial (GH-27109) (GH-27135)
(cherry picked from commit e5862f7)
1 parent 7e1d630 commit 794ff7d

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

Lib/test/test_sys_settrace.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1077,6 +1077,29 @@ def func():
10771077
(1, 'line'),
10781078
(1, 'return')])
10791079

1080+
def test_no_tracing_of_named_except_cleanup(self):
1081+
1082+
def func():
1083+
x = 0
1084+
try:
1085+
1/x
1086+
except ZeroDivisionError as error:
1087+
if x:
1088+
raise
1089+
return "done"
1090+
1091+
self.run_and_compare(func,
1092+
[(0, 'call'),
1093+
(1, 'line'),
1094+
(2, 'line'),
1095+
(3, 'line'),
1096+
(3, 'exception'),
1097+
(4, 'line'),
1098+
(5, 'line'),
1099+
(7, 'line'),
1100+
(7, 'return')])
1101+
1102+
10801103
class SkipLineEventsTraceTestCase(TraceTestCase):
10811104
"""Repeat the trace tests, but with per-line events skipped"""
10821105

Python/compile.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3220,10 +3220,10 @@ compiler_try_except(struct compiler *c, stmt_ty s)
32203220
/* second # body */
32213221
VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
32223222
compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body);
3223-
ADDOP(c, POP_BLOCK);
3224-
ADDOP(c, POP_EXCEPT);
32253223
/* name = None; del name; # Mark as artificial */
32263224
c->u->u_lineno = -1;
3225+
ADDOP(c, POP_BLOCK);
3226+
ADDOP(c, POP_EXCEPT);
32273227
ADDOP_LOAD_CONST(c, Py_None);
32283228
compiler_nameop(c, handler->v.ExceptHandler.name, Store);
32293229
compiler_nameop(c, handler->v.ExceptHandler.name, Del);
@@ -3254,7 +3254,6 @@ compiler_try_except(struct compiler *c, stmt_ty s)
32543254
return 0;
32553255
VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
32563256
compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body);
3257-
/* name = None; del name; # Mark as artificial */
32583257
c->u->u_lineno = -1;
32593258
ADDOP(c, POP_EXCEPT);
32603259
ADDOP_JUMP(c, JUMP_FORWARD, end);

0 commit comments

Comments
 (0)