Skip to content

Commit a14a97f

Browse files
ambvADThomas-astro
andauthored
[3.14] gh-129605: Add more exception handling when computing suggestions in traceback.py (GH-129632) (#159994)
(cherry picked from commit 97b5f24) Co-authored-by: Muse Code (Meta Muse Spark) Co-authored-by: Adam D. Thomas <adamt@diversityarrays.com>
1 parent 171f7e6 commit a14a97f

3 files changed

Lines changed: 52 additions & 22 deletions

File tree

‎Lib/test/test_traceback.py‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4722,6 +4722,32 @@ def func():
47224722
actual = self.get_suggestion(func)
47234723
self.assertIn("forget to import '_io'", actual)
47244724

4725+
def test_name_error_with_bad_getattr(self):
4726+
class A:
4727+
def __getattr__(self, x):
4728+
spam
4729+
4730+
actual = self.get_suggestion(A(), "eggs")
4731+
self.assertIn("name 'spam' is not defined", actual)
4732+
4733+
def test_name_error_with_bad_getattribute(self):
4734+
4735+
class A:
4736+
def __getattribute__(self, x):
4737+
spam
4738+
4739+
actual = self.get_suggestion(A(), "eggs")
4740+
self.assertIn("name 'spam' is not defined", actual)
4741+
4742+
def test_name_error_with_bad_property(self):
4743+
class A:
4744+
@property
4745+
def eggs(self):
4746+
eggs
4747+
4748+
actual = self.get_suggestion(A(), "eggs")
4749+
self.assertIn("name 'eggs' is not defined", actual)
4750+
47254751

47264752

47274753
class PurePythonSuggestionFormattingTests(

‎Lib/traceback.py‎

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1626,29 +1626,32 @@ def _compute_suggestion_error(exc_value, tb, wrong_name):
16261626
return None
16271627
else:
16281628
assert isinstance(exc_value, NameError)
1629-
# find most recent frame
1630-
if tb is None:
1629+
try:
1630+
# find most recent frame
1631+
if tb is None:
1632+
return None
1633+
while tb.tb_next is not None:
1634+
tb = tb.tb_next
1635+
frame = tb.tb_frame
1636+
d = (
1637+
list(frame.f_locals)
1638+
+ list(frame.f_globals)
1639+
+ list(frame.f_builtins)
1640+
)
1641+
d = [x for x in d if isinstance(x, str)]
1642+
1643+
# Check first if we are in a method and the instance
1644+
# has the wrong name as attribute
1645+
if 'self' in frame.f_locals:
1646+
self = frame.f_locals['self']
1647+
try:
1648+
has_wrong_name = hasattr(self, wrong_name)
1649+
except Exception:
1650+
has_wrong_name = False
1651+
if has_wrong_name:
1652+
return f"self.{wrong_name}"
1653+
except Exception:
16311654
return None
1632-
while tb.tb_next is not None:
1633-
tb = tb.tb_next
1634-
frame = tb.tb_frame
1635-
d = (
1636-
list(frame.f_locals)
1637-
+ list(frame.f_globals)
1638-
+ list(frame.f_builtins)
1639-
)
1640-
d = [x for x in d if isinstance(x, str)]
1641-
1642-
# Check first if we are in a method and the instance
1643-
# has the wrong name as attribute
1644-
if 'self' in frame.f_locals:
1645-
self = frame.f_locals['self']
1646-
try:
1647-
has_wrong_name = hasattr(self, wrong_name)
1648-
except Exception:
1649-
has_wrong_name = False
1650-
if has_wrong_name:
1651-
return f"self.{wrong_name}"
16521655

16531656
try:
16541657
import _suggestions
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Improve exception handling when computing suggestions in traceback.py. This fixes a bug that could cause PyREPL to exit unexpectedly.

0 commit comments

Comments
 (0)