Skip to content
Open
Show file tree
Hide file tree
Changes from 11 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
10 changes: 10 additions & 0 deletions Lib/test/test_traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -4491,6 +4491,16 @@ 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)
instance = A()
actual = self.get_suggestion(instance, "pop")
self.assertIn("name 'qq' 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
5 changes: 3 additions & 2 deletions Lib/traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -1520,8 +1520,9 @@ 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}"
if frame.f_code.co_name != '__getattr__':
Copy link
Member

Choose a reason for hiding this comment

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

This also happens with __getattribute__.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Addressed 05a9858

if hasattr(self, wrong_name):
return f"self.{wrong_name}"

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 ``__getattr__`` method.
Loading