Skip to content

Commit 842dc93

Browse files
committed
Add another test for NameError for property function, and also wrap exception around hasattr block
1 parent af9d076 commit 842dc93

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

Lib/test/test_traceback.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4507,6 +4507,16 @@ def __getattribute__(self, x):
45074507
self.assertIn("name 'qq' is not defined", actual)
45084508
self.assertEqual(actual.count("NameError"), 1)
45094509

4510+
def test_name_error_with_property_name(self):
4511+
class A:
4512+
@property
4513+
def past(self):
4514+
past
4515+
instance = A()
4516+
actual = self.get_suggestion(instance, "past")
4517+
self.assertIn("name 'past' is not defined", actual)
4518+
self.assertEqual(actual.count("NameError"), 1)
4519+
45104520
def test_unbound_local_error_does_not_match(self):
45114521
def func():
45124522
something = 3

Lib/traceback.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1520,9 +1520,11 @@ def _compute_suggestion_error(exc_value, tb, wrong_name):
15201520
# has the wrong name as attribute
15211521
if 'self' in frame.f_locals:
15221522
self = frame.f_locals['self']
1523-
if frame.f_code.co_name not in ('__getattr__', '__getattribute__'):
1523+
try:
15241524
if hasattr(self, wrong_name):
15251525
return f"self.{wrong_name}"
1526+
except Exception:
1527+
return None
15261528

15271529
try:
15281530
import _suggestions

0 commit comments

Comments
 (0)