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.
This commit is contained in:
Mark Shannon 2021-04-30 14:32:47 +01:00 committed by GitHub
parent 64141382ec
commit 5979e81a21
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 2970 additions and 2961 deletions

View file

@ -1087,7 +1087,7 @@ expected_opinfo_jumpy = [
Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=154, starts_line=None, is_jump_target=False), Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=154, starts_line=None, is_jump_target=False),
Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=156, starts_line=None, is_jump_target=False), Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=156, starts_line=None, is_jump_target=False),
Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=158, starts_line=None, is_jump_target=False), Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=158, starts_line=None, is_jump_target=False),
Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=160, starts_line=None, is_jump_target=False), Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=160, starts_line=25, is_jump_target=False),
Instruction(opname='DUP_TOP', opcode=4, arg=None, argval=None, argrepr='', offset=162, starts_line=None, is_jump_target=False), Instruction(opname='DUP_TOP', opcode=4, arg=None, argval=None, argrepr='', offset=162, starts_line=None, is_jump_target=False),
Instruction(opname='DUP_TOP', opcode=4, arg=None, argval=None, argrepr='', offset=164, starts_line=None, is_jump_target=False), Instruction(opname='DUP_TOP', opcode=4, arg=None, argval=None, argrepr='', offset=164, starts_line=None, is_jump_target=False),
Instruction(opname='CALL_FUNCTION', opcode=131, arg=3, argval=3, argrepr='', offset=166, starts_line=None, is_jump_target=False), Instruction(opname='CALL_FUNCTION', opcode=131, arg=3, argval=3, argrepr='', offset=166, starts_line=None, is_jump_target=False),

View file

@ -936,10 +936,11 @@ class TraceTestCase(unittest.TestCase):
(-4, 'line'), (-4, 'line'),
(-4, 'return'), (-4, 'return'),
(2, 'line'), (2, 'line'),
(1, 'line'),
(-3, 'call'), (-3, 'call'),
(-2, 'line'), (-2, 'line'),
(-2, 'return'), (-2, 'return'),
(2, 'return')]) (1, 'return')])
def test_if_false_in_try_except(self): def test_if_false_in_try_except(self):

View file

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

View file

