gh-103895: Improve how invalid Exception.__notes__ are displayed (#103897)

This commit is contained in:
Carey Metcalfe 2023-05-01 01:32:04 -06:00 committed by GitHub
parent 93107aa2a4
commit 487f55d580
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 23 additions and 5 deletions

View file

@ -852,12 +852,16 @@ class TracebackException:
yield _format_final_exc_line(stype, self._str)
else:
yield from self._format_syntax_error(stype)
if isinstance(self.__notes__, collections.abc.Sequence):
if (
isinstance(self.__notes__, collections.abc.Sequence)
and not isinstance(self.__notes__, (str, bytes))
):
for note in self.__notes__:
note = _safe_string(note, 'note')
yield from [l + '\n' for l in note.split('\n')]
elif self.__notes__ is not None:
yield _safe_string(self.__notes__, '__notes__', func=repr)
yield "{}\n".format(_safe_string(self.__notes__, '__notes__', func=repr))
def _format_syntax_error(self, stype):
"""Format SyntaxError exceptions (internal helper)."""