Skip to content

[3.6] [3.7] bpo-33026: Fix jumping out of "with" block by setting f_lineno. (GH-6074) #6075

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 1 commit into from
Mar 11, 2018
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
28 changes: 28 additions & 0 deletions Lib/test/test_sys_settrace.py
Original file line number Diff line number Diff line change
Expand Up @@ -751,6 +751,34 @@ def test_jump_across_with(output):
with tracecontext(output, 4):
output.append(5)

@jump_test(4, 5, [1, 3, 5, 6])
def test_jump_out_of_with_block_within_for_block(output):
output.append(1)
for i in [1]:
with tracecontext(output, 3):
output.append(4)
output.append(5)
output.append(6)

@jump_test(4, 5, [1, 2, 3, 5, -2, 6])
def test_jump_out_of_with_block_within_with_block(output):
output.append(1)
with tracecontext(output, 2):
with tracecontext(output, 3):
output.append(4)
output.append(5)
output.append(6)

@jump_test(5, 6, [2, 4, 6, 7])
def test_jump_out_of_with_block_within_finally_block(output):
try:
output.append(2)
finally:
with tracecontext(output, 4):
output.append(5)
output.append(6)
output.append(7)

@jump_test(8, 11, [1, 3, 5, 11, 12])
def test_jump_out_of_complex_nested_blocks(output):
output.append(1)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed jumping out of "with" block by setting f_lineno.
7 changes: 7 additions & 0 deletions Objects/frameobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,13 @@ frame_setlineno(PyFrameObject *f, PyObject* p_new_lineno)
PyObject *v = (*--f->f_stacktop);
Py_DECREF(v);
}
if (b->b_type == SETUP_FINALLY &&
code[b->b_handler] == WITH_CLEANUP_START)
{
/* Pop the exit function. */
PyObject *v = (*--f->f_stacktop);
Py_DECREF(v);
}
}

/* Finally set the new f_lineno and f_lasti and return OK. */
Expand Down