@contextmanager<EOL>def pipes(stdout=PIPE, stderr=PIPE, encoding=_default_encoding):
stdout_pipe = stderr_pipe = False<EOL>if stdout == PIPE:<EOL><INDENT>stdout_r, stdout_w = os.pipe()<EOL>stdout_w = os.fdopen(stdout_w, '<STR_LIT:wb>')<EOL>if encoding:<EOL><INDENT>stdout_r = io.open(stdout_r, '<STR_LIT:r>', encoding=encoding)<EOL><DEDENT>else:<EOL><INDENT>stdout_r = os.fdopen(stdout_r, '<STR_LIT:rb>')<EOL><DEDENT>stdout_pipe = True<EOL><DEDENT>else:<EOL><INDENT>stdout_r = stdout_w = stdout<EOL><DEDENT>if stderr == STDOUT:<EOL><INDENT>stderr_r = None<EOL>stderr_w = stdout_w<EOL><DEDENT>elif stderr == PIPE:<EOL><INDENT>stderr_r, stderr_w = os.pipe()<EOL>stderr_w = os.fdopen(stderr_w, '<STR_LIT:wb>')<EOL>if encoding:<EOL><INDENT>stderr_r = io.open(stderr_r, '<STR_LIT:r>', encoding=encoding)<EOL><DEDENT>else:<EOL><INDENT>stderr_r = os.fdopen(stderr_r, '<STR_LIT:rb>')<EOL><DEDENT>stderr_pipe = True<EOL><DEDENT>else:<EOL><INDENT>stderr_r = stderr_w = stderr<EOL><DEDENT>if stdout_pipe or stderr_pipe:<EOL><INDENT>capture_encoding = None<EOL><DEDENT>else:<EOL><INDENT>capture_encoding = encoding<EOL><DEDENT>w = Wurlitzer(stdout=stdout_w, stderr=stderr_w, encoding=capture_encoding)<EOL>try:<EOL><INDENT>with w:<EOL><INDENT>yield stdout_r, stderr_r<EOL><DEDENT><DEDENT>finally:<EOL><INDENT>if stdout_pipe:<EOL><INDENT>stdout_w.close()<EOL><DEDENT>if stderr_pipe:<EOL><INDENT>stderr_w.close()<EOL><DEDENT><DEDENT>
Capture C-level stdout/stderr in a context manager.

    The return value for the context manager is (stdout, stderr).

    Examples
    --------

    >>> with capture() as (stdout, stderr):
    ...     printf("C-level stdout")
    ... output = stdout.read()