bpo-45607: Make it possible to enrich exception displays via setting their __note__ field (GH-29880)

This commit is contained in:
Irit Katriel 2021-12-03 22:01:15 +00:00 committed by GitHub
parent d9301703fb
commit 5bb7ef2768
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 183 additions and 5 deletions

View file

@ -685,6 +685,8 @@ class TracebackException:
# Capture now to permit freeing resources: only complication is in the
# unofficial API _format_final_exc_line
self._str = _some_str(exc_value)
self.__note__ = exc_value.__note__ if exc_value else None
if exc_type and issubclass(exc_type, SyntaxError):
# Handle SyntaxError's specially
self.filename = exc_value.filename
@ -816,6 +818,8 @@ class TracebackException:
yield _format_final_exc_line(stype, self._str)
else:
yield from self._format_syntax_error(stype)
if self.__note__ is not None:
yield from [l + '\n' for l in self.__note__.split('\n')]
def _format_syntax_error(self, stype):
"""Format SyntaxError exceptions (internal helper)."""