Skip to content

Commit 5979e81

Browse files
authored
bpo-43933: Set frame.f_lineno during call to __exit__ (GH-25719)
* Set line number of __exit__ call in a with statement to be that of the with keyword.
1 parent 6414138 commit 5979e81

File tree

7 files changed

+2961
-2952
lines changed

7 files changed

+2961
-2952
lines changed

Lib/test/test_dis.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1087,7 +1087,7 @@ def jumpy():
10871087
Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=154, starts_line=None, is_jump_target=False),
10881088
Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=156, starts_line=None, is_jump_target=False),
10891089
Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=158, starts_line=None, is_jump_target=False),
1090-
Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=160, starts_line=None, is_jump_target=False),
1090+
Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=160, starts_line=25, is_jump_target=False),
10911091
Instruction(opname='DUP_TOP', opcode=4, arg=None, argval=None, argrepr='', offset=162, starts_line=None, is_jump_target=False),
10921092
Instruction(opname='DUP_TOP', opcode=4, arg=None, argval=None, argrepr='', offset=164, starts_line=None, is_jump_target=False),
10931093
Instruction(opname='CALL_FUNCTION', opcode=131, arg=3, argval=3, argrepr='', offset=166, starts_line=None, is_jump_target=False),

Lib/test/test_sys_settrace.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -936,10 +936,11 @@ def func():
936936
(-4, 'line'),
937937
(-4, 'return'),
938938
(2, 'line'),
939+
(1, 'line'),
939940
(-3, 'call'),
940941
(-2, 'line'),
941942
(-2, 'return'),
942-
(2, 'return')])
943+
(1, 'return')])
943944

944945
def test_if_false_in_try_except(self):
945946

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Set frame.f_lineno to the line number of the 'with' kweyword when executing
2+
the call to ``__exit__``.

Python/compile.c

+8-4
Original file line numberDiff line numberDiff line change
@@ -1745,6 +1745,7 @@ static int
17451745
compiler_unwind_fblock(struct compiler *c, struct fblockinfo *info,
17461746
int preserve_tos)
17471747
{
1748+
int loc;
17481749
switch (info->fb_type) {
17491750
case WHILE_LOOP:
17501751
case EXCEPTION_HANDLER:
@@ -1797,6 +1798,8 @@ compiler_unwind_fblock(struct compiler *c, struct fblockinfo *info,
17971798

17981799
case WITH:
17991800
case ASYNC_WITH:
1801+
loc = c->u->u_lineno;
1802+
SET_LOC(c, (stmt_ty)info->fb_datum);
18001803
ADDOP(c, POP_BLOCK);
18011804
if (preserve_tos) {
18021805
ADDOP(c, ROT_TWO);
@@ -1810,6 +1813,7 @@ compiler_unwind_fblock(struct compiler *c, struct fblockinfo *info,
18101813
ADDOP(c, YIELD_FROM);
18111814
}
18121815
ADDOP(c, POP_TOP);
1816+
c->u->u_lineno = loc;
18131817
return 1;
18141818

18151819
case HANDLER_CLEANUP:
@@ -4990,7 +4994,7 @@ compiler_async_with(struct compiler *c, stmt_ty s, int pos)
49904994

49914995
/* SETUP_ASYNC_WITH pushes a finally block. */
49924996
compiler_use_next_block(c, block);
4993-
if (!compiler_push_fblock(c, ASYNC_WITH, block, final, NULL)) {
4997+
if (!compiler_push_fblock(c, ASYNC_WITH, block, final, s)) {
49944998
return 0;
49954999
}
49965000

@@ -5016,6 +5020,7 @@ compiler_async_with(struct compiler *c, stmt_ty s, int pos)
50165020
/* For successful outcome:
50175021
* call __exit__(None, None, None)
50185022
*/
5023+
SET_LOC(c, s);
50195024
if(!compiler_call_exit_with_nones(c))
50205025
return 0;
50215026
ADDOP(c, GET_AWAITABLE);
@@ -5028,7 +5033,6 @@ compiler_async_with(struct compiler *c, stmt_ty s, int pos)
50285033

50295034
/* For exceptional outcome: */
50305035
compiler_use_next_block(c, final);
5031-
50325036
ADDOP(c, WITH_EXCEPT_START);
50335037
ADDOP(c, GET_AWAITABLE);
50345038
ADDOP_LOAD_CONST(c, Py_None);
@@ -5082,7 +5086,7 @@ compiler_with(struct compiler *c, stmt_ty s, int pos)
50825086

50835087
/* SETUP_WITH pushes a finally block. */
50845088
compiler_use_next_block(c, block);
5085-
if (!compiler_push_fblock(c, WITH, block, final, NULL)) {
5089+
if (!compiler_push_fblock(c, WITH, block, final, s)) {
50865090
return 0;
50875091
}
50885092

@@ -5112,14 +5116,14 @@ compiler_with(struct compiler *c, stmt_ty s, int pos)
51125116
/* For successful outcome:
51135117
* call __exit__(None, None, None)
51145118
*/
5119+
SET_LOC(c, s);
51155120
if (!compiler_call_exit_with_nones(c))
51165121
return 0;
51175122
ADDOP(c, POP_TOP);
51185123
ADDOP_JUMP(c, JUMP_FORWARD, exit);
51195124

51205125
/* For exceptional outcome: */
51215126
compiler_use_next_block(c, final);
5122-
51235127
ADDOP(c, WITH_EXCEPT_START);
51245128
compiler_with_except_finish(c);
51255129

0 commit comments

Comments
 (0)