Skip to content

Commit 4a54074

Browse files
authored
gh-105658: fix excess trace events for except block ending with a conditional block (#109384)
1 parent 1ce9ea0 commit 4a54074

File tree

4 files changed

+41
-20
lines changed

4 files changed

+41
-20
lines changed

Lib/test/test_dis.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,8 @@ def _tryfinallyconst(b):
642642
CALL 0
643643
POP_TOP
644644
RERAISE 0
645-
>> COPY 3
645+
646+
None >> COPY 3
646647
POP_EXCEPT
647648
RERAISE 1
648649
ExceptionTable:
@@ -674,7 +675,8 @@ def _tryfinallyconst(b):
674675
CALL 0
675676
POP_TOP
676677
RERAISE 0
677-
>> COPY 3
678+
679+
None >> COPY 3
678680
POP_EXCEPT
679681
RERAISE 1
680682
ExceptionTable:
@@ -1822,9 +1824,9 @@ def _prepare_test_cases():
18221824
Instruction(opname='CALL', opcode=53, arg=1, argval=1, argrepr='', offset=414, start_offset=414, starts_line=False, line_number=28, is_jump_target=False, positions=None),
18231825
Instruction(opname='POP_TOP', opcode=32, arg=None, argval=None, argrepr='', offset=422, start_offset=422, starts_line=False, line_number=28, is_jump_target=False, positions=None),
18241826
Instruction(opname='RERAISE', opcode=102, arg=0, argval=0, argrepr='', offset=424, start_offset=424, starts_line=False, line_number=28, is_jump_target=False, positions=None),
1825-
Instruction(opname='COPY', opcode=61, arg=3, argval=3, argrepr='', offset=426, start_offset=426, starts_line=False, line_number=28, is_jump_target=False, positions=None),
1826-
Instruction(opname='POP_EXCEPT', opcode=31, arg=None, argval=None, argrepr='', offset=428, start_offset=428, starts_line=False, line_number=28, is_jump_target=False, positions=None),
1827-
Instruction(opname='RERAISE', opcode=102, arg=1, argval=1, argrepr='', offset=430, start_offset=430, starts_line=False, line_number=28, is_jump_target=False, positions=None),
1827+
Instruction(opname='COPY', opcode=61, arg=3, argval=3, argrepr='', offset=426, start_offset=426, starts_line=True, line_number=None, is_jump_target=False, positions=None),
1828+
Instruction(opname='POP_EXCEPT', opcode=31, arg=None, argval=None, argrepr='', offset=428, start_offset=428, starts_line=False, line_number=None, is_jump_target=False, positions=None),
1829+
Instruction(opname='RERAISE', opcode=102, arg=1, argval=1, argrepr='', offset=430, start_offset=430, starts_line=False, line_number=None, is_jump_target=False, positions=None),
18281830
]
18291831

18301832
# One last piece of inspect fodder to check the default line number handling

Lib/test/test_sys_settrace.py

+30-1
Original file line numberDiff line numberDiff line change
@@ -929,6 +929,35 @@ def func():
929929
(6, 'line'),
930930
(6, 'return')])
931931

932+
def test_finally_with_conditional(self):
933+
934+
# See gh-105658
935+
condition = True
936+
def func():
937+
try:
938+
try:
939+
raise Exception
940+
finally:
941+
if condition:
942+
result = 1
943+
result = 2
944+
except:
945+
result = 3
946+
return result
947+
948+
self.run_and_compare(func,
949+
[(0, 'call'),
950+
(1, 'line'),
951+
(2, 'line'),
952+
(3, 'line'),
953+
(3, 'exception'),
954+
(5, 'line'),
955+
(6, 'line'),
956+
(8, 'line'),
957+
(9, 'line'),
958+
(10, 'line'),
959+
(10, 'return')])
960+
932961
def test_break_to_continue1(self):
933962

934963
def func():
@@ -2123,7 +2152,7 @@ def test_jump_in_nested_finally_3(output):
21232152
output.append(11)
21242153
output.append(12)
21252154

2126-
@jump_test(5, 11, [2, 4], (ValueError, 'exception'))
2155+
@jump_test(5, 11, [2, 4], (ValueError, 'comes after the current code block'))
21272156
def test_no_jump_over_return_try_finally_in_finally_block(output):
21282157
try:
21292158
output.append(2)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix bug where the line trace of an except block ending with a conditional
2+
includes an excess event with the line of the conditional expression.

Python/compile.c

+2-14
Original file line numberDiff line numberDiff line change
@@ -3261,18 +3261,6 @@ compiler_continue(struct compiler *c, location loc)
32613261
}
32623262

32633263

3264-
static location
3265-
location_of_last_executing_statement(asdl_stmt_seq *stmts)
3266-
{
3267-
for (Py_ssize_t i = asdl_seq_LEN(stmts) - 1; i >= 0; i++) {
3268-
location loc = LOC((stmt_ty)asdl_seq_GET(stmts, i));
3269-
if (loc.lineno > 0) {
3270-
return loc;
3271-
}
3272-
}
3273-
return NO_LOCATION;
3274-
}
3275-
32763264
/* Code generated for "try: <body> finally: <finalbody>" is as follows:
32773265
32783266
SETUP_FINALLY L
@@ -3341,9 +3329,9 @@ compiler_try_finally(struct compiler *c, stmt_ty s)
33413329
RETURN_IF_ERROR(
33423330
compiler_push_fblock(c, loc, FINALLY_END, end, NO_LABEL, NULL));
33433331
VISIT_SEQ(c, stmt, s->v.Try.finalbody);
3344-
loc = location_of_last_executing_statement(s->v.Try.finalbody);
33453332
compiler_pop_fblock(c, FINALLY_END, end);
33463333

3334+
loc = NO_LOCATION;
33473335
ADDOP_I(c, loc, RERAISE, 0);
33483336

33493337
USE_LABEL(c, cleanup);
@@ -3392,9 +3380,9 @@ compiler_try_star_finally(struct compiler *c, stmt_ty s)
33923380
compiler_push_fblock(c, loc, FINALLY_END, end, NO_LABEL, NULL));
33933381

33943382
VISIT_SEQ(c, stmt, s->v.TryStar.finalbody);
3395-
loc = location_of_last_executing_statement(s->v.Try.finalbody);
33963383

33973384
compiler_pop_fblock(c, FINALLY_END, end);
3385+
loc = NO_LOCATION;
33983386
ADDOP_I(c, loc, RERAISE, 0);
33993387

34003388
USE_LABEL(c, cleanup);

0 commit comments

Comments
 (0)