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" propertystderr- Default False. Capture stderr to the "stderr" propertypython_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
Metadata
Release files for stream-redirect 0.1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| stream-redirect-0.1.2.tar.gz | 4.2 kB | Without provenance | Details for stream-redirect-0.1.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| stream_redirect-0.1.2-py3-none-any.whl | Python 3 | none | any | Details for stream_redirect-0.1.2-py3-none-any.whl |
Total release size: 21.1 kB
Release files / stream-redirect-0.1.2.tar.gz
| Download URL | stream-redirect-0.1.2.tar.gz |
|---|---|
| Size | 4.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
66b96d71e82d23136d5d7614f53c5014116f3528da2457f3e36d4f31cc7a1b98
|
|
BLAKE2b-256 checksum How to use checksums |
71f72a49c28dc5a2b3734f1a8a15c4e98ed0b855e83d31e6bd78ecc8c14550b2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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
|
Release files / stream_redirect-0.1.2-py3-none-any.whl
| Download URL | stream_redirect-0.1.2-py3-none-any.whl |
|---|---|
| Size | 16.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
77b7a1fc015d9a1c031fa5e4d6a364c41878215d7116e33c99b329af7a30dde7
|
|
BLAKE2b-256 checksum How to use checksums |
8430a6fb293077c627971976aa5ea23b21a9830ae5f2669a8ba285cf74602b32
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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
|
Security for stream-redirect 0.1.2
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.