wurlitzer


Namewurlitzer JSON
Version 3.1.1 PyPI version JSON
download
home_pagehttps://github.com/minrk/wurlitzer
SummaryCapture C-level output in context managers
upload_time2024-06-12 10:27:30
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 both stdout and stderr in a single StringIO object:

```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/33/90/623f99c55c7d0727a58eb2b7dfb65cb406c561a5c2e9a95b0d6a450c473d/wurlitzer-3.1.1.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 both stdout and stderr in a single StringIO object:\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.1",
    "project_urls": {
        "Homepage": "https://github.com/minrk/wurlitzer"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "9a2493ce54550a9dd3fd996ed477f00221f215bf6da3580397fbc138d6036e2e",
                "md5": "efdf97d4009db5900c1a178297ba4038",
                "sha256": "0b2749c2cde3ef640bf314a9f94b24d929fe1ca476974719a6909dfc568c3aac"
            },
            "downloads": -1,
            "filename": "wurlitzer-3.1.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "efdf97d4009db5900c1a178297ba4038",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.5",
            "size": 8590,
            "upload_time": "2024-06-12T10:27:28",
            "upload_time_iso_8601": "2024-06-12T10:27:28.787923Z",
            "url": "https://files.pythonhosted.org/packages/9a/24/93ce54550a9dd3fd996ed477f00221f215bf6da3580397fbc138d6036e2e/wurlitzer-3.1.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "3390623f99c55c7d0727a58eb2b7dfb65cb406c561a5c2e9a95b0d6a450c473d",
                "md5": "da60ccfa9c6f5155fe8ec66dc8f8409a",
                "sha256": "bfb9144ab9f02487d802b9ff89dbd3fa382d08f73e12db8adc4c2fb00cd39bd9"
            },
            "downloads": -1,
            "filename": "wurlitzer-3.1.1.tar.gz",
            "has_sig": false,
            "md5_digest": "da60ccfa9c6f5155fe8ec66dc8f8409a",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.5",
            "size": 11867,
            "upload_time": "2024-06-12T10:27:30",
            "upload_time_iso_8601": "2024-06-12T10:27:30.089863Z",
            "url": "https://files.pythonhosted.org/packages/33/90/623f99c55c7d0727a58eb2b7dfb65cb406c561a5c2e9a95b0d6a450c473d/wurlitzer-3.1.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-06-12 10:27:30",
    "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: 1.23728s