Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
26 changes: 26 additions & 0 deletions Lib/test/test_traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -4491,6 +4491,32 @@ def foo(self):
actual = self.get_suggestion(instance.foo)
self.assertNotIn("self.blech", actual)

def test_name_error_with_getattr(self):
class A:
def __getattr__(self, x):
print(x)
print(qq)
class B:
def __getattribute__(self, x):
print(x)
print(qq)

for instance in (A(), B()):
with self.subTest(instance=instance):
actual = self.get_suggestion(instance, "pop")
self.assertIn("name 'qq' is not defined", actual)
self.assertEqual(actual.count("NameError"), 1)

def test_name_error_with_property_name(self):
class A:
@property
def past(self):
past
instance = A()
actual = self.get_suggestion(instance, "past")
self.assertIn("name 'past' is not defined", actual)
self.assertEqual(actual.count("NameError"), 1)

def test_unbound_local_error_does_not_match(self):
def func():
something = 3
Expand Down
7 changes: 5 additions & 2 deletions Lib/traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -1520,8 +1520,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):
return f"self.{wrong_name}"
try:
if hasattr(self, wrong_name):
return f"self.{wrong_name}"
except NameError:
return None

try:
import _suggestions
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix pyrepl crash when a :exc:`NameError` occurs in the :meth:`~object.__getattr__`, :meth:`~object.__getattribute__` and any method of a class.
Loading