https://github.com/python/cpython/commit/04fabe22dd98b4d87f672254b743fbadd5206352
commit: 04fabe22dd98b4d87f672254b743fbadd5206352
branch: main
author: Jérome Perrin <perrinjer...@gmail.com>
committer: vsajip <vinay_sa...@yahoo.co.uk>
date: 2024-01-16T09:49:24Z
summary:

gh-113358: Fix rendering tracebacks with exceptions with a broken __getattr__ 
(GH-113359)

Co-authored-by: Irit Katriel <1055913+iritkatr...@users.noreply.github.com>

files:
A Misc/NEWS.d/next/Library/2023-12-21-14-55-06.gh-issue-113358.nRkiSL.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 a6708119b81191..372fc48bf81a6a 100644
--- a/Lib/test/test_traceback.py
+++ b/Lib/test/test_traceback.py
@@ -2209,6 +2209,20 @@ def __repr__(self):
         err_msg = "b'please do not show me as numbers'"
         self.assertEqual(self.get_report(e), vanilla + err_msg + '\n')
 
+        # an exception with a broken __getattr__ raising a non expected error
+        class BrokenException(Exception):
+            broken = False
+            def __getattr__(self, name):
+                if self.broken:
+                    raise ValueError(f'no {name}')
+
+        e = BrokenException(123)
+        vanilla = self.get_report(e)
+        e.broken = True
+        self.assertEqual(
+            self.get_report(e),
+            vanilla + "Ignored error getting __notes__: ValueError('no 
__notes__')\n")
+
     def test_exception_with_multiple_notes(self):
         for e in [ValueError(42), SyntaxError('bad syntax')]:
             with self.subTest(e=e):
diff --git a/Lib/traceback.py b/Lib/traceback.py
index 30b42a4f693d95..d27c7a726d2bb6 100644
--- a/Lib/traceback.py
+++ b/Lib/traceback.py
@@ -1051,7 +1051,11 @@ def __init__(self, exc_type, exc_value, exc_traceback, 
*, limit=None,
         # Capture now to permit freeing resources: only complication is in the
         # unofficial API _format_final_exc_line
         self._str = _safe_string(exc_value, 'exception')
-        self.__notes__ = getattr(exc_value, '__notes__', None)
+        try:
+            self.__notes__ = getattr(exc_value, '__notes__', None)
+        except Exception as e:
+            self.__notes__ = [
+                f'Ignored error getting __notes__: {_safe_string(e, 
'__notes__', repr)}']
 
         self._is_syntax_error = False
         self._have_exc_type = exc_type is not None
diff --git 
a/Misc/NEWS.d/next/Library/2023-12-21-14-55-06.gh-issue-113358.nRkiSL.rst 
b/Misc/NEWS.d/next/Library/2023-12-21-14-55-06.gh-issue-113358.nRkiSL.rst
new file mode 100644
index 00000000000000..76416553a231a0
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2023-12-21-14-55-06.gh-issue-113358.nRkiSL.rst
@@ -0,0 +1 @@
+Fix rendering tracebacks with exceptions with a broken __getattr__

_______________________________________________
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