Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Source locations of comprehension bytecode are incorrect #98461

Closed
iritkatriel opened this issue Oct 19, 2022 · 2 comments · Fixed by #98464
Closed

Source locations of comprehension bytecode are incorrect #98461

iritkatriel opened this issue Oct 19, 2022 · 2 comments · Fixed by #98464
Assignees
Labels
3.12 bugs and security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@iritkatriel
Copy link
Member

def f():
  [(x,
   2*x) 
   for x
   in [1,2,3] if (x > 0 
                  and x < 100
                  and x != 50)]

f = f.__code__.co_consts[1]


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)])

The output is:

[((3, 3, 0, 0), 'RESUME', 0),
 ((3, 8, 2, 31), 'BUILD_LIST', 0),
 ((3, 8, 2, 31), 'LOAD_FAST', '.0'),
 ((3, 8, 2, 31), 'FOR_ITER', 66),
 ((5, 5, 7, 8), 'STORE_FAST', 'x'),
 ((6, 6, 18, 19), 'LOAD_FAST', 'x'),
 ((6, 6, 22, 23), 'LOAD_CONST', 0),
 ((6, 6, 18, 23), 'COMPARE_OP', '>'),
 ((6, 6, 18, 23), 'POP_JUMP_IF_FALSE', 64),
 ((7, 7, 22, 23), 'LOAD_FAST', 'x'),
 ((7, 7, 26, 29), 'LOAD_CONST', 100),
 ((7, 7, 22, 29), 'COMPARE_OP', '<'),
 ((7, 7, 22, 29), 'POP_JUMP_IF_FALSE', 64),
 ((8, 8, 22, 23), 'LOAD_FAST', 'x'),
 ((8, 8, 27, 29), 'LOAD_CONST', 50),
 ((8, 8, 22, 29), 'COMPARE_OP', '!='),
 ((8, 8, 22, 29), 'POP_JUMP_IF_TRUE', 50),
 ((None, None, None, None), 'JUMP_BACKWARD', 6),
 ((3, 3, 4, 5), 'LOAD_FAST', 'x'),
 ((4, 4, 3, 4), 'LOAD_CONST', 2),
 ((4, 4, 5, 6), 'LOAD_FAST', 'x'),
 ((4, 4, 3, 6), 'BINARY_OP', 5),
 ((3, 4, 3, 7), 'BUILD_TUPLE', 2),
 ((8, 8, 22, 29), 'LIST_APPEND', 2),                     <-- incorrect from here to the end
 ((8, 8, 22, 29), 'JUMP_BACKWARD', 6),
 ((8, 8, 22, 29), 'RETURN_VALUE', None)]
@iritkatriel iritkatriel added type-bug An unexpected behavior, bug, or error interpreter-core (Objects, Python, Grammar, and Parser dirs) 3.12 bugs and security fixes labels Oct 19, 2022
@iritkatriel iritkatriel self-assigned this Oct 19, 2022
@iritkatriel
Copy link
Member Author

Correct output should be:

[((3, 3, 0, 0), 'RESUME', 0),
 ((3, 8, 2, 31), 'BUILD_LIST', 0),
 ((3, 8, 2, 31), 'LOAD_FAST', '.0'),
 ((3, 8, 2, 31), 'FOR_ITER', 64),
 ((5, 5, 7, 8), 'STORE_FAST', 'x'),
 ((6, 6, 18, 19), 'LOAD_FAST', 'x'),
 ((6, 6, 22, 23), 'LOAD_CONST', 0),
 ((6, 6, 18, 23), 'COMPARE_OP', '>'),
 ((6, 6, 18, 23), 'POP_JUMP_IF_FALSE', 62),
 ((7, 7, 22, 23), 'LOAD_FAST', 'x'),
 ((7, 7, 26, 29), 'LOAD_CONST', 100),
 ((7, 7, 22, 29), 'COMPARE_OP', '<'),
 ((7, 7, 22, 29), 'POP_JUMP_IF_FALSE', 62),
 ((8, 8, 22, 23), 'LOAD_FAST', 'x'),
 ((8, 8, 27, 29), 'LOAD_CONST', 50),
 ((8, 8, 22, 29), 'COMPARE_OP', '!='),
 ((8, 8, 22, 29), 'POP_JUMP_IF_FALSE', 62),
 ((3, 3, 4, 5), 'LOAD_FAST', 'x'),
 ((4, 4, 3, 4), 'LOAD_CONST', 2),
 ((4, 4, 5, 6), 'LOAD_FAST', 'x'),
 ((4, 4, 3, 6), 'BINARY_OP', 5),
 ((3, 4, 3, 7), 'BUILD_TUPLE', 2),
 ((3, 4, 3, 7), 'LIST_APPEND', 2),
 ((3, 4, 3, 7), 'JUMP_BACKWARD', 6),
 ((3, 8, 2, 31), 'RETURN_VALUE', None)]

@iritkatriel
Copy link
Member Author

FYI @nedbat .

@iritkatriel iritkatriel changed the title Source location of comprehension bytecode are incorrect Source locations of comprehension bytecode are incorrect Oct 20, 2022
carljm added a commit to carljm/cpython that referenced this issue Oct 20, 2022
* main: (40 commits)
  pythongh-98461: Fix source location in comprehensions bytecode (pythonGH-98464)
  pythongh-98421: Clean Up PyObject_Print (pythonGH-98422)
  pythongh-98360: multiprocessing now spawns children on Windows with correct argv[0] in virtual environments (pythonGH-98462)
  CODEOWNERS: Become a typing code owner (python#98480)
  [doc] Improve logging cookbook example. (pythonGH-98481)
  Add more tkinter.Canvas tests (pythonGH-98475)
  pythongh-95023: Added os.setns and os.unshare functions (python#95046)
  pythonGH-98363: Presize the list for batched() (pythonGH-98419)
  pythongh-98374: Suppress ImportError for invalid query for help() command. (pythongh-98450)
  typing tests: `_overload_dummy` raises `NotImplementedError`, not `RuntimeError` (python#98351)
  pythongh-98354: Add unicode check for 'name' attribute in _imp_create_builtin (pythonGH-98412)
  pythongh-98257: Make _PyEval_SetTrace() reentrant (python#98258)
  pythongh-98414: py.exe launcher does not use defaults for -V:company/ option (pythonGH-98460)
  pythongh-98417: Store int_max_str_digits on the Interpreter State (pythonGH-98418)
  Doc: Remove title text from internal links (python#98409)
  [doc] Refresh the venv introduction documentation, and correct the statement about VIRTUAL_ENV (pythonGH-98350)
  Docs: Bump sphinx-lint and fix unbalanced inline literal markup (python#98441)
  pythongh-92886: Replace assertion statements in `handlers.BaseHandler` to support running with optimizations (`-O`) (pythonGH-93231)
  pythongh-92886: Fix tests that fail when running with optimizations (`-O`) in `_test_multiprocessing.py` (pythonGH-93233)
  pythongh-92886: Fix tests that fail when running with optimizations (`-O`) in `test_py_compile.py` (pythonGH-93235)
  ...
iritkatriel added a commit to iritkatriel/cpython that referenced this issue Oct 20, 2022
…ode. compiler_jump_if no longer needs a pointer to the loc
iritkatriel added a commit that referenced this issue Oct 25, 2022
…ompiler_jump_if no longer needs a pointer to the loc. (GH-98494)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.12 bugs and security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant