Backport of PEP 654 (exception groups)
Project description
This is a backport of the BaseExceptionGroup and ExceptionGroup classes from Python 3.11.
It contains the following:
The exceptiongroup.BaseExceptionGroup and exceptiongroup.ExceptionGroup classes
A utility function (exceptiongroup.catch()) for catching exceptions possibly nested in an exception group
Patches to the TracebackException class that properly formats exception groups (installed on import)
An exception hook that handles formatting of exception groups through TracebackException (installed on import)
If this package is imported on Python 3.11 or later, the built-in implementations of the exception group classes are used instead, TracebackException is not monkey patched and the exception hook won’t be installed.
See the standard library documentation for more information on exception groups.
Catching exceptions
Due to the lack of the except* syntax introduced by PEP 654 in earlier Python versions, you need to use exceptiongroup.catch() to catch exceptions that are potentially nested inside an exception group. This function returns a context manager that calls the given handler for any exceptions matching the sole argument.
The argument to catch() must be a dict (or any Mapping) where each key is either an exception class or an iterable of exception classes. Each value must be a callable that takes a single positional argument. The handler will be called at most once, with an exception group as an argument which will contain all the exceptions that are any of the given types, or their subclasses. The exception group may contain nested groups containing more matching exceptions.
Thus, the following Python 3.11+ code:
try:
...
except* (ValueError, KeyError) as excgroup:
for exc in excgroup.exceptions:
print('Caught exception:', type(exc))
except* RuntimeError:
print('Caught runtime error')
would be written with this backport like this:
from exceptiongroup import ExceptionGroup, catch
def value_key_err_handler(excgroup: ExceptionGroup) -> None:
for exc in excgroup.exceptions:
print('Caught exception:', type(exc))
def runtime_err_handler(exc: ExceptionGroup) -> None:
print('Caught runtime error')
with catch({
(ValueError, KeyError): value_key_err_handler,
RuntimeError: runtime_err_handler
}):
...
NOTE: Just like with except*, you cannot handle BaseExceptionGroup or ExceptionGroup with catch().
Notes on monkey patching
To make exception groups render properly when an unhandled exception group is being printed out, this package does two things when it is imported on any Python version earlier than 3.11:
The traceback.TracebackException class is monkey patched to store extra information about exception groups (in __init__()) and properly format them (in format())
An exception hook is installed at sys.excepthook, provided that no other hook is already present. This hook causes the exception to be formatted using traceback.TracebackException rather than the built-in rendered.
To prevent the exception hook and patches from being installed, set the environment variable EXCEPTIONGROUP_NO_PATCH to 1.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file exceptiongroup-1.0.0rc7.tar.gz
.
File metadata
- Download URL: exceptiongroup-1.0.0rc7.tar.gz
- Upload date:
- Size: 11.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.0 CPython/3.9.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2019e6e9d6ef84ff65c5c7c451b75802c662d53bcb124abf47ad118d39f9a1ca |
|
MD5 | 58dbca426bfda84c791250439674f69b |
|
BLAKE2b-256 | e74f0e7d12b079b8d694be4b47997780d149e14a504a638caa56ac03ef48e248 |
Provenance
File details
Details for the file exceptiongroup-1.0.0rc7-py3-none-any.whl
.
File metadata
- Download URL: exceptiongroup-1.0.0rc7-py3-none-any.whl
- Upload date:
- Size: 11.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.0 CPython/3.9.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 82dc1cfc21bb5f921a25925e80ec696c71b7d108c6c63bd8a786abe2b73c3263 |
|
MD5 | 553886d8703e6b0f3e5d27138d190014 |
|
BLAKE2b-256 | 8b31e81a7685eed779b50632cfe53202225584e9635efc817a01076ff6214733 |