This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author Zahari.Dim
Recipients Zahari.Dim
Date 2015-06-24.16:03:02
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
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

import sys
import os
from contextlib import contextmanager, redirect_stdout

def supress_stdout():
    devnull = open(os.devnull, 'wb')
        stdout_flieno = sys.stdout.fileno()
    except ValueError:
        redirect = False
        redirect = True
        devnull_fileno = devnull.fileno()
        saved_stdout_fd = os.dup(stdout_flieno)
        os.dup2(devnull_fileno, stdout_flieno)

    with redirect_stdout(devnull):
    if redirect:
        os.dup2(stdout_flieno, saved_stdout_fd)
Date User Action Args
2015-06-24 16:03:02Zahari.Dimsetrecipients: + Zahari.Dim
2015-06-24 16:03:02Zahari.Dimsetmessageid: <>
2015-06-24 16:03:02Zahari.Dimlinkissue24500 messages
2015-06-24 16:03:02Zahari.Dimcreate