#NEWLINE# @ contextlib . contextmanager #NEWLINE# def captured_output( stream_name ) : #NEWLINE# #INDENT# 'Return a context manager used by captured_stdout/stdin/stderr\n    that temporarily replaces the sys stream *stream_name* with a StringIO.' #NEWLINE# orig_stdout = getattr ( sys , orig_stdout ) #NEWLINE# setattr ( sys , stream_name , utils . StringIO ( ) ) #NEWLINE# try : #NEWLINE# #INDENT# ( yield getattr ( sys , stream_name ) ) #NEWLINE# #UNINDENT# finally : #NEWLINE# #INDENT# setattr ( sys , stream_name , orig_stdout )