Skip to main content

A python package for removing stack frames from stack traces.

Project description

dropstackframe

A python library for dropping stack frames.

This can be useful for removing decorators from stack traces, when using a framework with a lot of decorators.

Example

Let's us write a small decorator for measuring the time it takes to call a function:

from time import perf_counter


def measure_time(func):
    def wrapper(*args, **kwargs):
        before = perf_counter()
        result = func(*args, **kwargs)
        after = perf_counter()
        print(f"{func.__name__} took {after - before}s.")
        return result

    return wrapper

We can use it like this:

@measure_time
def foo(should_raise):
    assert not should_raise
    return 42


@measure_time
def bar(should_raise):
    return foo(should_raise)


@measure_time
def baz(should_raise):
    return bar(should_raise)


baz(False)

On my computer this prints:

foo took 2.2800122678745538e-07s.
bar took 3.576500057533849e-05s.
baz took 4.227000135870185e-05s.

Great. But what happens if we raise an error?

baz(True)

yields:

Traceback (most recent call last):
  File "example.py", line 32, in <module>
    baz(True)
  File "example.py", line 7, in wrapper
    result = func(*args, **kwargs)
  File "example.py", line 28, in baz
    return bar(should_raise)
  File "example.py", line 7, in wrapper
    result = func(*args, **kwargs)
  File "example.py", line 23, in bar
    return foo(should_raise)
  File "example.py", line 7, in wrapper
    result = func(*args, **kwargs)
  File "example.py", line 17, in foo
    assert not should_raise
AssertionError

Notice how every other line is the wrapper from our decorator? If we have large codebase and it is using a framework with a lot of decorators, this can make the stack traces hard to read, because most of the frames are irrelevant decorators.

We can use the dropstackframe library to rewrite our decorator:

from time import perf_counter
from dropstackframe import drop_stack_frame


def measure_time(func):
    def wrapper(*args, **kwargs):
        before = perf_counter()
        try:
            result = func(*args, **kwargs)
        except Exception:
            drop_stack_frame()
            raise
        after = perf_counter()
        print(f"{func.__name__} took {after - before}s.")
        return result

    return wrapper

Now, if we get an error:

baz(True)

we get:

Traceback (most recent call last):
  File "example2.py", line 37, in <module>
    baz(True)
  File "example2.py", line 33, in baz
    return bar(should_raise)
  File "example2.py", line 28, in bar
    return foo(should_raise)
  File "example2.py", line 22, in foo
    assert not should_raise
AssertionError

and all the annoying wrapper stack frames have been removed.

Disabling dropstackframe

Let's say you have a large codebase that uses dropstackframe and one day you have a bug that is really hard to find. In fact you start suspecting that the bug might be hidden by drop_stack_frame. You can use set_enable_drop_stack_frame to disable drop_stack_frame:

from dropstackframe import set_enable_drop_stack_frame

set_enable_drop_stack_frame(False)
baz(True)

set_enable_drop_stack_frame can also be used as a context manager, if you only want to disable drop_stack_frame in a limited region of your code:

from dropstackframe import set_enable_drop_stack_frame

with set_enable_drop_stack_frame(False):
    baz(True)

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

dropstackframe-0.1.1.tar.gz (4.2 kB view details)

Uploaded Source

Built Distribution

dropstackframe-0.1.1-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

Details for the file dropstackframe-0.1.1.tar.gz.

File metadata

  • Download URL: dropstackframe-0.1.1.tar.gz
  • Upload date:
  • Size: 4.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.0 CPython/3.12.5

File hashes

Hashes for dropstackframe-0.1.1.tar.gz
Algorithm Hash digest
SHA256 c264193d1941f247ecd719126a64b9c0fb06da83c4600fbb36ef061e4a135989
MD5 68361bb7a124fb04139ea256050d4580
BLAKE2b-256 e90c351f6871bcec45326cbce70b10bdaea585b9aec5fce60bf989b5454c8d13

See more details on using hashes here.

File details

Details for the file dropstackframe-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for dropstackframe-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 71cd73f26ed49c3111c3f1689013870561d4a6aa0211fe56dcfc431fec63713e
MD5 feb41cf5232d23e3574b237b094e95d4
BLAKE2b-256 b0a34da11dccd2be6accd0d298eb85670b0b6e7ffa172246371d1a27f05df2d3

See more details on using hashes here.

Supported by

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