Skip to main content

Save objects when an exception occurs using pickle.

Project description

bodysnatcher

Save objects when an exception occurs using pickle.

What it does

Bodysnatcher dumps frame locals() if a context is exitted via an exception.

with Bodysnatcher():
    ...
    raise Exception  # Will dump context locals()

However, it will not dump itself as that's probably not relevant.

[!WARNING] The locals() table can be quite large depending in which scope you create the context.

By default, it dumps to the current working directory. That can be configured using the path argument.

with Bodysnatcher("/tmp/"): ...  # Dump objects to /tmp/ (e.g., /tmp/foo.pkl)

Example usage

Run

from collections.abc import Generator

from bodysnatcher import Bodysnatcher


def may_err() -> Generator[str, None, None]:
    yield "Hello"
    yield "World"
    raise RuntimeError


def main() -> int:
    data = []
    with Bodysnatcher():
        for elt in may_err():
            data.append(elt)
    return 0


if __name__ == "__main__":
    raise SystemExit(main())

and then unpickle to recover the data. For example,

$ python -c "import pickle; print(pickle.load(open('data.pkl', 'rb')))"
['Hello', 'World']

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

bodysnatcher-0.1.1.tar.gz (4.6 kB view hashes)

Uploaded Source

Built Distribution

bodysnatcher-0.1.1-py3-none-any.whl (4.0 kB view hashes)

Uploaded Python 3

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