Skip to content

Commit 3eae45f

Browse files
authored
[3.12] gh-105658: fix excess trace events for except block ending with a conditional block (#109384) (#109411)
gh-105658: fix excess trace events for except block ending with a conditional block (#109384) (cherry picked from commit 4a54074)
1 parent 5c7e8c3 commit 3eae45f

File tree

3 files changed

+34
-15
lines changed

3 files changed

+34
-15
lines changed

Lib/test/test_sys_settrace.py

+30-1
Original file line numberDiff line numberDiff line change
@@ -921,6 +921,35 @@ def func():
921921
(6, 'line'),
922922
(6, 'return')])
923923

924+
def test_finally_with_conditional(self):
925+
926+
# See gh-105658
927+
condition = True
928+
def func():
929+
try:
930+
try:
931+
raise Exception
932+
finally:
933+
if condition:
934+
result = 1
935+
result = 2
936+
except:
937+
result = 3
938+
return result
939+
940+
self.run_and_compare(func,
941+
[(0, 'call'),
942+
(1, 'line'),
943+
(2, 'line'),
944+
(3, 'line'),
945+
(3, 'exception'),
946+
(5, 'line'),
947+
(6, 'line'),
948+
(8, 'line'),
949+
(9, 'line'),
950+
(10, 'line'),
951+
(10, 'return')])
952+
924953
def test_break_to_continue1(self):
925954

926955
def func():
@@ -2092,7 +2121,7 @@ def test_jump_in_nested_finally_3(output):
20922121
output.append(11)
20932122
output.append(12)
20942123

2095-
@jump_test(5, 11, [2, 4], (ValueError, 'exception'))
2124+
@jump_test(5, 11, [2, 4], (ValueError, 'comes after the current code block'))
20962125
def test_no_jump_over_return_try_finally_in_finally_block(output):
20972126
try:
20982127
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
@@ -3184,18 +3184,6 @@ compiler_continue(struct compiler *c, location loc)
31843184
}
31853185

31863186

3187-
static location
3188-
location_of_last_executing_statement(asdl_stmt_seq *stmts)
3189-
{
3190-
for (Py_ssize_t i = asdl_seq_LEN(stmts) - 1; i >= 0; i++) {
3191-
location loc = LOC((stmt_ty)asdl_seq_GET(stmts, i));
3192-
if (loc.lineno > 0) {
3193-
return loc;
3194-
}
3195-
}
3196-
return NO_LOCATION;
3197-
}
3198-
31993187
/* Code generated for "try: <body> finally: <finalbody>" is as follows:
32003188
32013189
SETUP_FINALLY L
@@ -3264,9 +3252,9 @@ compiler_try_finally(struct compiler *c, stmt_ty s)
32643252
RETURN_IF_ERROR(
32653253
compiler_push_fblock(c, loc, FINALLY_END, end, NO_LABEL, NULL));
32663254
VISIT_SEQ(c, stmt, s->v.Try.finalbody);
3267-
loc = location_of_last_executing_statement(s->v.Try.finalbody);
32683255
compiler_pop_fblock(c, FINALLY_END, end);
32693256

3257+
loc = NO_LOCATION;
32703258
ADDOP_I(c, loc, RERAISE, 0);
32713259

32723260
USE_LABEL(c, cleanup);
@@ -3315,9 +3303,9 @@ compiler_try_star_finally(struct compiler *c, stmt_ty s)
33153303
compiler_push_fblock(c, loc, FINALLY_END, end, NO_LABEL, NULL));
33163304

33173305
VISIT_SEQ(c, stmt, s->v.TryStar.finalbody);
3318-
loc = location_of_last_executing_statement(s->v.Try.finalbody);
33193306

33203307
compiler_pop_fblock(c, FINALLY_END, end);
3308+
loc = NO_LOCATION;
33213309
ADDOP_I(c, loc, RERAISE, 0);
33223310

33233311
USE_LABEL(c, cleanup);

0 commit comments

Comments
 (0)