Adding a non-string key to globals() dict and then trying to print a non-defined variable crashes the 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
Python 3.13.1 (v3.13.1:06714517797, Dec 3 2024, 14:00:22) [Clang 15.0.0 (clang-1500.3.9.4)]
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
dict.update()function onglobals()to add a non-string key to the global variables dict.NameErrorexception, Python is unable to deal with the invalid key inglobals(), which causes a crash.Expected behavior
dict.update()should behave differently forglobals(), only allowing valid string names as keys. For invalid keys, aTypeErrorshould be raised.Interactive prompt
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)]