Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

gh-130999: Avoid exiting the new REPL when there are non-string candidates for suggestions #131001

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
Loading
from

Conversation

devdanzin
Copy link
Contributor

@devdanzin devdanzin commented Mar 9, 2025

The new REPL will exit when given code like:

>>> import runpy
... runpy._run_module_code("blech", {0: "", "bluch": ""}, "")
...

(See full traceback in #130999 (comment))

With this PR, the repr of the error causing the exit will be displayed instead:

>>> import runpy
... runpy._run_module_code("blech", {0: "", "bluch": ""}, "")
...
TypeError("all elements in 'candidates' must be strings")
>>>

This is an easy, but perhaps too rough, solution. Suggestions for a better way to display the error (or even catch the exception) are very welcome.

@ZeroIntensity ZeroIntensity added topic-repl Related to the interactive shell needs backport to 3.13 bugs and security fixes labels Mar 9, 2025
@pablogsal
Copy link
Member

I am not convincing this is the right solution. This will hide the real error and as the traceback is missing will make it very difficult to find out what the problem is. The real solution is probably to filter the candidates in the traceback module before submitting it to the _generate_suggestions function.

@ambv @iritkatriel what do you think?

@iritkatriel
Copy link
Member

I agree with @pablogsal.

Either filter in traceback, or error out in runpy when the globals dict is weird.

@devdanzin
Copy link
Contributor Author

Thank you both for the review, sorry for taking so long to address it.

Either filter in traceback, or error out in runpy when the globals dict is weird.

runpy is just an easy path to a NameError with a user provided globals, but it also works with e.g.:

>>> g = {0:1, "p": 1}
>>> exec(compile("pa", 'g', "exec"), g)
[...]
NameError: name 'pa' is not defined
During handling of the above exception, another exception occurred:
[...]
TypeError: all elements in 'candidates' must be strings
[And the REPL exits]

So, the current situation in trackeback._compute_suggestion_error() is:

  • ImportError: if the module has a non-string in its __dict__, no suggestion is offered and no error happens.
  • AttributeError: object returns a non-string in its __dir__ (or has one in __dict__), no suggestion is offered and no error happens.
  • NameError: if f_locals, f_globals or f_builtins has a non-string key, an error is raised that exits the new REPL.

I suggest we just guard against an exception in the NameError case and offer no suggestions (by returning None). An alternative that gets the same result is to return None if a check like any(not isinstance(x, str) for x in candidates) succeeds.

Or we can filter non-strings from the candidates in the NameError case, which would provide suggestions while avoiding the error. If this is preferred, I suggest to also filter non-strings in the ImportError and AttributeError cases, for the same effect.

@tomasr8
Copy link
Member

tomasr8 commented Apr 14, 2025

@devdanzin If you just want to fix the crash, this one-liner does the job:

diff --git a/Lib/traceback.py b/Lib/traceback.py
index 647c23ed782..cd5e2a88f07 100644
--- a/Lib/traceback.py
+++ b/Lib/traceback.py
@@ -1527,6 +1527,7 @@ def _compute_suggestion_error(exc_value, tb, wrong_name):
             + list(frame.f_globals)
             + list(frame.f_builtins)
         )
+        d = [name for name in d if isinstance(name, str)]
 
         # Check first if we are in a method and the instance
         # has the wrong name as attribute

@python-cla-bot
Copy link

python-cla-bot bot commented Apr 18, 2025

All commit authors signed the Contributor License Agreement.

CLA signed

@serhiy-storchaka serhiy-storchaka added the needs backport to 3.14 bugs and security fixes label May 8, 2025
@devdanzin
Copy link
Contributor Author

Gentle ping @iritkatriel @pablogsal . Should we go with guarding against the exception and not offering suggestions on NameError case? What's your preference?

@terryjreedy
Copy link
Member

Non-string keys are legal in dicts. Hints are an excellent by optional (and CPython-specific, I presume) enhancement. The hinters should just ignore, by filtering out, non-string keys and give a hint if possible. There is no reason to not give hint. Hinters are not linters.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting review needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes topic-repl Related to the interactive shell
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.