Skip to content

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

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)]

Activity

  1. added
    type-bugAn unexpected behavior, bug, or error
    topic-replRelated to the interactive shell
    3.13only security fixes
    3.14bugs and security fixes
    3.15bugs and security fixes
    and removed
    type-crashA hard crash of the interpreter, possibly with a core dump
    type-bugAn unexpected behavior, bug, or error
    topic-replRelated to the interactive shell
    on May 9, 2025
  2. StanFromIreland commented on May 9, 2025

    @StanFromIreland
    Member

    I think we can just raise the error when someone attempts to insert an invalid value, I doubt there are backwards compatibility concerns. Unless we want to catch the errors (safer for backwards compatibility)?

  3. changed the title [-]Adding a non-string key to globals() dict makes it possible to crash the interactive prompt[/-] [+]REPL does not handle `TypeError` raised from `_suggestions._generate_suggestions` [/+] on May 9, 2025
  4. added
    stdlibStandard Library Python modules in the Lib/ directory
    on May 9, 2025
  5. devdanzin commented on May 9, 2025

    @devdanzin
    Member

    I think this is a duplicate of #130999?

  6. terryjreedy commented on May 9, 2025

    @terryjreedy
    Member

    I think the hinter should just be fixed. Anyway, closing as dup of #130999, (edit: where I will comment more).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.13only security fixes3.14bugs and security fixes3.15bugs 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