Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-105292: Add option to make traceback.TracebackException.format_exception_only recurse into exception groups#105294

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
ambv merged 5 commits intopython:mainfromiritkatriel:show_group
Jun 6, 2023
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletionsDoc/library/traceback.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -319,19 +319,24 @@ capture data for later printing in a lightweight fashion.
The message indicating which exception occurred is always the last
string in the output.

.. method:: format_exception_only()
.. method:: format_exception_only(*, show_group=False)

Format the exception part of the traceback.

The return value is a generator of strings, each ending in a newline.

Normally, the generator emits a single string; however, for
:exc:`SyntaxError` exceptions, it emits several lines that (when
printed) display detailed information about where the syntax
error occurred.
When *show_group* is ``False``, the generator normally emits a single
string; however, for :exc:`SyntaxError` exceptions, it emits several
lines that (when printed) display detailed information about where
the syntax error occurred. The message indicating which exception
occurred is always the last string in the output.

The message indicating which exception occurred is always the last
string in the output.
When *show_group* is ``True``, and the exception is an instance of
:exc:`BaseExceptionGroup`, the nested exceptions are included as
well, recursively, with indentation relative to their nesting depth.

.. versionchanged:: 3.13
Added the *show_group* parameter.

.. versionchanged:: 3.10
Added the *compact* parameter.
Expand All@@ -340,6 +345,7 @@ capture data for later printing in a lightweight fashion.
Added the *max_group_width* and *max_group_depth* parameters.



:class:`StackSummary` Objects
-----------------------------

Expand Down
7 changes: 7 additions & 0 deletionsDoc/whatsnew/3.13.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -113,6 +113,13 @@ pathlib
:meth:`~pathlib.Path.rglob`.
(Contributed by Barney Gale in :gh:`77609`.)

traceback
---------

* Add *show_group* paramter to :func:`traceback.TracebackException.format_exception_only`
to format the nested exceptions of a :exc:`BaseExceptionGroup` instance, recursively.
(Contributed by Irit Katriel in :gh:`105292`.)

Optimizations
=============

Expand Down
14 changes: 14 additions & 0 deletionsLib/test/test_traceback.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2792,6 +2792,20 @@ def test_exception_group_format_exception_only(self):

self.assertEqual(formatted, expected)

def test_exception_group_format_exception_onlyi_recursive(self):
teg = traceback.TracebackException.from_exception(self.eg)
formatted = ''.join(teg.format_exception_only(show_group=True)).split('\n')
expected = [
'ExceptionGroup: eg2 (2 sub-exceptions)',
' ExceptionGroup: eg1 (2 sub-exceptions)',
' ZeroDivisionError: division by zero',
' ValueError: 42',
' ValueError: 24',
''
]

self.assertEqual(formatted, expected)

def test_exception_group_format(self):
teg = traceback.TracebackException.from_exception(self.eg)

Expand Down
18 changes: 12 additions & 6 deletionsLib/traceback.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -826,7 +826,7 @@ def __eq__(self, other):
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.
Expand All@@ -839,8 +839,10 @@ def format_exception_only(self):
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)
yieldindent +_format_final_exc_line(None, self._str)
return

stype = self.exc_type.__qualname__
Expand All@@ -851,19 +853,23 @@ def format_exception_only(self):
stype = smod + '.' + stype

if not issubclass(self.exc_type, SyntaxError):
yield _format_final_exc_line(stype, self._str)
yieldindent +_format_final_exc_line(stype, self._str)
else:
yield from self._format_syntax_error(stype)
yield from[indent + l for l inself._format_syntax_error(stype)]

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')]
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)."""
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
Add option to :func:`traceback.format_exception_only` to recurse into the
nested exception of a :exc:`BaseExceptionGroup`.

[8]ページ先頭

©2009-2025 Movatter.jp