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

Skip to content

Commit 3854f58

Browse files
[2.7] bpo-33026: Fix jumping out of "with" block by setting f_lineno. (pythonGH-6026). (pythonGH-6074) (pythonGH-6076)
(cherry picked from commit 26c9f56) (cherry picked from commit 04aadf2) Co-authored-by: Serhiy Storchaka <[email protected]>
1 parent 34bb88d commit 3854f58

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

Lib/test/test_sys_settrace.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,34 @@ def test_jump_across_with(output):
723723
with tracecontext(output, 4):
724724
output.append(5)
725725

726+
@jump_test(4, 5, [1, 3, 5, 6])
727+
def test_jump_out_of_with_block_within_for_block(output):
728+
output.append(1)
729+
for i in [1]:
730+
with tracecontext(output, 3):
731+
output.append(4)
732+
output.append(5)
733+
output.append(6)
734+
735+
@jump_test(4, 5, [1, 2, 3, 5, -2, 6])
736+
def test_jump_out_of_with_block_within_with_block(output):
737+
output.append(1)
738+
with tracecontext(output, 2):
739+
with tracecontext(output, 3):
740+
output.append(4)
741+
output.append(5)
742+
output.append(6)
743+
744+
@jump_test(5, 6, [2, 4, 6, 7])
745+
def test_jump_out_of_with_block_within_finally_block(output):
746+
try:
747+
output.append(2)
748+
finally:
749+
with tracecontext(output, 4):
750+
output.append(5)
751+
output.append(6)
752+
output.append(7)
753+
726754
@jump_test(8, 11, [1, 3, 5, 11, 12])
727755
def test_jump_out_of_complex_nested_blocks(output):
728756
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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,11 @@ frame_setlineno(PyFrameObject *f, PyObject* p_new_lineno)
340340
PyObject *v = (*--f->f_stacktop);
341341
Py_DECREF(v);
342342
}
343+
if (b->b_type == SETUP_WITH) {
344+
/* Pop the exit function. */
345+
PyObject *v = (*--f->f_stacktop);
346+
Py_DECREF(v);
347+
}
343348
}
344349

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

0 commit comments

Comments
 (0)