Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.12] gh-105658: fix excess trace events for except block ending with a conditional block (#109384) #109411

Merged
merged 1 commit into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion Lib/test/test_sys_settrace.py
Original file line number Diff line number Diff line change
Expand Up @@ -921,6 +921,35 @@ def func():
(6, 'line'),
(6, 'return')])

def test_finally_with_conditional(self):

# See gh-105658
condition = True
def func():
try:
try:
raise Exception
finally:
if condition:
result = 1
result = 2
except:
result = 3
return result

self.run_and_compare(func,
[(0, 'call'),
(1, 'line'),
(2, 'line'),
(3, 'line'),
(3, 'exception'),
(5, 'line'),
(6, 'line'),
(8, 'line'),
(9, 'line'),
(10, 'line'),
(10, 'return')])

def test_break_to_continue1(self):

def func():
Expand Down Expand Up @@ -2092,7 +2121,7 @@ def test_jump_in_nested_finally_3(output):
output.append(11)
output.append(12)

@jump_test(5, 11, [2, 4], (ValueError, 'exception'))
@jump_test(5, 11, [2, 4], (ValueError, 'comes after the current code block'))
def test_no_jump_over_return_try_finally_in_finally_block(output):
try:
output.append(2)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix bug where the line trace of an except block ending with a conditional
includes an excess event with the line of the conditional expression.
16 changes: 2 additions & 14 deletions Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -3184,18 +3184,6 @@ compiler_continue(struct compiler *c, location loc)
}


static location
location_of_last_executing_statement(asdl_stmt_seq *stmts)
{
for (Py_ssize_t i = asdl_seq_LEN(stmts) - 1; i >= 0; i++) {
location loc = LOC((stmt_ty)asdl_seq_GET(stmts, i));
if (loc.lineno > 0) {
return loc;
}
}
return NO_LOCATION;
}

/* Code generated for "try: <body> finally: <finalbody>" is as follows:

SETUP_FINALLY L
Expand Down Expand Up @@ -3264,9 +3252,9 @@ compiler_try_finally(struct compiler *c, stmt_ty s)
RETURN_IF_ERROR(
compiler_push_fblock(c, loc, FINALLY_END, end, NO_LABEL, NULL));
VISIT_SEQ(c, stmt, s->v.Try.finalbody);
loc = location_of_last_executing_statement(s->v.Try.finalbody);
compiler_pop_fblock(c, FINALLY_END, end);

loc = NO_LOCATION;
ADDOP_I(c, loc, RERAISE, 0);

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

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

compiler_pop_fblock(c, FINALLY_END, end);
loc = NO_LOCATION;
ADDOP_I(c, loc, RERAISE, 0);

USE_LABEL(c, cleanup);
Expand Down
Loading