Skip to content

traceback._compute_suggestion_error crashes on __getattr__ raising #129605

Open
@jcea

Description

@jcea

Bug report

Bug description:

This is a regression introduced in 3.13 because the new pyrepl. 3.14 is also affected.

Type this in an interactive 3.13/3.14 session:

>>> class a:
...     def __getattr__(self, x):
...         print(x)
...         print(qq)
...         
>>> b=a()
>>> b.pepe
pepe
qq
qq
Traceback (most recent call last):
  File "<python-input-5>", line 1, in <module>
  File "<python-input-3>", line 4, in __getattr__
NameError: name 'qq' is not defined

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "/usr/local/lib/python3.13/_pyrepl/__main__.py", line 6, in <module>
    __pyrepl_interactive_console()
  File "/usr/local/lib/python3.13/_pyrepl/main.py", line 59, in interactive_console
    run_multiline_interactive_console(console)
  File "/usr/local/lib/python3.13/_pyrepl/simple_interact.py", line 160, in run_multiline_interactive_console
    more = console.push(_strip_final_indent(statement), filename=input_name, _symbol="single")  # type: ignore[call-arg]
  File "/usr/local/lib/python3.13/code.py", line 314, in push
    more = self.runsource(source, filename, symbol=_symbol)
  File "/usr/local/lib/python3.13/_pyrepl/console.py", line 211, in runsource
    self.runcode(code)
  File "/usr/local/lib/python3.13/code.py", line 96, in runcode
    self.showtraceback()
  File "/usr/local/lib/python3.13/code.py", line 129, in showtraceback
    self._showtraceback(typ, value, tb.tb_next, '')
  File "/usr/local/lib/python3.13/code.py", line 145, in _showtraceback
    self._excepthook(typ, value, tb)
  File "/usr/local/lib/python3.13/_pyrepl/console.py", line 169, in _excepthook
    lines = traceback.format_exception(
  File "/usr/local/lib/python3.13/traceback.py", line 154, in format_exception
    te = TracebackException(type(value), value, tb, limit=limit, compact=True)
  File "/usr/local/lib/python3.13/traceback.py", line 1090, in __init__
    suggestion = _compute_suggestion_error(exc_value, exc_traceback, wrong_name)
  File "/usr/local/lib/python3.13/traceback.py", line 1516, in _compute_suggestion_error
    if hasattr(self, wrong_name):
  File "<python-input-3>", line 4, in __getattr__
NameError: name 'qq' is not defined

And the interactive python session is terminated.

Under previous Python releases, with no pyrepl, the right exception is raised and the repl keeps going:

Python 3.9.21 (main, Dec  9 2024, 19:43:35) 
[GCC 10.5.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> class a:
...   def __getattr__(self, x):
...     print(x)
...     print(qq)
... 
>>> b=a()
>>> b.pepe
pepe
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 4, in __getattr__
NameError: name 'qq' is not defined
>>> 

Notice that this problem raises SPECIFICALLY if __getattr__ raises a NameError exception. Other exceptions, like division by zero, don't break pyrepl.

CPython versions tested on:

3.13, 3.12, 3.11, 3.10, 3.9, 3.14

Operating systems tested on:

Linux

Linked PRs

Metadata

Metadata

Assignees

Labels

stdlibPython modules in the Lib dirtype-bugAn unexpected behavior, bug, or error

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions