Skip to main content

Enhanced exception analysis library for Python - prints detailed information about what happened when an exception occurs

Project description

Lunacept

Lunacept is an enhanced exception analysis library for Python. It automatically instruments your code to capture and display the values of intermediate expressions when an exception occurs, making debugging significantly easier.

Instead of just telling you where an error happened, Lunacept tells you why by showing you the runtime values of every part of the failing expression.

Features

  • Detailed Exception Reports: See the values of all sub-expressions in the line that caused the exception.
  • Global Instrumentation: Automatically instruments all modules in your project and handles exceptions.
  • Decorator Support: Use @capture_exceptions to instrument and monitor specific functions.
  • Thread Support: Automatically hooks into threading.excepthook to catch exceptions in threads.
  • Manual Handling: Utilities to print or render detailed exception reports for caught exceptions.

Installation

pip install lunacept

Usage

1. Global Installation

Simply import lunacept and call install():

import lunacept

def get_multiplier(x):
    return x * 2

def complex_calculation(a, b):
    return get_multiplier(a) / (b - 5)

if __name__ == "__main__":
    lunacept.install()
    complex_calculation(10, 5)

Example Output

When the above code runs and fails, Lunacept prints:

Frame #1: "demo.py:11" in <module>()
   line 11, cols 4-30

 10 │ if __name__ == "__main__":
 11 │     lunacept.install()
 12 │     complex_calculation(10, 5)

Expr Tree:
   `-- complex_calculation = <function complex_calculation at 0x...>
────────────────────────────────────────────────────────────

Frame #2: "demo.py:7" in complex_calculation()
   line 7, cols 11-38

  6 │ def complex_calculation(a, b):
  7 │     return get_multiplier(a) / (b - 5)

Expr Tree:
   |-- get_multiplier(a) = 20
   |   |-- get_multiplier = <function get_multiplier at 0x...>
   |   `-- a = 10
   `-- b - 5 = 0
       `-- b = 5
────────────────────────────────────────────────────────────

   ZeroDivisionError: division by zero

Notice how it captures that get_multiplier(a) returned 20 and b - 5 evaluated to 0.

2. Decorator Usage

If you want to target specific functions, use the @capture_exceptions decorator.

from lunacept import capture_exceptions

@capture_exceptions
def complex_calculation(x, y):
    return (x * 2) / (y - 5)

complex_calculation(10, 5)

3. Manual Exception Handling

You can also use Lunacept to print details for exceptions you catch yourself.

import lunacept

try:
    val = some_risky_function()
except Exception as e:
    lunacept.print_exception(e)

How It Works

Lunacept uses Python's ast (Abstract Syntax Tree) module to parse your code and transform it at runtime. It breaks down complex expressions into a series of temporary variable assignments.

For example, an expression like:

result = func(a) + b

might be internally transformed into something like:

__tmp_1 = func(a)
__tmp_2 = __tmp_1 + b
result = __tmp_2

This allows Lunacept to track the value of func(a) and b individually. When an exception occurs, it uses these captured values to generate a detailed report.

Performance

Lunacept is designed to be lightweight, but since it instruments code at runtime, there is some overhead. Below are benchmark results comparing standard execution vs. Lunacept instrumentation (MacBook Pro, Apple M1, 16GB RAM):

Test Case Baseline Instrumented Slowdown
Simple Math (Arithmetic Loop) 0.052 ms 0.068 ms 1.3x
Recursive Fib (Function Calls) 0.062 ms 0.089 ms 1.4x
Complex Logic (Branching) 0.049 ms 0.056 ms 1.1x

The overhead is generally between 1.1x and 1.4x, making it suitable for development and testing environments.

License

This project is licensed under the MIT License.

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

lunacept-0.3.2.tar.gz (20.4 kB view details)

Uploaded Source

Built Distribution

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

lunacept-0.3.2-py3-none-any.whl (15.5 kB view details)

Uploaded Python 3

File details

Details for the file lunacept-0.3.2.tar.gz.

File metadata

  • Download URL: lunacept-0.3.2.tar.gz
  • Upload date:
  • Size: 20.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.7

File hashes

Hashes for lunacept-0.3.2.tar.gz
Algorithm Hash digest
SHA256 940da51aaac3aeb5a03771679af84153b69ed7dfff280850daa8bf41abbc2152
MD5 2177979929ebf6cb95ae5beb97fcbd5e
BLAKE2b-256 99921ffefbbcf14cb7ef08364830e70bb965166f2782eb4faf9e8141ff98346f

See more details on using hashes here.

File details

Details for the file lunacept-0.3.2-py3-none-any.whl.

File metadata

  • Download URL: lunacept-0.3.2-py3-none-any.whl
  • Upload date:
  • Size: 15.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.7

File hashes

Hashes for lunacept-0.3.2-py3-none-any.whl
Algorithm Hash digest
SHA256 a28079273d0d55b6cfd48b7f2711671873c93aaf4177e8bc19ccc4dca0421b2c
MD5 267d2c5b48778b8b04ac43d835364c85
BLAKE2b-256 3317b7c29d3515e3219df199263bd09ac15938c36abf1e6e5770bdb747b07279

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