Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
devdanzin wants to merge5 commits intopython:main
base:main
Choose a base branch
Loading
fromdevdanzin:fix_non_string_candidates_exit_repl

Conversation

devdanzin
Copy link
Contributor

@devdanzindevdanzin commentedMar 9, 2025
edited by bedevere-appbot
Loading

The new REPL will exit when given code like:

>>>importrunpy...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:

>>>importrunpy...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.

@ZeroIntensityZeroIntensity added topic-replRelated to the interactive shell needs backport to 3.13bugs and security fixes labelsMar 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 intraceback, or error out inrunpy when the globals dict is weird.

@devdanzin
Copy link
ContributorAuthor

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

Either filter intraceback, or error out inrunpy when the globals dict is weird.

runpy is just an easy path to aNameError 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'isnotdefinedDuringhandlingoftheaboveexception,anotherexceptionoccurred:[...]TypeError:allelementsin'candidates'mustbestrings[AndtheREPLexits]

So, the current situation intrackeback._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: iff_locals,f_globals orf_builtins has a non-string key, an error is raised that exits the new REPL.

I suggest we just guard against an exception in theNameError case and offer no suggestions (by returningNone). An alternative that gets the same result is to returnNone if a check likeany(not isinstance(x, str) for x in candidates) succeeds.

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

@tomasr8
Copy link
Member

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

diff --git a/Lib/traceback.py b/Lib/traceback.pyindex 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-botbot commentedApr 18, 2025
edited
Loading

All commit authors signed the Contributor License Agreement.

CLA signed

@serhiy-storchakaserhiy-storchaka added the needs backport to 3.14bugs and security fixes labelMay 8, 2025
@devdanzin
Copy link
ContributorAuthor

Gentle ping@iritkatriel@pablogsal . Should we go with guarding against the exception and not offering suggestions onNameError 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 freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@pablogsalpablogsalAwaiting requested review from pablogsalpablogsal is a code owner

@lysnikolaoulysnikolaouAwaiting requested review from lysnikolaoulysnikolaou is a code owner

@ambvambvAwaiting requested review from ambvambv is a code owner

Assignees
No one assigned
Labels
awaiting reviewneeds backport to 3.13bugs and security fixesneeds backport to 3.14bugs and security fixestopic-replRelated to the interactive shell
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

7 participants
@devdanzin@pablogsal@iritkatriel@tomasr8@terryjreedy@serhiy-storchaka@ZeroIntensity

[8]ページ先頭

©2009-2025 Movatter.jp