GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-129605: Add more exception handling when computing suggestions in traceback.py by ADThomas-astro · Pull Request #129632 · python/cpython

gh-129605: Add more exception handling when computing suggestions in traceback.py #129632

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.

Sign up for GitHub

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

Merged
ambv merged 2 commits into python:main from ADThomas-astro:gh129605_fix
Oct 20, 2026

Uh oh!

There was an error while loading. Please reload this page.

Merged

gh-129605: Add more exception handling when computing suggestions in traceback.py #129632

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
2 commits
Select commit Hold shift + click to select a range
e93095c
Add fix and tests
ADThomas-astro Feb 4, 2025
97771ba
📜🤖 Added by blurb_it.
blurb-it[bot] Feb 4, 2025
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

There are no files selected for viewing

26 changes: 26 additions & 0 deletions 26 Lib/test/test_traceback.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -5109,6 +5109,32 @@ def func():
actual = self.get_suggestion(func)
self.assertIn("forget to import '_io'", actual)

def test_name_error_with_bad_getattr(self):
class A:
def __getattr__(self, x):
spam

actual = self.get_suggestion(A(), "eggs")
self.assertIn("name 'spam' is not defined", actual)

def test_name_error_with_bad_getattribute(self):

class A:
def __getattribute__(self, x):
spam

actual = self.get_suggestion(A(), "eggs")
self.assertIn("name 'spam' is not defined", actual)

def test_name_error_with_bad_property(self):
class A:
@property
def eggs(self):
eggs

actual = self.get_suggestion(A(), "eggs")
self.assertIn("name 'eggs' is not defined", actual)



class PurePythonSuggestionFormattingTests(
Expand Down
51 changes: 27 additions & 24 deletions 51 Lib/traceback.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -1954,31 +1954,34 @@ def _compute_suggestion_error(exc_value, tb, wrong_name):
return None
else:
assert isinstance(exc_value, NameError)
# find most recent frame
if tb is None:
try:
Comment thread

@danielhollas danielhollas Feb 4, 2025

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a very broad try-except block. If you scoped it only around the hasattr call would the new test cases still pass?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

ZeroIntensity and picnixz reacted with thumbs up emoji
All reactions
  • 👍 2 reactions

@ADThomas-astro ADThomas-astro Feb 5, 2025

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes they would still pass. It's usually good practice to minimise code in a try block, but I think this is an exceptional case. This function attempts to provide "Did you mean foo?" suggestions after there has already been an error in user code. Hence a different exception propagating from this function is very unhelpful (and the cause of the issue). So I think it's worth having wider try blocks here to minimise the risk of unhandled exceptions in the future. The existing code has wide try blocks ignoring all Exception subclasses when doing processing for AttributeError (L1485-1494) and ImportError (L1499-1502). I think we we should treat the processing for NameError the same. (Although - I haven't found a way to cause an exception in L1508-1517, so this is mostly theoretical.) Happy to narrow the scope of the try if folks feel strongly.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
# find most recent frame
if tb is None:
return None
while tb.tb_next is not None:
tb = tb.tb_next
frame = tb.tb_frame
d = (
list(frame.f_locals)
+ list(frame.f_globals)
+ list(frame.f_builtins)
)
d = [x for x in d if isinstance(x, str)]
if not_normalized and wrong_name in d:
return wrong_name

# Check first if we are in a method and the instance
# has the wrong name as attribute
if 'self' in frame.f_locals:
self = frame.f_locals['self']
try:
has_wrong_name = hasattr(self, wrong_name)
except Exception:
has_wrong_name = False
if has_wrong_name:
return f"self.{wrong_name}"
except Exception:
return None
while tb.tb_next is not None:
tb = tb.tb_next
frame = tb.tb_frame
d = (
list(frame.f_locals)
+ list(frame.f_globals)
+ list(frame.f_builtins)
)
d = [x for x in d if isinstance(x, str)]
if not_normalized and wrong_name in d:
return wrong_name

# Check first if we are in a method and the instance
# has the wrong name as attribute
if 'self' in frame.f_locals:
self = frame.f_locals['self']
try:
has_wrong_name = hasattr(self, wrong_name)
except Exception:
has_wrong_name = False
if has_wrong_name:
return f"self.{wrong_name}"

if not_normalized and wrong_name in d:
return wrong_name
Expand Down
1 change: 1 addition & 0 deletions 1 Misc/NEWS.d/next/Library/2025-02-04-09-01-06.gh-issue-129605.wKGNXQ.rst
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improve exception handling when computing suggestions in traceback.py. This fixes a bug that could cause PyREPL to exit unexpectedly.
Loading
Oops, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.