Skip to content

New dis.py gets tripped up by ENTER_EXECUTOR #112355

Closed

Description

@gvanrossum

Bug report

Bug description:

I have some code that calls dis.dis(test, adaptive=True) where test is simple function containing a loop containing a branch. Somehow dis crashes like this:

Traceback (most recent call last):
  File "/Users/guido/cpython/t.py", line 20, in <module>
    dis.dis(test, adaptive=True)
  File "/Users/guido/cpython/Lib/dis.py", line 113, in dis
    _disassemble_recursive(x, file=file, depth=depth, show_caches=show_caches, adaptive=adaptive, show_offsets=show_offsets)
  File "/Users/guido/cpython/Lib/dis.py", line 709, in _disassemble_recursive
    disassemble(co, file=file, show_caches=show_caches, adaptive=adaptive, show_offsets=show_offsets)
  File "/Users/guido/cpython/Lib/dis.py", line 701, in disassemble
    _disassemble_bytes(_get_code_array(co, adaptive),
  File "/Users/guido/cpython/Lib/dis.py", line 754, in _disassemble_bytes
    for instr in _get_instructions_bytes(code, varname_from_oparg, names,
  File "/Users/guido/cpython/Lib/dis.py", line 668, in _get_instructions_bytes
    yield Instruction._create(op, arg, offset, start_offset, starts_line, line_number,
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/guido/cpython/Lib/dis.py", line 413, in _create
    argval, argrepr = cls._get_argval_argrepr(
                      ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/guido/cpython/Lib/dis.py", line 376, in _get_argval_argrepr
    argrepr = f"to L{labels_map[argval]}"
                     ~~~~~~~~~~^^^^^^^^
KeyError: 108

Here's the script:

Details
def pr(i):
    pass

def is_prime(n):
    # Bogus
    return n == 2 or n == 3 or n == 5 or n == 7 or (n % 2 != 0 and n % 3 != 0 and n % 5 != 0 and n % 7 != 0)

def test():
    for i in range(2, 50):
        if is_prime(i):
            print(i)

import _testinternalcapi

_testinternalcapi.set_optimizer(_testinternalcapi.get_uop_optimizer())
test()
_testinternalcapi.set_optimizer(None)

import dis
dis.dis(test, adaptive=True)

CPython versions tested on:

CPython main branch

Operating systems tested on:

No response

Linked PRs

Activity

  1. added
    type-bug
    on Nov 24, 2023
  2. gvanrossum commented on Nov 24, 2023

    @gvanrossum
    MemberAuthor

    Note, to run the script, pass -Xuops or set PYTHON_UOPS=1. (Never mind, this version forces the optimizer on using _testinternalcapi.)

  3. iritkatriel commented on Nov 24, 2023

    @iritkatriel
    Member

    I think the problem is that ENTER_EXECUTOR is interpreted as a jump (because it contains JUMPBY). So we look up the label of offset+(2*arg+1). Is it really a jump, or should it use SKIP_OVER instead of JUMPBY?

  4. iritkatriel commented on Nov 24, 2023

    @iritkatriel
    Member

    Actually it's probably that dis assumes it's a forward jump, while it should regard it as a backward jump.

  5. added 2 commits that reference this issue on Nov 24, 2023
  6. added a commit that references this issue on Feb 11, 2024
  7. added a commit that references this issue on Sep 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

type-bug

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions