Skip to content

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

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
Openstreetmapler opened this issue May 9, 2025 · 3 comments
Labels
3.13 bugs and security fixes 3.14 bugs and security fixes 3.15 new features, bugs and security fixes stdlib Python modules in the Lib dir topic-repl Related to the interactive shell type-bug An unexpected behavior, bug, or error

Comments

@Openstreetmapler
Copy link

Openstreetmapler commented May 9, 2025

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

@Openstreetmapler Openstreetmapler added the type-crash A hard crash of the interpreter, possibly with a core dump label May 9, 2025
@Eclips4 Eclips4 added type-bug An unexpected behavior, bug, or error topic-repl Related to the interactive shell 3.13 bugs and security fixes 3.14 bugs and security fixes 3.15 new features, bugs and security fixes and removed type-crash A hard crash of the interpreter, possibly with a core dump type-bug An unexpected behavior, bug, or error topic-repl Related to the interactive shell labels May 9, 2025
@StanFromIreland
Copy link
Contributor

StanFromIreland commented May 9, 2025

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

@picnixz picnixz 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 May 9, 2025
@picnixz picnixz added the stdlib Python modules in the Lib dir label May 9, 2025
@devdanzin
Copy link
Contributor

I think this is a duplicate of #130999?

@terryjreedy
Copy link
Member

terryjreedy commented May 9, 2025

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
Labels
3.13 bugs and security fixes 3.14 bugs and security fixes 3.15 new features, bugs and security fixes stdlib Python modules in the Lib dir topic-repl Related to the interactive shell type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

6 participants