8000 gh-132385: Fix instance error suggestions potential exceptions in `traceback` by sobolevn · Pull Request #132387 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-132385: Fix instance error suggestions potential exceptions in traceback #132387

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

Merged
merged 2 commits into from
May 2, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions Lib/test/test_traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -4504,6 +4504,28 @@ def foo(self):
actual = self.get_suggestion(instance.foo)
self.assertNotIn("self.blech", actual)

def test_unbound_local_error_with_side_effect(self):
# gh-132385
class A:
def __getattr__(self, key):
if key == 'foo':
raise SystemExit('foo')
if key == 'spam':
raise ValueError('spam')

def bar(self):
foo
def baz(self):
spam

suggestion = self.get_suggestion(A().bar)
self.assertNotIn('self.', suggestion)
self.assertIn("'foo'", suggestion)

suggestion = self.get_suggestion(A().baz)
self.assertNotIn('self.', suggestion)
self.assertIn("'spam'", suggestion)

def test_unbound_local_error_does_not_match(self):
def func():
something = 3
Expand Down
6 changes: 5 additions & 1 deletion Lib/traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -1532,7 +1532,11 @@ def _compute_suggestion_error(exc_value, tb, wrong_name):
# has the wrong name as attribute
if 'self' in frame.f_locals:
self = frame.f_locals['self']
if hasattr(self, wrong_name):
try:
has_wrong_name = hasattr(self, wrong_name)
except BaseException:
Copy link
Member

Choose a reason for hiding this comment

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

This can swallow things like KeyboardInterrupt and MemoryError. We shouldn't do that.

8000 Copy link
Member Author

Choose a reason for hiding this comment

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

Then Exception only?

Copy link
Member

Choose a reason for hiding this comment

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

Would that catch SystemExit?

Copy link
Member Author

Choose a reason for hiding this comment

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

No :)

>>> SystemExit.__mro__
(<class 'SystemExit'>, <class 'BaseException'>, <class 'object'>)

has_wrong_name = False
if has_wrong_name:
return f"self.{wrong_name}"

try:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix instance error suggestions trigger potential exceptions
in :meth:`object.__getattr__` in :mod:`traceback`.
Loading
0