Skip to content

Commit a839ff6

Browse files
[3.11] pythongh-113358: Fix rendering tracebacks with exceptions with a broken __getattr__ (pythonGH-113359)
(cherry picked from commit 04fabe2) Co-authored-by: Jérome Perrin <perrinjerome@gmail.com> Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com>
1 parent e5ae15f commit a839ff6

3 files changed

Lines changed: 20 additions & 1 deletion

File tree

‎Lib/test/test_traceback.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1601,6 +1601,20 @@ def __repr__(self):
16011601
err_msg = '<note str() failed>'
16021602
self.assertEqual(self.get_report(e), vanilla + err_msg + '\nFinal Note\n')
16031603

1604+
# an exception with a broken __getattr__ raising a non expected error
1605+
class BrokenException(Exception):
1606+
broken = False
1607+
def __getattr__(self, name):
1608+
if self.broken:
1609+
raise ValueError(f'no {name}')
1610+
1611+
e = BrokenException(123)
1612+
vanilla = self.get_report(e)
1613+
e.broken = True
1614+
self.assertEqual(
1615+
self.get_report(e),
1616+
vanilla + "Ignored error getting __notes__: ValueError('no __notes__')\n")
1617+
16041618
def test_exception_with_multiple_notes(self):
16051619
for e in [ValueError(42), SyntaxError('bad syntax')]:
16061620
with self.subTest(e=e):

‎Lib/traceback.py‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -733,7 +733,11 @@ def __init__(self, exc_type, exc_value, exc_traceback, *, limit=None,
733733
# Capture now to permit freeing resources: only complication is in the
734734
# unofficial API _format_final_exc_line
735735
self._str = _safe_string(exc_value, 'exception')
736-
self.__notes__ = getattr(exc_value, '__notes__', None)
736+
try:
737+
self.__notes__ = getattr(exc_value, '__notes__', None)
738+
except Exception as e:
739+
self.__notes__ = [
740+
f'Ignored error getting __notes__: {_safe_string(e, '__notes__', repr)}']
737741

738742
if exc_type and issubclass(exc_type, SyntaxError):
739743
# Handle SyntaxError's specially
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix rendering tracebacks with exceptions with a broken __getattr__

0 commit comments

Comments
 (0)