Skip to content

Add limit_group option to traceback.format_exception_only #111448

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
sobolevn opened this issue Oct 29, 2023 · 6 comments
Open

Add limit_group option to traceback.format_exception_only #111448

sobolevn opened this issue Oct 29, 2023 · 6 comments
Assignees
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@sobolevn
Copy link
Member

sobolevn commented Oct 29, 2023

Feature or enhancement

Right now there's no limit in both width and depth that format_exception_only prints exception groups.

I propose adding limit_group=True parameter that will respect max_group_depth and max_group_width values and add ... (max_group_depth is {self.max_group_depth})\n and and {remaining} more exception{plural}\n, just like .format() does.

There are two reasons why I propose adding this option.

The first reason is that it will be consistent with .format(). One of the use-cases for format_exception_only is to compare parsed traceback produced by .format() with the one returned from format_exception_only. Right now (in my patch) it fails to compare these two corner-cases: too deep group and too wide group.

The second reason is that this code simply fails:

import sys, traceback
>>> exc = TypeError('bad type')
>>> for i in range(sys.getrecursionlimit() + 1):
...   exc = ExceptionGroup(f'eg{i}', [ValueError(i), exc, ValueError(-i)])
... 
>>> traceback.format_exception_only(exc, show_group=True)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
    traceback.format_exception_only(exc, show_group=True)
  File "/Users/sobolev/Desktop/cpython2/Lib/traceback.py", line 169, in format_exception_only
    return list(te.format_exception_only(show_group=show_group))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/sobolev/Desktop/cpython2/Lib/traceback.py", line 942, in format_exception_only
    yield from ex.format_exception_only(show_group=show_group, _depth=_depth+1)
  File "/Users/sobolev/Desktop/cpython2/Lib/traceback.py", line 942, in format_exception_only
    yield from ex.format_exception_only(show_group=show_group, _depth=_depth+1)
  File "/Users/sobolev/Desktop/cpython2/Lib/traceback.py", line 942, in format_exception_only
    yield from ex.format_exception_only(show_group=show_group, _depth=_depth+1)
  [Previous line repeated 993 more times]
  File "/Users/sobolev/Desktop/cpython2/Lib/traceback.py", line 918, in format_exception_only
    formatted = _format_final_exc_line(
                ^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/sobolev/Desktop/cpython2/Lib/traceback.py", line 175, in _format_final_exc_line
    valuestr = _safe_string(value, 'exception')
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
RecursionError: maximum recursion depth exceeded

This is not very realistic, but it is totally possible.
This option will prevent this case from happening.

Questions:

  • I am open to param name options, I choose limit_group, because it is in sync with show_group
  • Default value: I use True, because it is safer (RecursionError case won't happen)

I have a PR ready :)
This is a part of #104150

Linked PRs

@sobolevn sobolevn added type-feature A feature request or enhancement stdlib Python modules in the Lib dir labels Oct 29, 2023
@sobolevn sobolevn self-assigned this Oct 29, 2023
sobolevn added a commit to sobolevn/cpython that referenced this issue Nov 10, 2023
@iritkatriel
Copy link
Member

TracebackException constructor has max_group_width and max_group_depth.

@sobolevn
Copy link
Member Author

Yes :)
But, format_exception_only did not respect these values until #111952

@iritkatriel
Copy link
Member

Well, if format_exception_only should respect them then it should give them the same names.

But it's not the intention that the convenience wrappers will support everything that TracebackException supports. The wrappers should do something reasonable, and users who need to fine control over the traceback should use TracebackException directly.

@sobolevn
Copy link
Member Author

Sorry, I am a bit confused about which names you are talking about. Maybe we should move this discussion to the PR? So we can have the same context.

@iritkatriel
Copy link
Member

My point is that we don't need to complicate the API with another parameter (limit_group). Just make format_exception_only respect the existing limits on groups, and if anyone wants to turn that off they can do it by controlling those args.

@sobolevn
Copy link
Member Author

Oh, I see 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

2 participants