stdio_proxy
stdio_proxy is a thread-safe library for Python 2.7 and Python 3.5+ that can temporarily redirect stdio to another objects.
Background
Python 3.5+ has redirect_stdout and redirect_stderr in contextlib, that are utility functions for temporarily redirecting sys.stdout and sys.stderr to another file-like objects, respectively.
But those functions have the global side effect on sys.stdout and sys.stderr.
That means we cannot use those functions in most threaded applications.
- Python code
import contextlib
import io
import time
from concurrent.futures import ThreadPoolExecutor
def run(value):
for i in range(2):
print("Hello from {}:{}".format(value, i))
time.sleep(1)
def run_hook(value):
buf = io.StringIO()
with contextlib.redirect_stdout(buf):
run(value)
return buf.getvalue()
with ThreadPoolExecutor() as executor:
f1 = executor.submit(run, "th1")
f2 = executor.submit(run_hook, "th2")
f1.result()
result = f2.result()
print("===Done===")
print("Redirected Stdout:\n{}".format(result))
- What we want
Hello from th1:0
Hello from th1:1
===Done===
Redirected Stdout:
Hello from th2:0
Hello from th2:1
- Example of actual output
Hello from th1:0
===Done===
Redirected Stdout:
Hello from th2:0
Hello from th1:1
Hello from th2:1
This library aims to redirect those stdio correctly in threaded applications as well.
By just replacing run_hook function with the following code, the result would be exactly the same as "what we want" :)
def run_hook(value):
buf = io.BytesIO()
with stdio_proxy.redirect_stdout(buf):
run(value)
return buf.getvalue()
Install
$ pip install stdio-proxy
Usage
- Redirect a buffer to
stdin
buf = io.BytesIO(b"foo\n")
with stdio_proxy.redirect_stdin(buf):
print("Read: {}".format(sys.stdin.readline()))
- Redirect
stdoutto a buffer
buf = io.BytesIO()
with stdio_proxy.redirect_stdout(buf):
print("foo")
print("Redirected: {}".format(buf.getvalue()))
- Redirect
stderrto a buffer
buf = io.BytesIO()
with stdio_proxy.redirect_stderr(buf):
sys.stderr.write("foo\n")
print("Redirected: {}".format(buf.getvalue()))
License
MIT License
Metadata
Release files for stdio-proxy 0.1.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Built distributions (wheels)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| stdio_proxy-0.1.3-py3-none-any.whl | Python 3 | none | any | Details for stdio_proxy-0.1.3-py3-none-any.whl |
| stdio_proxy-0.1.3-py2-none-any.whl | Python 2 | none | any | Details for stdio_proxy-0.1.3-py2-none-any.whl |
Total release size: 12.6 kB
Release files / stdio_proxy-0.1.3-py3-none-any.whl
| Download URL | stdio_proxy-0.1.3-py3-none-any.whl |
|---|---|
| Size | 6.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ddbb8bf6e56e3ed7cafd71bf8a9facd9582e8392ad89f7898182f020edd5e365
|
|
BLAKE2b-256 checksum How to use checksums |
5cf8b48162d4e996810e37504f925f14121d38eb0e1ae5eb230c4f39a448b86d
|
| 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/42.0.1 requests-toolbelt/0.9.1 tqdm/4.40.1 CPython/3.6.9
|
Release files / stdio_proxy-0.1.3-py2-none-any.whl
| Download URL | stdio_proxy-0.1.3-py2-none-any.whl |
|---|---|
| Size | 6.3 kB |
| Tags | Python 2 |
|
SHA-256 checksum How to use checksums |
9bf9cdaa4b68b0abf25eccf372de9e6497f91096390a5c53b219b394ffb1fbfd
|
|
BLAKE2b-256 checksum How to use checksums |
0942629491ff2ac35f4a1af3ea18c6444a020bcd63c85bdcd526d34d68b68238
|
| 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/42.0.1 requests-toolbelt/0.9.1 tqdm/4.40.1 CPython/3.6.9
|
Security for stdio-proxy 0.1.3
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.