classification
Title: provide context manager to redirect C output
Type: enhancement Stage: needs patch
Components: Library (Lib) Versions: Python 3.6
process
Status: languishing Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Zahari.Dim, martin.panter, r.david.murray
Priority: normal Keywords:

Created on 2015-06-24 16:03 by Zahari.Dim, last changed 2016-06-02 16:01 by Zahari.Dim.

Messages (5)
msg245760 - (view) Author: Zahari Dim (Zahari.Dim) Date: 2015-06-24 16:03
It is common to have an inflexible C wrapper with lots of undesired output. However it is not so trivial to supress (or redirect) that output from Python in a selective way. contextlib.redirect_stdout doesn't help, since it only changes sys.sdout, without touching the actual file descriptor. The following worked for my use case, which I adapted from here http://eli.thegreenplace.net/2015/redirecting-all-kinds-of-stdout-in-python/:

import sys
import os
from contextlib import contextmanager, redirect_stdout

@contextmanager
def supress_stdout():
    devnull = open(os.devnull, 'wb')
    try:
        stdout_flieno = sys.stdout.fileno()
    except ValueError:
        redirect = False
    else:
        redirect = True
        sys.stdout.flush()
        #sys.stdout.close()
        devnull_fileno = devnull.fileno()
        saved_stdout_fd = os.dup(stdout_flieno)
        os.dup2(devnull_fileno, stdout_flieno)

    with redirect_stdout(devnull):
        yield
    if redirect:
        os.dup2(stdout_flieno, saved_stdout_fd)
msg245766 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2015-06-24 17:13
This was discussed in the original redirect_stdout issue (issue 15805).  It was suggested then that C level redirection was a separate issue, and should probably be something in the subprocess module.
msg245842 - (view) Author: Zahari Dim (Zahari.Dim) Date: 2015-06-26 09:51
Well, the simple minded example I posted has so many bugs (many of which I don't understand, for example why it destroys the stdout of an interpreter permanently) that I really think this feature is necessary.
msg265095 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2016-05-07 22:04
Is it really common to have a C wrapper with undesirable output? I suspect there is not much demand for this feature. Maybe this would be better outside of Python’s standard library.
msg266887 - (view) Author: Zahari Dim (Zahari.Dim) Date: 2016-06-02 16:01
Considering Python is used often to interact with lower level
languages, it seems interesting to have the ability to control the
"real" standard output and error that those languages use. Note that
redirecting to /dev/null is only one possible application of this
feature. Others would be for example linking the stout to the logging
module.

Specifically regarding redirecting to /dev/null, in my experience this
would be fairly useful In scientific software where low level code
tends to be used on scientific merits rather than on how much control
it has over verbosity.

On Sun, May 8, 2016 at 12:04 AM, Martin Panter <report@bugs.python.org> wrote:
>
> Martin Panter added the comment:
>
> Is it really common to have a C wrapper with undesirable output? I suspect there is not much demand for this feature. Maybe this would be better outside of Python’s standard library.
>
> ----------
> nosy: +martin.panter
> status: open -> languishing
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <http://bugs.python.org/issue24500>
> _______________________________________
History
Date User Action Args
2016-06-02 16:01:09Zahari.Dimsetmessages: + msg266887
2016-05-07 22:04:31martin.pantersetstatus: open -> languishing
nosy: + martin.panter
messages: + msg265095

2015-06-26 09:51:11Zahari.Dimsetmessages: + msg245842
2015-06-24 17:13:15r.david.murraysettitle: contextlib.redirect_stdout should redirect C output -> provide context manager to redirect C output
components: - Extension Modules

nosy: + r.david.murray
versions: + Python 3.6, - Python 3.4
messages: + msg245766
stage: needs patch
2015-06-24 16:09:46Zahari.Dimsettitle: xontextlib.redirect_stdout should redirect C output -> contextlib.redirect_stdout should redirect C output
2015-06-24 16:03:02Zahari.Dimcreate