mirror of
https://github.com/python/cpython.git
synced 2025-12-23 09:19:18 +00:00
gh-132385: Fix instance error suggestions trigger potential exceptions in traceback (#132387)
This commit is contained in:
parent
20f8ed595d
commit
641253cfac
3 changed files with 29 additions and 1 deletions
|
|
@ -4555,6 +4555,28 @@ class SuggestionFormattingTestBase:
|
|||
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 AttributeError('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
|
||||
|
|
|
|||
|
|
@ -1636,7 +1636,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 Exception:
|
||||
has_wrong_name = False
|
||||
if has_wrong_name:
|
||||
return f"self.{wrong_name}"
|
||||
|
||||
try:
|
||||
|
|
|
|||
|
|
@ -0,0 +1,2 @@
|
|||
Fix instance error suggestions trigger potential exceptions
|
||||
in :meth:`object.__getattr__` in :mod:`traceback`.
|
||||
Loading…
Add table
Add a link
Reference in a new issue