Capture C-level stdout/stderr in Python
Switch branches/tags
Nothing to show
Clone or download
Latest commit 088bb99 Dec 12, 2018
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.bumpversion.cfg Bump version: 1.0.2 → 1.0.3.dev May 20, 2018
.gitignore gitignore pytest cache May 9, 2018
.travis.yml macos needs language: generic Dec 12, 2018
Demo.ipynb Make it an IPython extension Mar 14, 2016
LICENSE Initial commit Mar 8, 2016
MANIFEST.in packaging Mar 8, 2016
README.md Link blogpost about redirecting stdout/stderr Jun 30, 2018
dev-requirements.txt
release.sh
setup.cfg init package Mar 8, 2016
setup.py setup.py improvements Jul 13, 2018
test.py sleep after flushing stdout Dec 12, 2018
wurlitzer.py poll timeout is in milliseconds Dec 12, 2018

README.md

Wurlitzer

Capture C-level stdout/stderr pipes in Python via os.dup2.

For more details on why this is needed, please read this blog post.

Install

pip install wurlitzer

Usage

Capture stdout/stderr in pipes:

from wurlitzer import pipes

with pipes() as (out, err):
    call_some_c_function()

stdout = out.read()

Capture stdout/stderr in StringIO:

from io import StringIO
from wurlitzer import pipes, STDOUT

out = StringIO()
with pipes(stdout=out, stderr=STDOUT):
    call_some_c_function()

stdout = out.getvalue()

Forward C-level stdout/stderr to Python sys.stdout/stderr, which may already be forwarded somewhere by the environment, e.g. IPython:

from wurlitzer import sys_pipes

with sys_pipes():
    call_some_c_function()

Or even simpler, enable it as an IPython extension:

%load_ext wurlitzer

To forward all C-level output to IPython during execution.

Acknowledgments

This package is based on stuff we learned with @takluyver and @karies while working on capturing output from the Cling Kernel for Jupyter.

Wurlitzer?!

Wurlitzer makes pipe organs. Get it? Pipes? Naming is hard.