Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 04aadf2

Browse files
[3.7] bpo-33026: Fix jumping out of "with" block by setting f_lineno. (pythonGH-6026). (python#6074)
(cherry picked from commit 26c9f56) Co-authored-by: Serhiy Storchaka <[email protected]>
1 parent 39441fc commit 04aadf2

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
@@ -797,6 +797,34 @@ def test_jump_across_with(output):
797797
with tracecontext(output, 4):
798798
output.append(5)
799799

800+
@jump_test(4, 5, [1, 3, 5, 6])
801+
def test_jump_out_of_with_block_within_for_block(output):
802+
output.append(1)
803+
for i in [1]:
804+
with tracecontext(output, 3):
805+
output.append(4)
806+
output.append(5)
807+
output.append(6)
808+
809+
@jump_test(4, 5, [1, 2, 3, 5, -2, 6])
810+
def test_jump_out_of_with_block_within_with_block(output):
811+
output.append(1)
812+
with tracecontext(output, 2):
813+
with tracecontext(output, 3):
814+
output.append(4)
815+
output.append(5)
816+
output.append(6)
817+
818+
@jump_test(5, 6, [2, 4, 6, 7])
819+
def test_jump_out_of_with_block_within_finally_block(output):
820+
try:
821+
output.append(2)
822+
finally:
823+
with tracecontext(output, 4):
824+
output.append(5)
825+
output.append(6)
826+
output.append(7)
827+
800828
@jump_test(8, 11, [1, 3, 5, 11, 12])
801829
def test_jump_out_of_complex_nested_blocks(output):
802830
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
@@ -320,6 +320,13 @@ frame_setlineno(PyFrameObject *f, PyObject* p_new_lineno)
320320
PyObject *v = (*--f->f_stacktop);
321321
Py_DECREF(v);
322322
}
323+
if (b->b_type == SETUP_FINALLY &&
324+
code[b->b_handler] == WITH_CLEANUP_START)
325+
{
326+
/* Pop the exit function. */
327+
PyObject *v = (*--f->f_stacktop);
328+
Py_DECREF(v);
329+
}
323330
}
324331

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

0 commit comments

Comments
 (0)