Skip to content

Source location of return instruction in a with block is incorrect #98442

@iritkatriel

Description

def f():
 with x:
   return 42

import dis
from pprint import pprint as pp
def pos(p):
    return (p.lineno, p.end_lineno, p.col_offset, p.end_col_offset)

pp([(pos(x.positions), x.opname, x.argval) for x in dis.get_instructions(f)])

Output:

[((1, 1, 0, 0), 'RESUME', 0),
 ((2, 2, 6, 7), 'LOAD_GLOBAL', 'x'),
 ((2, 3, 1, 12), 'BEFORE_WITH', None),
 ((2, 3, 1, 12), 'POP_TOP', None),
 ((3, 3, 10, 12), 'NOP', None),
 ((2, 3, 1, 12), 'LOAD_CONST', None),
 ((2, 3, 1, 12), 'LOAD_CONST', None),
 ((2, 3, 1, 12), 'LOAD_CONST', None),
 ((2, 3, 1, 12), 'CALL', 2),
 ((2, 3, 1, 12), 'POP_TOP', None),
 ((2, 3, 1, 12), 'LOAD_CONST', 42),                 <-- incorrect
 ((2, 3, 1, 12), 'RETURN_VALUE', None),             <-- incorrect
 ((2, 3, 1, 12), 'PUSH_EXC_INFO', None),
 ((2, 3, 1, 12), 'WITH_EXCEPT_START', None),
 ((2, 3, 1, 12), 'POP_JUMP_IF_TRUE', 50),
 ((2, 3, 1, 12), 'RERAISE', 2),
 ((2, 3, 1, 12), 'POP_TOP', None),
 ((2, 3, 1, 12), 'POP_EXCEPT', None),
 ((2, 3, 1, 12), 'POP_TOP', None),
 ((2, 3, 1, 12), 'POP_TOP', None),
 ((2, 3, 1, 12), 'LOAD_CONST', None),
 ((2, 3, 1, 12), 'RETURN_VALUE', None),
 ((None, None, None, None), 'COPY', 3),
 ((None, None, None, None), 'POP_EXCEPT', None),
 ((None, None, None, None), 'RERAISE', 1)]

Linked PRs

Metadata

Assignees

Labels

3.12bugs and security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or error

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions