https://github.com/python/cpython/commit/16e9e74a8b0b50ee75f56eb1c954718bda1f11ec
commit: 16e9e74a8b0b50ee75f56eb1c954718bda1f11ec
branch: 3.13
author: Miss Islington (bot) <31488909+miss-isling...@users.noreply.github.com>
committer: sobolevn <m...@sobolevn.me>
date: 2025-05-02T13:27:54Z
summary:

[3.13] gh-132385: Fix instance error suggestions trigger potential exceptions 
in `traceback` (GH-132387) (#133297)

gh-132385: Fix instance error suggestions trigger potential exceptions in 
`traceback` (GH-132387)
(cherry picked from commit 641253cfac789e57c2b0c16047bdbf355535f60f)

Co-authored-by: sobolevn <m...@sobolevn.me>

files:
A Misc/NEWS.d/next/Library/2025-04-11-12-41-47.gh-issue-132385.86HoA7.rst
M Lib/test/test_traceback.py
M Lib/traceback.py

diff --git a/Lib/test/test_traceback.py b/Lib/test/test_traceback.py
index e906fcc17c6c5d..eaa1a4fa4fc74f 100644
--- a/Lib/test/test_traceback.py
+++ b/Lib/test/test_traceback.py
@@ -4549,6 +4549,28 @@ def foo(self):
         actual = self.get_suggestion(instance.foo)
         self.assertNotIn("self.blech", actual)
 
+    def test_unbound_local_error_with_side_effect(self):
+        # gh-132385
+        class A:
+            def __getattr__(self, key):
+                if key == 'foo':
+                    raise AttributeError('foo')
+                if key == 'spam':
+                    raise ValueError('spam')
+
+            def bar(self):
+                foo
+            def baz(self):
+                spam
+
+        suggestion = self.get_suggestion(A().bar)
+        self.assertNotIn('self.', suggestion)
+        self.assertIn("'foo'", suggestion)
+
+        suggestion = self.get_suggestion(A().baz)
+        self.assertNotIn('self.', suggestion)
+        self.assertIn("'spam'", suggestion)
+
     def test_unbound_local_error_does_not_match(self):
         def func():
             something = 3
diff --git a/Lib/traceback.py b/Lib/traceback.py
index 12235a8d93ea5c..a3b7de085701c6 100644
--- a/Lib/traceback.py
+++ b/Lib/traceback.py
@@ -1528,7 +1528,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):
+            try:
+                has_wrong_name = hasattr(self, wrong_name)
+            except Exception:
+                has_wrong_name = False
+            if has_wrong_name:
                 return f"self.{wrong_name}"
 
     try:
diff --git 
a/Misc/NEWS.d/next/Library/2025-04-11-12-41-47.gh-issue-132385.86HoA7.rst 
b/Misc/NEWS.d/next/Library/2025-04-11-12-41-47.gh-issue-132385.86HoA7.rst
new file mode 100644
index 00000000000000..9aa2da452d29f6
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2025-04-11-12-41-47.gh-issue-132385.86HoA7.rst
@@ -0,0 +1,2 @@
+Fix instance error suggestions trigger potential exceptions
+in :meth:`object.__getattr__` in :mod:`traceback`.

_______________________________________________
Python-checkins mailing list -- python-checkins@python.org
To unsubscribe send an email to python-checkins-le...@python.org
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: arch...@mail-archive.com

Reply via email to