diff options
author | Irit Katriel <1055913+iritkatriel@users.noreply.github.com> | 2023-06-06 09:26:18 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-06-06 09:26:18 (GMT) |
commit | f4d8e10d0d0cc1ba0787d2350a699d9fb227a7cd (patch) | |
tree | a529369faabe03a15219fec7c5793670d448d9a5 /Lib/traceback.py | |
parent | 92022d8416d9e175800b65c4d71d4e4fb47adcb0 (diff) | |
download | cpython-f4d8e10d0d0cc1ba0787d2350a699d9fb227a7cd.zip cpython-f4d8e10d0d0cc1ba0787d2350a699d9fb227a7cd.tar.gz cpython-f4d8e10d0d0cc1ba0787d2350a699d9fb227a7cd.tar.bz2 |
gh-105292: Add option to make traceback.TracebackException.format_exception_only recurse into exception groups (#105294)
Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>
Co-authored-by: Ćukasz Langa <lukasz@langa.pl>
Diffstat (limited to 'Lib/traceback.py')
-rw-r--r-- | Lib/traceback.py | 18 |
1 files changed, 12 insertions, 6 deletions
diff --git a/Lib/traceback.py b/Lib/traceback.py index 21e3204..354754b 100644 --- a/Lib/traceback.py +++ b/Lib/traceback.py @@ -826,7 +826,7 @@ class TracebackException: def __str__(self): return self._str - def format_exception_only(self): + def format_exception_only(self, *, show_group=False, _depth=0): """Format the exception part of the traceback. The return value is a generator of strings, each ending in a newline. @@ -839,8 +839,10 @@ class TracebackException: The message indicating which exception occurred is always the last string in the output. """ + + indent = 3 * _depth * ' ' if self.exc_type is None: - yield _format_final_exc_line(None, self._str) + yield indent + _format_final_exc_line(None, self._str) return stype = self.exc_type.__qualname__ @@ -851,9 +853,9 @@ class TracebackException: stype = smod + '.' + stype if not issubclass(self.exc_type, SyntaxError): - yield _format_final_exc_line(stype, self._str) + yield indent + _format_final_exc_line(stype, self._str) else: - yield from self._format_syntax_error(stype) + yield from [indent + l for l in self._format_syntax_error(stype)] if ( isinstance(self.__notes__, collections.abc.Sequence) @@ -861,9 +863,13 @@ class TracebackException: ): for note in self.__notes__: note = _safe_string(note, 'note') - yield from [l + '\n' for l in note.split('\n')] + yield from [indent + l + '\n' for l in note.split('\n')] elif self.__notes__ is not None: - yield "{}\n".format(_safe_string(self.__notes__, '__notes__', func=repr)) + yield indent + "{}\n".format(_safe_string(self.__notes__, '__notes__', func=repr)) + + if self.exceptions and show_group: + for ex in self.exceptions: + yield from ex.format_exception_only(show_group=show_group, _depth=_depth+1) def _format_syntax_error(self, stype): """Format SyntaxError exceptions (internal helper).""" |