Skip to main content

A simple, zero-dependency utility to raise exceptions as if they originated directly from the invoking method

Project description

Introduction

clean_raise is a lightweight utility designed to raise exceptions as if they originated directly from the invoking method. By omitting the internal module's call stack, it keeps your traceback outputs exceptionally clean.

Supported Versions

  • Overall Package Support: Python 3.9 ~ Python 3.15
  • Distribution Strategy: This project distributes independent wheel files optimized for specific Python versions. The source code version provided here (which parses co_exceptiontable and handles Intrinsic Opcodes) is explicitly designed for the low-level exception architectures of Python 3.13 ~ 3.15 (and 3.11+).

Basic Usage

from raise_tool import clean_raise

def func():
    # This raises an exception without exposing clean_raise's internal stack
    clean_raise(RuntimeError('An error occurred during runtime'))

func()

You will get a traceback like this, entirely skipping the execution frames of raise_tool itself:

Traceback (most recent call last):
  File "(FILEPATH)", line 6, in <module>
    func()
    ~~~~^^
RuntimeError: An error occurred during runtime

Advanced API

The lasti_move Parameter

clean_raise accepts an optional bytecode offset parameter, lasti_move, allowing you to manually recalibrate where the traceback indicator (^) points at the bytecode level.

def clean_raise(exception: Any | None = None, lasti_move: int = 0, /) -> NoReturn:
  • exception: The exception instance or class to be raised. If omitted, it defaults to raising RuntimeError('No active exception to reraise').
  • lasti_move: The bytecode instruction offset (positive moves forward, negative moves backward). Use this to fine-tune the traceback pointer if it does not align precisely with your target code block.

How it Works & Known Limitations

To strip the internal call stack from the traceback, this utility employs low-level and somewhat intrusive runtime operations. Please be aware of the following behaviors when integrating this module:

1. Global sys.excepthook Hijacking

To prevent a native raise statement from duplicating the error message on screen, this tool overrides the global sys.excepthook.

  • Mechanism: When clean_raise is invoked, it manually constructs a TracebackType chain, flushes the clean error directly via sys.__excepthook__, flags an internal is_silenced state to True, and then triggers a literal raise. The overridden hook catches and suppresses this subsequent exception to prevent a double print.
  • Side Effects: If your project or other third-party libraries (e.g., Sentry, custom logging frameworks) heavily rely on or alter sys.excepthook, conflicts may occur.

2. Behavior Within try-except Blocks

  • To ensure exceptions are not accidentally swallowed within active error-handling scopes, the utility reversely parses the current frame's co_exceptiontable.
  • If it detects that clean_raise was invoked inside a handled block (by verifying the presence of CALL_INTRINSIC_1 / CALL_INTRINSIC_2 opcodes), it will temporarily bypass the silencing mechanism. This allows the exception to bubble up normally so that external except clauses can capture it correctly.

3. Global Scope Restrictions

clean_raise() relies on traversing back up the execution frames to synthesize the clean traceback. Consequently, it cannot be called directly from the global scope. Violating this constraint will fall back to throwing: RuntimeError('clean_raise() cannot be called from the global scope').

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

clean_raise-1.0.3-py313-none-any.whl (8.9 kB view details)

Uploaded Python 3.13

clean_raise-1.0.3-py312-none-any.whl (9.0 kB view details)

Uploaded Python 3.12

clean_raise-1.0.3-py311-none-any.whl (9.0 kB view details)

Uploaded Python 3.11

clean_raise-1.0.3-py39-none-any.whl (8.8 kB view details)

Uploaded Python 3.9

File details

Details for the file clean_raise-1.0.3-py313-none-any.whl.

File metadata

  • Download URL: clean_raise-1.0.3-py313-none-any.whl
  • Upload date:
  • Size: 8.9 kB
  • Tags: Python 3.13
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.15.0a3

File hashes

Hashes for clean_raise-1.0.3-py313-none-any.whl
Algorithm Hash digest
SHA256 bc2ec2a835e4994c19c88767ca6b265f7cd357c4b0217c5e76c4863d1db60950
MD5 e2bb549722f1a1fcfba93b148975e836
BLAKE2b-256 afb6a627779fc39a5abb4b75347c384a8339bf155096b2b6591c9971ffc17247

See more details on using hashes here.

File details

Details for the file clean_raise-1.0.3-py312-none-any.whl.

File metadata

  • Download URL: clean_raise-1.0.3-py312-none-any.whl
  • Upload date:
  • Size: 9.0 kB
  • Tags: Python 3.12
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.15.0a3

File hashes

Hashes for clean_raise-1.0.3-py312-none-any.whl
Algorithm Hash digest
SHA256 47ceadfc8bc150ae1a5a2cbd93351dc51b0805b8ebbf2970ebb133ac412edf51
MD5 932e42c42a0f522978c1bf56a0978a43
BLAKE2b-256 faea8f03970d2cb904bece8cb96fbf74ed9c0b1d74c030b94e458b678ce064ea

See more details on using hashes here.

File details

Details for the file clean_raise-1.0.3-py311-none-any.whl.

File metadata

  • Download URL: clean_raise-1.0.3-py311-none-any.whl
  • Upload date:
  • Size: 9.0 kB
  • Tags: Python 3.11
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.15.0a3

File hashes

Hashes for clean_raise-1.0.3-py311-none-any.whl
Algorithm Hash digest
SHA256 9192b212e6316e8e71bdbc8885665a76b7e1bccdb001346af7320ef8eb86b0dc
MD5 f6e027fadeabca78b19942042cf07c70
BLAKE2b-256 ed5969065f59499129958ce224bead895e240e362161fe945774c72de9c2fc8f

See more details on using hashes here.

File details

Details for the file clean_raise-1.0.3-py39-none-any.whl.

File metadata

  • Download URL: clean_raise-1.0.3-py39-none-any.whl
  • Upload date:
  • Size: 8.8 kB
  • Tags: Python 3.9
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.15.0a3

File hashes

Hashes for clean_raise-1.0.3-py39-none-any.whl
Algorithm Hash digest
SHA256 51d8809e646972fe8b6d2c43beec97cf9b1beebdaa80993139aae725ace31370
MD5 7b4a8f1c34eee62d29b8b11c4159361f
BLAKE2b-256 61d4fbe043a573e385da69127e6aceb29224194c81adb78c67b19f67abd39580

See more details on using hashes here.

Supported by

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