Skip to content

Commit 20ac11a

Browse files
[3.6] bpo-33026: Fix jumping out of "with" block by setting f_lineno. (GH-6026). (GH-6074) (GH-6075)
(cherry picked from commit 26c9f56) (cherry picked from commit 04aadf2) Co-authored-by: Serhiy Storchaka <[email protected]>
1 parent 3f439d1 commit 20ac11a

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

Lib/test/test_sys_settrace.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -751,6 +751,34 @@ def test_jump_across_with(output):
751751
with tracecontext(output, 4):
752752
output.append(5)
753753

754+
@jump_test(4, 5, [1, 3, 5, 6])
755+
def test_jump_out_of_with_block_within_for_block(output):
756+
output.append(1)
757+
for i in [1]:
758+
with tracecontext(output, 3):
759+
output.append(4)
760+
output.append(5)
761+
output.append(6)
762+
763+
@jump_test(4, 5, [1, 2, 3, 5, -2, 6])
764+
def test_jump_out_of_with_block_within_with_block(output):
765+
output.append(1)
766+
with tracecontext(output, 2):
767+
with tracecontext(output, 3):
768+
output.append(4)
769+
output.append(5)
770+
output.append(6)
771+
772+
@jump_test(5, 6, [2, 4, 6, 7])
773+
def test_jump_out_of_with_block_within_finally_block(output):
774+
try:
775+
output.append(2)
776+
finally:
777+
with tracecontext(output, 4):
778+
output.append(5)
779+
output.append(6)
780+
output.append(7)
781+
754782
@jump_test(8, 11, [1, 3, 5, 11, 12])
755783
def test_jump_out_of_complex_nested_blocks(output):
756784
output.append(1)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed jumping out of "with" block by setting f_lineno.

Objects/frameobject.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,13 @@ frame_setlineno(PyFrameObject *f, PyObject* p_new_lineno)
317317
PyObject *v = (*--f->f_stacktop);
318318
Py_DECREF(v);
319319
}
320+
if (b->b_type == SETUP_FINALLY &&
321+
code[b->b_handler] == WITH_CLEANUP_START)
322+
{
323+
/* Pop the exit function. */
324+
PyObject *v = (*--f->f_stacktop);
325+
Py_DECREF(v);
326+
}
320327
}
321328

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

0 commit comments

Comments
 (0)