@ -1745,6 +1745,7 @@ static int
compiler_unwind_fblock(struct compiler *c, struct fblockinfo *info, compiler_unwind_fblock(struct compiler *c, struct fblockinfo *info,
int preserve_tos) int preserve_tos)
{ {
int loc;
switch (info->fb_type) { switch (info->fb_type) {
case WHILE_LOOP: case WHILE_LOOP:
case EXCEPTION_HANDLER: case EXCEPTION_HANDLER:
@ -1797,6 +1798,8 @@ compiler_unwind_fblock(struct compiler *c, struct fblockinfo *info,
case WITH: case WITH:
case ASYNC_WITH: case ASYNC_WITH:
loc = c->u->u_lineno;
SET_LOC(c, (stmt_ty)info->fb_datum);
ADDOP(c, POP_BLOCK); ADDOP(c, POP_BLOCK);
if (preserve_tos) { if (preserve_tos) {
ADDOP(c, ROT_TWO); ADDOP(c, ROT_TWO);
@ -1810,6 +1813,7 @@ compiler_unwind_fblock(struct compiler *c, struct fblockinfo *info,
ADDOP(c, YIELD_FROM); ADDOP(c, YIELD_FROM);
} }
ADDOP(c, POP_TOP); ADDOP(c, POP_TOP);
c->u->u_lineno = loc;
return 1; return 1;
case HANDLER_CLEANUP: case HANDLER_CLEANUP:
@ -4990,7 +4994,7 @@ compiler_async_with(struct compiler *c, stmt_ty s, int pos)
/* SETUP_ASYNC_WITH pushes a finally block. */ /* SETUP_ASYNC_WITH pushes a finally block. */
compiler_use_next_block(c, block); compiler_use_next_block(c, block);
if (!compiler_push_fblock(c, ASYNC_WITH, block, final, NULL)) { if (!compiler_push_fblock(c, ASYNC_WITH, block, final, s)) {
return 0; return 0;
} }
@ -5016,6 +5020,7 @@ compiler_async_with(struct compiler *c, stmt_ty s, int pos)
/* For successful outcome: /* For successful outcome:
* call __exit__(None, None, None) * call __exit__(None, None, None)
*/ */
SET_LOC(c, s);
if(!compiler_call_exit_with_nones(c)) if(!compiler_call_exit_with_nones(c))
return 0; return 0;
ADDOP(c, GET_AWAITABLE); ADDOP(c, GET_AWAITABLE);
@ -5028,7 +5033,6 @@ compiler_async_with(struct compiler *c, stmt_ty s, int pos)
/* For exceptional outcome: */ /* For exceptional outcome: */
compiler_use_next_block(c, final); compiler_use_next_block(c, final);
ADDOP(c, WITH_EXCEPT_START); ADDOP(c, WITH_EXCEPT_START);
ADDOP(c, GET_AWAITABLE); ADDOP(c, GET_AWAITABLE);
ADDOP_LOAD_CONST(c, Py_None); ADDOP_LOAD_CONST(c, Py_None);
@ -5082,7 +5086,7 @@ compiler_with(struct compiler *c, stmt_ty s, int pos)
/* SETUP_WITH pushes a finally block. */ /* SETUP_WITH pushes a finally block. */
compiler_use_next_block(c, block); compiler_use_next_block(c, block);
if (!compiler_push_fblock(c, WITH, block, final, NULL)) { if (!compiler_push_fblock(c, WITH, block, final, s)) {
return 0; return 0;
} }
@ -5112,6 +5116,7 @@ compiler_with(struct compiler *c, stmt_ty s, int pos)
/* For successful outcome: /* For successful outcome:
* call __exit__(None, None, None) * call __exit__(None, None, None)
*/ */
SET_LOC(c, s);
if (!compiler_call_exit_with_nones(c)) if (!compiler_call_exit_with_nones(c))
return 0; return 0;
ADDOP(c, POP_TOP); ADDOP(c, POP_TOP);
@ -5119,7 +5124,6 @@ compiler_with(struct compiler *c, stmt_ty s, int pos)
/* For exceptional outcome: */ /* For exceptional outcome: */
compiler_use_next_block(c, final); compiler_use_next_block(c, final);
ADDOP(c, WITH_EXCEPT_START); ADDOP(c, WITH_EXCEPT_START);
compiler_with_except_finish(c); compiler_with_except_finish(c);

2605
Python/importlib.h generated

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

View file

@ -760,8 +760,8 @@ const unsigned char _Py_M__zipimport[] = {
1,16,1,16,1,16,1,12,1,10,1,18,1,8,1,2, 1,16,1,16,1,16,1,12,1,10,1,18,1,8,1,2,
2,14,1,14,1,18,1,14,1,18,1,2,4,28,1,18, 2,14,1,14,1,18,1,14,1,18,1,2,4,28,1,18,
1,4,255,14,2,18,1,10,2,10,2,2,3,14,1,14, 1,4,255,14,2,18,1,10,2,10,2,2,3,14,1,14,
1,18,1,12,2,12,1,20,1,8,1,8,1,4,202,14, 1,18,1,12,2,12,1,20,1,8,1,8,1,4,202,2,
6,18,128,14,49,4,1,2,247,2,246,2,246,2,227,2, 6,30,196,14,109,4,1,2,247,2,246,2,246,2,227,2,
227,2,248,2,246,2,248,114,27,0,0,0,117,190,1,0, 227,2,248,2,246,2,248,114,27,0,0,0,117,190,1,0,
0,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14, 0,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,
15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30, 15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,
@ -864,7 +864,7 @@ const unsigned char _Py_M__zipimport[] = {
0,0,20,1,8,1,8,1,12,2,2,2,14,1,12,1, 0,0,20,1,8,1,8,1,12,2,2,2,14,1,12,1,
18,1,10,1,12,1,8,1,16,2,18,2,16,2,16,1, 18,1,10,1,12,1,8,1,16,2,18,2,16,2,16,1,
12,1,8,1,2,1,14,1,12,1,18,1,10,1,12,1, 12,1,8,1,2,1,14,1,12,1,18,1,10,1,12,1,
8,1,14,255,16,128,8,3,4,2,2,3,10,1,12,1, 8,1,2,255,28,233,8,26,4,2,2,3,10,1,12,1,
8,1,10,1,2,254,2,243,2,240,114,59,0,0,0,99, 8,1,10,1,2,254,2,243,2,240,114,59,0,0,0,99,
2,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0, 2,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,
3,0,0,0,67,0,0,0,115,16,0,0,0,116,0,124, 3,0,0,0,67,0,0,0,115,16,0,0,0,116,0,124,