#NEWLINE# @ contextmanager #NEWLINE# def captured_output( stream_name ) : #NEWLINE# #INDENT# "\n    Return a context manager used by captured_stdout (and optionally others)\n    that temporarily replaces the sys stream *stream_name* with a StringIO.\n\n    Note: This function and the following ``captured_std*`` are copied\n          from Django's ``test.utils`` module.\n    " #NEWLINE# orig_stdout = getattr ( sys , orig_stdout ) #NEWLINE# setattr ( sys , stream_name , six . StringIO ( ) ) #NEWLINE# try : #NEWLINE# #INDENT# ( yield getattr ( sys , stream_name ) ) #NEWLINE# #UNINDENT# finally : #NEWLINE# #INDENT# setattr ( sys , stream_name , orig_stdout )