mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
gh-113358: Fix rendering tracebacks with exceptions with a broken __getattr__ (GH-113359)
Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com>
This commit is contained in:
parent
17b73ab99e
commit
04fabe22dd
3 changed files with 20 additions and 1 deletions
|
@ -2209,6 +2209,20 @@ class BaseExceptionReportingTests:
|
||||||
err_msg = "b'please do not show me as numbers'"
|
err_msg = "b'please do not show me as numbers'"
|
||||||
self.assertEqual(self.get_report(e), vanilla + err_msg + '\n')
|
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):
|
def test_exception_with_multiple_notes(self):
|
||||||
for e in [ValueError(42), SyntaxError('bad syntax')]:
|
for e in [ValueError(42), SyntaxError('bad syntax')]:
|
||||||
with self.subTest(e=e):
|
with self.subTest(e=e):
|
||||||
|
|
|
@ -1051,7 +1051,11 @@ class TracebackException:
|
||||||
# Capture now to permit freeing resources: only complication is in the
|
# Capture now to permit freeing resources: only complication is in the
|
||||||
# unofficial API _format_final_exc_line
|
# unofficial API _format_final_exc_line
|
||||||
self._str = _safe_string(exc_value, 'exception')
|
self._str = _safe_string(exc_value, 'exception')
|
||||||
|
try:
|
||||||
self.__notes__ = getattr(exc_value, '__notes__', None)
|
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._is_syntax_error = False
|
||||||
self._have_exc_type = exc_type is not None
|
self._have_exc_type = exc_type is not None
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
Fix rendering tracebacks with exceptions with a broken __getattr__
|
Loading…
Add table
Add a link
Reference in a new issue