summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorJérome Perrin <perrinjerome@gmail.com>2024-01-21 17:12:17 (GMT)
committerGitHub <noreply@github.com>2024-01-21 17:12:17 (GMT)
commit00e8c9ce9ef73fb6f64dde7f97a75e17fcaa7541 (patch)
tree8b233f29d6fb5921189af68067339a30a0935cfe /Lib
parentafefa4a74cecc505a54094c6a0f129fcdc02060a (diff)
downloadcpython-00e8c9ce9ef73fb6f64dde7f97a75e17fcaa7541.zip
cpython-00e8c9ce9ef73fb6f64dde7f97a75e17fcaa7541.tar.gz
cpython-00e8c9ce9ef73fb6f64dde7f97a75e17fcaa7541.tar.bz2
[3.12] gh-113358: Fix rendering tracebacks with exceptions with a broken __getattr__ (GH-113359) (#114173)
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/test_traceback.py15
-rw-r--r--Lib/traceback.py6
2 files changed, 20 insertions, 1 deletions
diff --git a/Lib/test/test_traceback.py b/Lib/test/test_traceback.py
index e0ef9e0..61eb0a7 100644
--- a/Lib/test/test_traceback.py
+++ b/Lib/test/test_traceback.py
@@ -1654,6 +1654,21 @@ 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}')
+ raise AttributeError(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 f61d5db..8247d8f 100644
--- a/Lib/traceback.py
+++ b/Lib/traceback.py
@@ -738,7 +738,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)}']
if exc_type and issubclass(exc_type, SyntaxError):
# Handle SyntaxError's specially