Skip to content

New dis.py gets tripped up by ENTER_EXECUTOR #112355

@gvanrossum

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:

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

Metadata

Metadata

Assignees

Labels

type-bugAn unexpected behavior, bug, or error

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions