Skip to content

dis._try_compile: traceback printed on error in source argument is too wordy #102114

Closed
@chgnrdv

Description

@chgnrdv

_try_compile function from dis module firstly tries to compile given source string with 'eval' mode and, if exception is occured, catches it and tries again with 'exec' mode. These actions lead to a long chained traceback if given string contains syntax/indentation error, because this string gets refused by compile in both 'eval' and 'exec' modes.
All functions/constructors from dis module that use _try_compile and accept source string as an argument (dis, get_instructions, code_info, show_code, Bytecode) are showing this behavior:

>>> dis.dis(')')
Traceback (most recent call last):
  File "/home/.../cpython/Lib/dis.py", line 67, in _try_compile
    c = compile(source, name, 'eval')
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<dis>", line 1
    )
    ^
SyntaxError: unmatched ')'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/.../cpython/Lib/dis.py", line 112, in dis
    _disassemble_str(x, file=file, depth=depth, show_caches=show_caches, adaptive=adaptive)
  File "/home/.../cpython/Lib/dis.py", line 593, in _disassemble_str
    _disassemble_recursive(_try_compile(source, '<dis>'), **kwargs)
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/.../cpython/Lib/dis.py", line 69, in _try_compile
    c = compile(source, name, 'exec')
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<dis>", line 1
    )
    ^
SyntaxError: unmatched ')'

Python versions affected: 3.10.8, 3.11.0, current main.

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.12only security fixesstdlibPython modules in the Lib dirtype-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions