Skip to content

Source locations of comprehension bytecode are incorrect #98461

Description

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

Activity

  1. added
    type-bugAn unexpected behavior, bug, or error
    interpreter-core(Objects, Python, Grammar, and Parser dirs)
    3.12only security fixes
    on Oct 19, 2022
  2. self-assigned this
    on Oct 19, 2022
  3. iritkatriel commented on Oct 19, 2022

    @iritkatriel
    MemberAuthor

    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)]
    
    
  4. added a commit that references this issue on Oct 19, 2022
  5. iritkatriel commented on Oct 19, 2022

    @iritkatriel
    MemberAuthor

    FYI @nedbat .

  6. changed the title [-]Source location of comprehension bytecode are incorrect[/-] [+]Source locations of comprehension bytecode are incorrect[/+] on Oct 20, 2022
  7. added a commit that references this issue on Oct 20, 2022
  8. added a commit that references this issue on Oct 20, 2022
  9. added 2 commits that reference this issue on Oct 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

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

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions