https://github.com/greg-hellings/stream-redirect
https://github.com/greg-hellings/stream-redirect
Last synced: 3 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/greg-hellings/stream-redirect
- Owner: greg-hellings
- License: gpl-3.0
- Created: 2020-02-12T07:34:09.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2020-02-14T07:12:24.000Z (over 6 years ago)
- Last Synced: 2025-09-07T20:43:18.179Z (10 months ago)
- Language: Python
- Size: 50.8 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://github.com/greg-hellings/stream-redirect/actions)
[](https://codecov.io/gh/greg-hellings/stream-redirect)
[](https://badge.fury.io/py/stream-redirect)
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`
```python
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
```python
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](https://docs.python.org/3/library/contextlib.html#contextlib.redirect_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