]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-113358: Fix rendering tracebacks with exceptions with a broken __getattr__ (GH...
authorJérome Perrin <perrinjerome@gmail.com>
Tue, 16 Jan 2024 09:49:24 +0000 (18:49 +0900)
committerGitHub <noreply@github.com>
Tue, 16 Jan 2024 09:49:24 +0000 (09:49 +0000)
Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com>
Lib/test/test_traceback.py
Lib/traceback.py
Misc/NEWS.d/next/Library/2023-12-21-14-55-06.gh-issue-113358.nRkiSL.rst [new file with mode: 0644]

index a6708119b8119114aa0543cc88fed8876f1aeb82..372fc48bf81a6a3b066730d9a13b9bc9a2438e6a 100644 (file)
@@ -2209,6 +2209,20 @@ class BaseExceptionReportingTests:
         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):
index 30b42a4f693d95a6e66d185b6370fad5afe23cb7..d27c7a726d2bb6c0cb8f5f3dabf61f21bcb90579 100644 (file)
@@ -1051,7 +1051,11 @@ class TracebackException:
         # 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 (file)
index 0000000..7641655
--- /dev/null
@@ -0,0 +1 @@
+Fix rendering tracebacks with exceptions with a broken __getattr__