Skip to content

REPL does not handle TypeError raised from _suggestions._generate_suggestions  #133748

@Openstreetmapler

Description

@Openstreetmapler

Crash report

What happened?

Adding a non-string key to globals() dict and then trying to print a non-defined variable crashes the interactive prompt.

How to reproduce

  1. Start an interactive prompt for Python 3.13.1.
  2. Use the dict.update() function on globals() to add a non-string key to the global variables dict.
  3. Try printing a not-defined variable,
  4. While raising the NameError exception, Python is unable to deal with the invalid key in globals(), which causes a crash.

Expected behavior
dict.update() should behave differently for globals(), only allowing valid string names as keys. For invalid keys, a TypeError should be raised.

Interactive prompt

Python 3.13.1 (v3.13.1:06714517797, Dec  3 2024, 14:00:22) [Clang 15.0.0 (clang-1500.3.9.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> globals().update({123: 'test'})
>>> not_defined
Traceback (most recent call last):
  File "<python-input-1>", line 1, in <module>
NameError: name 'not_defined' 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 "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/_pyrepl/__main__.py", line 6, in <module>
    __pyrepl_interactive_console()
  File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/_pyrepl/main.py", line 59, in interactive_console
    run_multiline_interactive_console(console)
  File "/Library/Frameworks/Python.framework/Versions/3.13/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 "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/code.py", line 314, in push
    more = self.runsource(source, filename, symbol=_symbol)
  File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/_pyrepl/console.py", line 211, in runsource
    self.runcode(code)
  File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/code.py", line 96, in runcode
    self.showtraceback()
  File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/code.py", line 129, in showtraceback
    self._showtraceback(typ, value, tb.tb_next, '')
  File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/code.py", line 145, in _showtraceback
    self._excepthook(typ, value, tb)
  File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/_pyrepl/console.py", line 169, in _excepthook
    lines = traceback.format_exception(
  File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/traceback.py", line 154, in format_exception
    te = TracebackException(type(value), value, tb, limit=limit, compact=True)
  File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/traceback.py", line 1090, in __init__
    suggestion = _compute_suggestion_error(exc_value, exc_traceback, wrong_name)
  File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/traceback.py", line 1524, in _compute_suggestion_error
    return _suggestions._generate_suggestions(d, wrong_name)
TypeError: all elements in 'candidates' must be strings

CPython versions tested on:

3.13

Operating systems tested on:

macOS

Output from running 'python -VV' on the command line:

Python 3.13.1 (v3.13.1:06714517797, Dec 3 2024, 14:00:22) [Clang 15.0.0 (clang-1500.3.9.4)]

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.13bugs and security fixes3.14bugs and security fixes3.15new features, bugs and security fixesstdlibStandard Library Python modules in the Lib/ directorytopic-replRelated to the interactive shelltype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions