wurlitzer


Namewurlitzer JSON
Version 3.1.0 PyPI version JSON
download
home_pagehttps://github.com/minrk/wurlitzer
SummaryCapture C-level output in context managers
upload_time2024-04-29 10:39:46
maintainerNone
docs_urlNone
authorMin RK
requires_python>=3.5
licenseMIT
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # 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](https://eli.thegreenplace.net/2015/redirecting-all-kinds-of-stdout-in-python/).

## Install

    pip install wurlitzer

## Usage

Capture stdout/stderr in pipes:

```python
from wurlitzer import pipes

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

stdout = out.read()
```

Capture stdout/stderr in StringIO:

```python
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:

```python
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.

```python
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):

```python
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](https://github.com/root-mirror/cling/tree/master/tools/Jupyter/kernel) for Jupyter.

## Wurlitzer?!

[Wurlitzer](https://en.wikipedia.org/wiki/Wurlitzer) makes pipe organs. Get it? Pipes? Naming is hard.

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/minrk/wurlitzer",
    "name": "wurlitzer",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.5",
    "maintainer_email": null,
    "keywords": null,
    "author": "Min RK",
    "author_email": "benjaminrk@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/7b/4a/8fdefccb188c37992d8c7c5922b89aaa6cbf7d738a8798f92109ed7d4212/wurlitzer-3.1.0.tar.gz",
    "platform": null,
    "description": "# Wurlitzer\n\nCapture C-level stdout/stderr pipes in Python via `os.dup2`.\n\nFor more details on why this is needed, please read [this blog post](https://eli.thegreenplace.net/2015/redirecting-all-kinds-of-stdout-in-python/).\n\n## Install\n\n    pip install wurlitzer\n\n## Usage\n\nCapture stdout/stderr in pipes:\n\n```python\nfrom wurlitzer import pipes\n\nwith pipes() as (out, err):\n    call_some_c_function()\n\nstdout = out.read()\n```\n\nCapture stdout/stderr in StringIO:\n\n```python\nfrom io import StringIO\nfrom wurlitzer import pipes, STDOUT\n\nout = StringIO()\nwith pipes(stdout=out, stderr=STDOUT):\n    call_some_c_function()\n\nstdout = out.getvalue()\n```\n\nForward C-level stdout/stderr to Python sys.stdout/stderr,\nwhich may already be forwarded somewhere by the environment, e.g. IPython:\n\n```python\nfrom wurlitzer import sys_pipes\n\nwith sys_pipes():\n    call_some_c_function()\n```\n\nForward C-level output to Python Logger objects (new in 3.1).\nEach line of output will be a log message.\n\n```python\nfrom wurlitzer import pipes, STDOUT\nimport logging\n\nlogger = logging.getLogger(\"my.log\")\nlogger.setLevel(logging.INFO)\nlogger.addHandler(logging.FileHandler(\"mycode.log\"))\n\nwith pipes(logger, stderr=STDOUT):\n    call_some_c_function()\n```\n\nForward C-level output to a file (avoids GIL issues with a background thread, new in 3.1):\n\n```python\nfrom wurlitzer import pipes, STDOUT\n\nwith open(\"log.txt\", \"ab\") as f, pipes(f, stderr=STDOUT):\n    blocking_gil_holding_function()\n```\n\nOr even simpler, enable it as an IPython extension:\n\n```\n%load_ext wurlitzer\n```\n\nTo forward all C-level output to IPython (e.g. Jupyter cell output) during execution.\n\n## Acknowledgments\n\nThis package is based on stuff we learned with @takluyver and @karies while working on capturing output from the [Cling Kernel](https://github.com/root-mirror/cling/tree/master/tools/Jupyter/kernel) for Jupyter.\n\n## Wurlitzer?!\n\n[Wurlitzer](https://en.wikipedia.org/wiki/Wurlitzer) makes pipe organs. Get it? Pipes? Naming is hard.\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Capture C-level output in context managers",
    "version": "3.1.0",
    "project_urls": {
        "Homepage": "https://github.com/minrk/wurlitzer"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "512333f2ed4247e30770d964e3952bf4cbe13d22846eca940557447e0a4a2125",
                "md5": "8a0317b735a85e053b6f4fca51c43d0c",
                "sha256": "a20994e53681fea8fb9d16dc05ff5f9f9701f09723d894456a6d6b29cfb3188d"
            },
            "downloads": -1,
            "filename": "wurlitzer-3.1.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "8a0317b735a85e053b6f4fca51c43d0c",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.5",
            "size": 8363,
            "upload_time": "2024-04-29T10:39:45",
            "upload_time_iso_8601": "2024-04-29T10:39:45.190879Z",
            "url": "https://files.pythonhosted.org/packages/51/23/33f2ed4247e30770d964e3952bf4cbe13d22846eca940557447e0a4a2125/wurlitzer-3.1.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "7b4a8fdefccb188c37992d8c7c5922b89aaa6cbf7d738a8798f92109ed7d4212",
                "md5": "41a7d99f577cbd11aef6820dda8e7202",
                "sha256": "b31d3b18ab6b8394b3adb8b21841ad6d4b6bb195b9b642953872dbbab4c29b2a"
            },
            "downloads": -1,
            "filename": "wurlitzer-3.1.0.tar.gz",
            "has_sig": false,
            "md5_digest": "41a7d99f577cbd11aef6820dda8e7202",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.5",
            "size": 11571,
            "upload_time": "2024-04-29T10:39:46",
            "upload_time_iso_8601": "2024-04-29T10:39:46.407788Z",
            "url": "https://files.pythonhosted.org/packages/7b/4a/8fdefccb188c37992d8c7c5922b89aaa6cbf7d738a8798f92109ed7d4212/wurlitzer-3.1.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-04-29 10:39:46",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "minrk",
    "github_project": "wurlitzer",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "wurlitzer"
}
        
Elapsed time: 0.23656s