8000 New dis.py gets tripped up by ENTER_EXECUTOR · Issue #112355 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

New dis.py gets tripped up by ENTER_EXECUTOR #112355

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

Closed
gvanrossum opened this issue Nov 24, 2023 · 3 comments
Closed

New dis.py gets tripped up by ENTER_EXECUTOR #112355

gvanrossum opened this issue Nov 24, 2023 · 3 comments
Assignees
Labels
type-bug An unexpected behavior, bug, or error

Comments

@gvanrossum
Copy link
Member
gvanrossum commented Nov 24, 2023

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

@gvanrossum gvanrossum added the type-bug An unexpected behavior, bug, or error label Nov 24, 2023
@gvanrossum
Copy link
Member Author
gvanrossum commented Nov 24, 2023

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

@iritkatriel
Copy link
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?

@iritkatriel
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants
0