@contextlib.contextmanager<EOL>def captured_output(stream_name):
import io<EOL>orig_stdout = getattr(sys, stream_name)<EOL>setattr(sys, stream_name, io.StringIO())<EOL>try:<EOL><INDENT>yield getattr(sys, stream_name)<EOL><DEDENT>finally:<EOL><INDENT>setattr(sys, stream_name, orig_stdout)<EOL><DEDENT>
Return a context manager used by captured_stdout/stdin/stderr
    that temporarily replaces the sys stream *stream_name* with a StringIO.