Skip to main content

suppress-unhandled

Suppress traceback of specified unhandled exceptions, preserve everything else.

from suppress_unhandled import suppress_unhandled

suppress_unhandled(KeyboardInterrupt)
input('Pause')
$ python example.py
Pause ^C
$

Install

pip install suppress-unhandled

Exception groups

Pass inspect_groups to also suppress a group whose contents, however deeply nested, are all being suppressed:

import asyncio
from suppress_unhandled import suppress_unhandled

suppress_unhandled(ValueError, inspect_groups=True)

async def some_task():
    raise ValueError('oh no!')

async def main():
    async with asyncio.TaskGroup() as group:
        group.create_task(some_task())
        group.create_task(some_task())

asyncio.run(main())
$ python example.py
$

asyncio.TaskGroup collects both failures into an ExceptionGroup, which suppress_unhandled(ValueError), without inspect_groups=True, will not match:

$ python example.py
  + Exception Group Traceback (most recent call last):
  ...
  | ExceptionGroup: unhandled errors in a TaskGroup (2 sub-exceptions)
  +-+---------------- 1 ----------------
    | ValueError: oh no!
    +---------------- 2 ----------------
    | ValueError: oh no!
    +------------------------------------

Groups containing anything else are not suppressed and their tracebacks print.

Background

Ctrl+C is often the normal way to exit a Python program. By default, Python prints a scary-looking traceback to the stderr:

$ python example.py
^CTraceback (most recent call last):
  File "/tmp/example.py", line 1, in <module>
    do_something_forever()
    ~~~~~~~~~~~~~~~~~~~~^^
KeyboardInterrupt

You could suppress KeyboardInterrupt:

from contextlib import suppress

with suppress(KeyboardInterrupt):
    do_something_forever()

The traceback is gone, but the return code is wrong and, if you add code after the with block, it'll get run! There's also all this complex behaviour that gets suppressed.

Metadata

Release files for suppress-unhandled 0.3.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for suppress-unhandled 0.3.1
File Size Uploaded Provenance Actions
suppress_unhandled-0.3.1.tar.gz 4.1 kB Without provenance Details for suppress_unhandled-0.3.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for suppress-unhandled 0.3.1
File Interpreter ABI Platform Actions
suppress_unhandled-0.3.1-py3-none-any.whl Python 3 none any Details for suppress_unhandled-0.3.1-py3-none-any.whl

Total release size: 7.9 kB

Release files / suppress_unhandled-0.3.1.tar.gz

Download URL suppress_unhandled-0.3.1.tar.gz
Size 4.1 kB
Tags Source
SHA-256 checksum
How to use checksums
c0b512be54594fa178561c26ea752c44a98521a7dba21bc2b9a267c842ffe9fe
BLAKE2b-256 checksum
How to use checksums
f31deae53b49a3891af32f4e8d80cbf7e813fa602f566fc9fe41269fe5917003
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.8

Release files / suppress_unhandled-0.3.1-py3-none-any.whl

Download URL suppress_unhandled-0.3.1-py3-none-any.whl
Size 3.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9d6fed86a4646d4e6d44cf429698ce6e2fbb291cbf094feb553636b605e101f1
BLAKE2b-256 checksum
How to use checksums
ebf4ae8492c3c714cba5a81ff98125d8f641c643f5747074d45c1704ea8ce856
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.8

Security for suppress-unhandled 0.3.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page