Skip to content

bpo-42645: Make sure that return/break/continue are only traced once when exiting via a finally block. #23780

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

Merged
merged 3 commits into from
Dec 16, 2020
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
16 changes: 4 additions & 12 deletions Lib/test/test_dis.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,16 +366,12 @@ def _tryfinallyconst(b):
%3d 6 LOAD_FAST 1 (b)
8 CALL_FUNCTION 0
10 POP_TOP

%3d 12 RETURN_VALUE

%3d >> 14 LOAD_FAST 1 (b)
12 RETURN_VALUE
>> 14 LOAD_FAST 1 (b)
16 CALL_FUNCTION 0
18 POP_TOP
20 RERAISE
""" % (_tryfinally.__code__.co_firstlineno + 1,
_tryfinally.__code__.co_firstlineno + 2,
_tryfinally.__code__.co_firstlineno + 4,
_tryfinally.__code__.co_firstlineno + 2,
_tryfinally.__code__.co_firstlineno + 4,
)
Expand All @@ -388,17 +384,13 @@ def _tryfinallyconst(b):
%3d 4 LOAD_FAST 0 (b)
6 CALL_FUNCTION 0
8 POP_TOP

%3d 10 LOAD_CONST 1 (1)
10 LOAD_CONST 1 (1)
12 RETURN_VALUE

%3d >> 14 LOAD_FAST 0 (b)
>> 14 LOAD_FAST 0 (b)
16 CALL_FUNCTION 0
18 POP_TOP
20 RERAISE
""" % (_tryfinallyconst.__code__.co_firstlineno + 1,
_tryfinallyconst.__code__.co_firstlineno + 2,
_tryfinallyconst.__code__.co_firstlineno + 4,
_tryfinallyconst.__code__.co_firstlineno + 2,
_tryfinallyconst.__code__.co_firstlineno + 4,
)
Expand Down
113 changes: 113 additions & 0 deletions Lib/test/test_sys_settrace.py
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,119 @@ def func():
(4, 'line'),
(4, 'return')])

def test_if_break(self):

def func():
seq = [1, 0]
while seq:
n = seq.pop()
if n:
break # line 5
else:
n = 99
return n # line 8

self.run_and_compare(func,
[(0, 'call'),
(1, 'line'),
(2, 'line'),
(3, 'line'),
(4, 'line'),
(2, 'line'),
(3, 'line'),
(4, 'line'),
(5, 'line'),
(8, 'line'),
(8, 'return')])

def test_break_through_finally(self):

def func():
a, c, d, i = 1, 1, 1, 99
try:
for i in range(3):
try:
a = 5
if i > 0:
break # line 7
a = 8
finally:
c = 10
except:
d = 12 # line 12
assert a == 5 and c == 10 and d == 1 # line 13

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

def test_continue_through_finally(self):

def func():
a, b, c, d, i = 1, 1, 1, 1, 99
try:
for i in range(2):
try:
a = 5
if i > 0:
continue # line 7
b = 8
finally:
c = 10
except:
d = 12 # line 12
assert (a, b, c, d) == (5, 8, 10, 1) # line 13

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

def test_return_through_finally(self):

def func():
try:
return 2
finally:
4

self.run_and_compare(func,
[(0, 'call'),
(1, 'line'),
(2, 'line'),
(4, 'line'),
(4, 'return')])


class SkipLineEventsTraceTestCase(TraceTestCase):
"""Repeat the trace tests, but with per-line events skipped"""
Expand Down
17 changes: 13 additions & 4 deletions Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -1695,19 +1695,22 @@ compiler_unwind_fblock(struct compiler *c, struct fblockinfo *info,
return 1;

case FINALLY_TRY:
/* This POP_BLOCK gets the line number of the unwinding statement */
ADDOP(c, POP_BLOCK);
if (preserve_tos) {
if (!compiler_push_fblock(c, POP_VALUE, NULL, NULL, NULL)) {
return 0;
}
}
/* Emit the finally block, restoring the line number when done */
int saved_lineno = c->u->u_lineno;
/* Emit the finally block */
VISIT_SEQ(c, stmt, info->fb_datum);
c->u->u_lineno = saved_lineno;
if (preserve_tos) {
compiler_pop_fblock(c, POP_VALUE, NULL);
}
/* The finally block should appear to execute after the
* statement causing the unwinding, so make the unwinding
* instruction artificial */
c->u->u_lineno = -1;
return 1;

case FINALLY_END:
Expand Down Expand Up @@ -2859,14 +2862,20 @@ compiler_return(struct compiler *c, stmt_ty s)
}
if (preserve_tos) {
VISIT(c, expr, s->v.Return.value);
} else {
/* Emit instruction with line number for expression */
if (s->v.Return.value != NULL) {
SET_LOC(c, s->v.Return.value);
ADDOP(c, NOP);
}
}
if (!compiler_unwind_fblock_stack(c, preserve_tos, NULL))
return 0;
if (s->v.Return.value == NULL) {
ADDOP_LOAD_CONST(c, Py_None);
}
else if (!preserve_tos) {
VISIT(c, expr, s->v.Return.value);
ADDOP_LOAD_CONST(c, s->v.Return.value->v.Constant.value);
}
ADDOP(c, RETURN_VALUE);
NEXT_BLOCK(c);
Expand Down
Loading