Skip to main content

Capture C-level output in context managers

Project description

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()

Forward C-level output to Python Logger objects (new in 3.1). Each line of output will be a log message.

from wurlitzer import pipes, STDOUT
import logging

logger = logging.getLogger("my.log")
logger.setLevel(logging.INFO)
logger.addHandler(logging.FileHandler("mycode.log"))

with pipes(logger, stderr=STDOUT):
    call_some_c_function()

Forward C-level output to a file (avoids GIL issues with a background thread, new in 3.1):

from wurlitzer import pipes, STDOUT

with open("log.txt", "ab") as f, pipes(f, stderr=STDOUT):
    blocking_gil_holding_function()

Or even simpler, enable it as an IPython extension:

%load_ext wurlitzer

To forward all C-level output to IPython (e.g. Jupyter cell output) 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.

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

wurlitzer-3.1.0.tar.gz (11.6 kB view details)

Uploaded Source

Built Distribution

wurlitzer-3.1.0-py3-none-any.whl (8.4 kB view details)

Uploaded Python 3

File details

Details for the file wurlitzer-3.1.0.tar.gz.

File metadata

  • Download URL: wurlitzer-3.1.0.tar.gz
  • Upload date:
  • Size: 11.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.8.19

File hashes

Hashes for wurlitzer-3.1.0.tar.gz
Algorithm Hash digest
SHA256 b31d3b18ab6b8394b3adb8b21841ad6d4b6bb195b9b642953872dbbab4c29b2a
MD5 41a7d99f577cbd11aef6820dda8e7202
BLAKE2b-256 7b4a8fdefccb188c37992d8c7c5922b89aaa6cbf7d738a8798f92109ed7d4212

See more details on using hashes here.

File details

Details for the file wurlitzer-3.1.0-py3-none-any.whl.

File metadata

  • Download URL: wurlitzer-3.1.0-py3-none-any.whl
  • Upload date:
  • Size: 8.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.8.19

File hashes

Hashes for wurlitzer-3.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a20994e53681fea8fb9d16dc05ff5f9f9701f09723d894456a6d6b29cfb3188d
MD5 8a0317b735a85e053b6f4fca51c43d0c
BLAKE2b-256 512333f2ed4247e30770d964e3952bf4cbe13d22846eca940557447e0a4a2125

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