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 details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: bodysnatcher-0.1.1.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.12.2

File hashes

Hashes for bodysnatcher-0.1.1.tar.gz
Algorithm Hash digest
SHA256 4499929d5874f4616a7503d0d6e95e800f2e2130e8e9370751c022939bef211d
MD5 b803163e27446d1d4e7d977c48a463a3
BLAKE2b-256 73d9cbd28c46f6dea6c4bf393994af2d6837d4cc616e65216cbdf8b86a8ce32d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bodysnatcher-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 dcccd15202b828392792a1450ecfdc655c834162162f0f1e8042e5ca43e38342
MD5 92e57a595b704e999536c4c11fbde87d
BLAKE2b-256 cbd1c940d3b1d2f22751f9171ecbb671af3c314982da289e9a5cd229b0cee032

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