Skip to main content

exguard - Guard code against exceptions, e.g. for running untrusted module code in a framework

Description

exgurd provides a decorator to guard a function or method against exceptions. This is useful in situations where untrusted code is executed, e.g. when executing a third-party module in a framework.

In comparison to the suppress context manager from Python 3, exguard provides more means to control exception handling:

  • filter on modules and namespaces to define which exceptions to catch and leave exceptions in other code untouched

  • execute a callback function once an exception was caught

  • execute a callback function in the finally block

For example, an API method in a web application that calls through to code in third-party modules can be guarded against exceptions in this modules only, while having exceptions in the framework itself be raised verbatim:

from exguard import guard, traceback_str

def throw_to_client(exception, module):
    tb = traceback_str(exception)

    # Do something to pass a traceback to the browser, disable the plugin,…
    pass

# All third-party modules are under one namespace
@guard(modules=["myframework.plugins"], submodules=True, fullstack=True, cb_except=throw_to_client)
def call_plugin_api():
    # An exception inside this will be caught
    myframework.plugins.evil.crash()

    # This will still raise an exception like normal
    x = 17 / 0

Authors

exguard has been started as a part of Veripeditus, mainly by Eike Tim Jesinghaus <eike@naturalnet.de>.

Changelog

0.2.1

  • Guard against <string> or other non-module frames.

0.2

  • Add utility function to get full traceback as string.

  • Re-licence under MIT.

  • Minor documentation updates.

0.1

  • First release.

Metadata

Release files for exguard 0.2.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 exguard 0.2.1
File Size Uploaded
exguard-0.2.1.tar.gz 4.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for exguard 0.2.1
File Interpreter ABI Platform
exguard-0.2.1-py3-none-any.whl Python 3 none any Details

Total release size: 11.1 kB

Release files / exguard-0.2.1.tar.gz

Download URL exguard-0.2.1.tar.gz
Size 4.1 kB
Tags Source
SHA-256 checksum
How to use checksums
e4b06870a9791fb4a8d7f84af2aef54eb3383251d1ef79e0a019ebe277d2c015
BLAKE2b-256 checksum
How to use checksums
457f777821ee51265f83e157f9f0aca4792024c645811ee70142b48f47435e43
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / exguard-0.2.1-py3-none-any.whl

Download URL exguard-0.2.1-py3-none-any.whl
Size 7.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b955add660111f120ceb0e7b113539d82e5a9823d28b657929efc98a668971b5
BLAKE2b-256 checksum
How to use checksums
f10a11208767200ebe041a41770b76d0b0d929717c88349552cb72d868096113
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.2.1 This release

2 release files

0.2

2 release files

0.1

2 release files

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