Skip to main content

Backport of PEP 654 (exception groups)

Project description

Build Status Code Coverage

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 which is the exception object to be handled.

Thus, the following Python 3.11+ code:

try:
    ...
except* (ValueError, KeyError) as exc:
    print('Caught exception:', type(exc))
except* RuntimeError:
    print('Caught runtime error')

would be written with this backport like this:

from exceptiongroup import catch

def value_key_err_handler(exc: Exception) -> None:
    print('Caught exception:', type(exc))

def runtime_err_handler(exc: RuntimeError) -> 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:

  1. The traceback.TracebackException class is monkey patched to store extra information about exception groups (in __init__()) and properly format them (in format())

  2. 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

exceptiongroup-1.0.0rc2.tar.gz (10.0 kB view details)

Uploaded Source

Built Distribution

exceptiongroup-1.0.0rc2-py3-none-any.whl (10.4 kB view details)

Uploaded Python 3

File details

Details for the file exceptiongroup-1.0.0rc2.tar.gz.

File metadata

  • Download URL: exceptiongroup-1.0.0rc2.tar.gz
  • Upload date:
  • Size: 10.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.10.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10

File hashes

Hashes for exceptiongroup-1.0.0rc2.tar.gz
Algorithm Hash digest
SHA256 4d254b05231bed1d43079bdcfe0f1d66c0ab4783e6777a329355f9b78de3ad83
MD5 5e2fc80e9a55c9d65fe72abc3784fe5e
BLAKE2b-256 afd21c6b34cecf5ef7e5ffebcefe2c74e7583f15d9547f0bc38b64ce75bf369b

See more details on using hashes here.

File details

Details for the file exceptiongroup-1.0.0rc2-py3-none-any.whl.

File metadata

  • Download URL: exceptiongroup-1.0.0rc2-py3-none-any.whl
  • Upload date:
  • Size: 10.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.10.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10

File hashes

Hashes for exceptiongroup-1.0.0rc2-py3-none-any.whl
Algorithm Hash digest
SHA256 83e465152bd0bc2bc40d9b75686854260f86946bb947c652b5cafc31cdff70e7
MD5 12c76488e8ef3fac01a113e3fafe3133
BLAKE2b-256 849e0aaafdcfb4613b045fa547a73b51ece17df0e2389de153172ddc2c0c1f6d

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page