@contextmanager
def silence_output(out=None, err=None):

    if out is None:
        out = open(os.devnull, 'w')
    if err is None:
        err = open(os.devnull, 'w')

    _old_stderr = sys.stderr
    _old_stderr.flush()

    _old_stdout = sys.stdout
    _old_stdout.flush()

    try:
        sys.stdout = out
        sys.stderr = err
        yield
    finally:
        sys.stdout = _old_stdout
        sys.stderr = _old_stderr
Re-direct the stderr and stdout streams while in the block.
dataset/ETHPy150Open enthought/pyface/pyface/ui/qt4/util/testing.py/silence_output