Skip to content

Capture Additional Exception Attributes in traceback.TracebackException #111927

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

Closed
22 tasks
ericsnowcurrently opened this issue Nov 9, 2023 · 1 comment
Closed
22 tasks
Labels
3.13 bugs and security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@ericsnowcurrently
Copy link
Member

ericsnowcurrently commented Nov 9, 2023

Feature or enhancement

traceback.TracebackException currently captures all the information common to all exceptions. It also captures some of the information that is specific to certain builtin exceptions, enough to print all exceptions.

I don't have a motivating use case. My interest here is mostly conceptual. I think of TracebackException as a summary or representative proxy of an exception and currently there are gaps in that representation. If TracebackException is really only intended for printing exceptions/tracebacks then there probably isn't much to do here (and I'm okay with that). That said, I'll proceed here with my thoughts.

There are two categories of gap-in-representation that I see:

  • some builtin exceptions aren't represented fully (e.g. ImportError)
  • custom information for user-defined exception types aren't represented at all

Ultimately, it should probably be each exception type that determines what information should be preserved in a snapshot like TracebackException. That suggests room for a new protocol (i.e. "dunder" method). At the very least it would be nice to rely on exc.args, which all exceptions have, and map that to names. For non-builtin exceptions I don't see a real alternative to a general protocol.

For builtin exceptions, however, we can fill in the gaps in TracebackException manually, following the pattern of what we've done for SyntaxError and ExceptionGroup.

  • ImportError
    • msg
    • name
    • path
    • name_from
  • OSError
    • errno
    • winerror
    • strerror
    • filename
    • filename2
  • SystemExit
    • code
  • UnicodeError
    • encoding
    • reason
    • object
    • start
    • end
  • BlockingIOError
    • characters_written
  • BaseExceptionGroup
    • message
  • StopIteration
    • value
  • NameError
    • name
  • AttributeError
    • name
    • obj
  • SyntaxError
    • print_file_and_line

Related:

@ericsnowcurrently ericsnowcurrently added type-feature A feature request or enhancement stdlib Python modules in the Lib dir 3.13 bugs and security fixes labels Nov 9, 2023
@ericsnowcurrently
Copy link
Member Author

On reflection, TracebackException isn't meant to be an actual snapshot of an exception. Rather, it's main purpose is to facilitate error display. Thus, it only needs to preserve information that affects how the exception is formatted, and it does not need these extra attributes.

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

No branches or pull requests

1 participant