From 1688c770fa28ce30b6bfd6bc3ec1d602985c66f0 Mon Sep 17 00:00:00 2001 From: "Adam D. Thomas" Date: Tue, 4 Feb 2025 19:03:05 +1100 Subject: [PATCH 1/2] Add fix and tests --- Lib/test/test_traceback.py | 26 ++++++++++++++++++++++++++ Lib/traceback.py | 37 ++++++++++++++++++++----------------- 2 files changed, 46 insertions(+), 17 deletions(-) diff --git a/Lib/test/test_traceback.py b/Lib/test/test_traceback.py index 89980ae6f8573a..1926ecc7443aa6 100644 --- a/Lib/test/test_traceback.py +++ b/Lib/test/test_traceback.py @@ -4514,6 +4514,32 @@ def func(): actual = self.get_suggestion(func) self.assertIn("forget to import '_io'", actual) + def test_name_error_with_bad_getattr(self): + class A: + def __getattr__(self, x): + spam + + actual = self.get_suggestion(A(), "eggs") + self.assertIn("name 'spam' is not defined", actual) + + def test_name_error_with_bad_getattribute(self): + + class A: + def __getattribute__(self, x): + spam + + actual = self.get_suggestion(A(), "eggs") + self.assertIn("name 'spam' is not defined", actual) + + def test_name_error_with_bad_property(self): + class A: + @property + def eggs(self): + eggs + + actual = self.get_suggestion(A(), "eggs") + self.assertIn("name 'eggs' is not defined", actual) + class PurePythonSuggestionFormattingTests( diff --git a/Lib/traceback.py b/Lib/traceback.py index 31c73efcef5a52..28cbf1dd88c6e4 100644 --- a/Lib/traceback.py +++ b/Lib/traceback.py @@ -1504,24 +1504,27 @@ def _compute_suggestion_error(exc_value, tb, wrong_name): return None else: assert isinstance(exc_value, NameError) - # find most recent frame - if tb is None: + try: + # find most recent frame + if tb is None: + return None + while tb.tb_next is not None: + tb = tb.tb_next + frame = tb.tb_frame + d = ( + list(frame.f_locals) + + list(frame.f_globals) + + list(frame.f_builtins) + ) + + # Check first if we are in a method and the instance + # 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}" + except Exception: return None - while tb.tb_next is not None: - tb = tb.tb_next - frame = tb.tb_frame - d = ( - list(frame.f_locals) - + list(frame.f_globals) - + list(frame.f_builtins) - ) - - # Check first if we are in a method and the instance - # 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: import _suggestions From a50ace5a2c2b0c49283de1148a2dda3aeee8f747 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Tue, 4 Feb 2025 09:01:07 +0000 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2025-02-04-09-01-06.gh-issue-129605.wKGNXQ.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2025-02-04-09-01-06.gh-issue-129605.wKGNXQ.rst diff --git a/Misc/NEWS.d/next/Library/2025-02-04-09-01-06.gh-issue-129605.wKGNXQ.rst b/Misc/NEWS.d/next/Library/2025-02-04-09-01-06.gh-issue-129605.wKGNXQ.rst new file mode 100644 index 00000000000000..9dcf05913870b9 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-02-04-09-01-06.gh-issue-129605.wKGNXQ.rst @@ -0,0 +1 @@ +Improve exception handling when computing suggestions in traceback.py. This fixes a bug that could cause PyREPL to exit unexpectedly.