Skip to main content

Collect and redirect stdout and stderr from a python program

Project description

Check Pull Request

stdout_redirect

What?

A Python library to collect and redirect code from stdout (and/or stderr) and collect it into program-accessible locations.

Tested and working on Linux, Mac OS, and Windows!

How?

pip install -U stream-redirect

from stream_redirect import Redirect

r = Redirect(stdout=True, stderr=True)
with r:
    print("This will be captured")
    os.system("echo This will also be captured")
    some_c_module.method_that_writes("This willl be, too")

print("Captured stdout: ", r.stdout)
print("Captured stderr: ", r.stderr)

Construction

Redirect accepts three arguments:

  • stdout - Default True. Capture stdout to the "stdout" property
  • stderr - Default False. Capture stderr to the "stderr" property
  • python_only - Default False. Only capture the output of Python code and not the output of system calls and C libraries. This behavior mimics the built in context manager (discussed below) and is useful when you just want to drop in this to replace that one in versions of Python where it doesn't exist

Usage

If you only want to silence output you can do

with Redirect():
   pass

However, if you wish to inspect the results of the run, you need to create and read the context manager outside of the with invocation, as the object created during the call is only accessible within the body of the with block.

Why?

Isn't there already a standard module for this?

It's true that the standard library includes a context decorator that already redirects stdout for most normal uses. But this is not sufficient. It fails on at least a few scenarios.

It is new

The built-in module won't help you on Python 2.7 or Python 3.0-3.3 (where it does not exist).

This module will do so

It doesn't capture system calls

The above module doesn't capture system calls

In [1]: import io, os, contextlib

In [2]: s = io.StringIO()

In [3]: with contextlib.redirect_stdout(s):
   ...:     os.system("echo Hello world")
   ...:
Hello world

In [4]: print("Captured output: ", s.getvalue())
Captured output:

This module will do so

It doesn't capture output from the C library

If an extension module written in C writes output, the built-in context wrapper will not capture it.

This module will do so

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

stream-redirect-0.1.2.tar.gz (4.2 kB view details)

Uploaded Source

Built Distribution

stream_redirect-0.1.2-py3-none-any.whl (16.9 kB view details)

Uploaded Python 3

File details

Details for the file stream-redirect-0.1.2.tar.gz.

File metadata

  • Download URL: stream-redirect-0.1.2.tar.gz
  • Upload date:
  • Size: 4.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.1.0 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.7.6

File hashes

Hashes for stream-redirect-0.1.2.tar.gz
Algorithm Hash digest
SHA256 66b96d71e82d23136d5d7614f53c5014116f3528da2457f3e36d4f31cc7a1b98
MD5 eca779e9830b322a415c6fdc0a0683cd
BLAKE2b-256 71f72a49c28dc5a2b3734f1a8a15c4e98ed0b855e83d31e6bd78ecc8c14550b2

See more details on using hashes here.

File details

Details for the file stream_redirect-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: stream_redirect-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 16.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.1.0 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.7.6

File hashes

Hashes for stream_redirect-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 77b7a1fc015d9a1c031fa5e4d6a364c41878215d7116e33c99b329af7a30dde7
MD5 52821570fa080469081e4491ff2b5d30
BLAKE2b-256 8430a6fb293077c627971976aa5ea23b21a9830ae5f2669a8ba285cf74602b32

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