Skip to main content

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

Reason this release was yanked:

Yanked due to broken functionality caused by mismatched high-version code introduced via copy-paste.

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.1.0-py313-none-any.whl (8.9 kB view details)

Uploaded Python 3.13

clean_raise-1.1.0-py312-none-any.whl (8.9 kB view details)

Uploaded Python 3.12

clean_raise-1.1.0-py311-none-any.whl (8.9 kB view details)

Uploaded Python 3.11

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

Uploaded Python 3.9

File details

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

File metadata

  • Download URL: clean_raise-1.1.0-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.1.0-py313-none-any.whl
Algorithm Hash digest
SHA256 834c98cd4214c5f1690d468d4a432f3802cc5b9841d7810b28db1877d183a874
MD5 35cf254c980eba56c326bc635273376a
BLAKE2b-256 2121ea34bee2a520f3cdaa21316e7a191d2e6139601ceb1aac4fc1030c1a4eb4

See more details on using hashes here.

File details

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

File metadata

  • Download URL: clean_raise-1.1.0-py312-none-any.whl
  • Upload date:
  • Size: 8.9 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.1.0-py312-none-any.whl
Algorithm Hash digest
SHA256 e12fd6e26baf2b79682f0cf98c4673455ce46dd6921069a37c06398dd4795fb3
MD5 b7ef31c29e55cbf03487d5cce4e5f8ca
BLAKE2b-256 b1fa377c0a1be211da74c4abf2721a53294a1ccc435e642ace68096294584064

See more details on using hashes here.

File details

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

File metadata

  • Download URL: clean_raise-1.1.0-py311-none-any.whl
  • Upload date:
  • Size: 8.9 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.1.0-py311-none-any.whl
Algorithm Hash digest
SHA256 1cfd3c03955001a1d41e18036a55153f16728792d7ebe849cf3f9d7b447f268f
MD5 cf70a94c300a65cbfdcb8e39eed43500
BLAKE2b-256 cc49a85e8910761368753aacbbcc65e88cbbb265de4fe9712b211d698dbf953f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: clean_raise-1.1.0-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.1.0-py39-none-any.whl
Algorithm Hash digest
SHA256 7398c3c333ac7b10539787b62059f5a09bed6e0a9038af7974e6f7e53aca07de
MD5 b57765db37556c310e41139742981932
BLAKE2b-256 2fed0af02ec85f15cad31e322b55ec2abc07c9f43997631878c522b9b6546bf0

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