Skip to main content

Compatibility Implementations Format Downloads

Capture stdout/stderr and optionally release when an exception occurs.

from abduct import captured, out, err

with captured(out()) as stdout:
    ...

with captured(out(), err()) as (stdout, stderr):
    ...

@captured(out(), err())
...

Installation:

$ pip install abduct

When stdout or stderr is captured, the related sys.stdout or sys.stderr object is replaced with a StringIO object for the life of the context.

Examples

It’s often useful to capture the output of a block of code. Abduct makes this easy:

with captured(out()) as stdout:
    print('hello!')

assert stdout.getvalue() == 'hello!'

Sometimes you may want to hide the output of some code unless something goes wrong. In this case, simply specify release_on_exception=True:

with captured(out(release_on_exception=True)):
    print('Really important message!')
    if blow_up:
        raise RuntimeError()

In this case, Really important message! will be printed on stdout if the exception is raised.

If you’d like to capture the output, but still write through to stdout or stderr, use the tee=True parameter:

with captured(err(tee=True)) as stderr:
    sys.stderr.write('Error!')

assert stderr.getvalue() == 'Error!'

In this case, Error! is captured and written to stderr at the same time.

Changelog

2.0.1

  • Added tests for the decorator usage.

2.0.0

  • Feature: “Create a write-through option for output.”

  • Backwards-incompatible change: stdout and stderr methods are now out and err respectively.

1.0.4

  • Fixed Travis release criteria.

1.0.3

  • Refactored test runner.

1.0.2

  • Fixed README and description.

1.0.1

  • Travis config now defers to tox.

  • Added examples to README.

1.0.0

  • Actual working code. Yay!

0.0.1

  • Initial release.

Metadata

Release files for abduct 2.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for abduct 2.0.1
File Size Uploaded Provenance Actions
abduct-2.0.1.tar.gz 2.9 kB Without provenance Details for abduct-2.0.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for abduct 2.0.1
File Interpreter ABI Platform Actions
abduct-2.0.1-py2.py3-none-any.whl Python 3, Python 2 none any Details for abduct-2.0.1-py2.py3-none-any.whl

Total release size: 6.4 kB

Release files / abduct-2.0.1.tar.gz

Download URL abduct-2.0.1.tar.gz
Size 2.9 kB
Tags Source
SHA-256 checksum
How to use checksums
7617a853d5feed772863c4cbf51adedd7d06ba025ea1396224a5f0becdb9cf2f
BLAKE2b-256 checksum
How to use checksums
fc0b7880d5c7e482cefb58a4049df5ef486639e7c4a44a33a7eed7eba9a1ea7b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / abduct-2.0.1-py2.py3-none-any.whl

Download URL abduct-2.0.1-py2.py3-none-any.whl
Size 3.4 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
a3d1a3dcc7e544054534a1e10c3443d8ed5cde57c9dc47da8484910c36478a3b
BLAKE2b-256 checksum
How to use checksums
23165e3393ab3c9c5a4c15feab2e95636409087fac077438a80fa35861c9ac5e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for abduct 2.0.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page