<s> """<STR_LIT>""" <EOL> from functools import wraps <EOL> import sys <EOL> from StringIO import StringIO <EOL> class CarbonCopy ( StringIO ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , buffer = '<STR_LIT>' , cc = None ) : <EOL> """<STR_LIT>""" <EOL> StringIO . __init__ ( self , buffer ) <EOL> if cc is None : <EOL> cc = [ ] <EOL> elif hasattr ( cc , '<STR_LIT>' ) : <EOL> cc = [ cc ] <EOL> self . cc = cc <EOL> def write ( self , s ) : <EOL> StringIO . write ( self , s ) <EOL> for writer in self . cc : <EOL> writer . write ( s ) <EOL> def mock_streams ( which ) : <EOL> """<STR_LIT>""" <EOL> both = ( which == '<STR_LIT>' ) <EOL> stdout = ( which == '<STR_LIT>' ) or both <EOL> stderr = ( which == '<STR_LIT>' ) or both <EOL> def mocked_streams_decorator ( func ) : <EOL> @ wraps ( func ) <EOL> def inner_wrapper ( * args , ** kwargs ) : <EOL> if both : <EOL> sys . stdall = StringIO ( ) <EOL> fake_stdout = CarbonCopy ( cc = sys . stdall ) <EOL> fake_stderr = CarbonCopy ( cc = sys . stdall ) <EOL> else : <EOL> fake_stdout , fake_stderr = StringIO ( ) , StringIO ( ) <EOL> if stdout : <EOL> my_stdout , sys . stdout = sys . stdout , fake_stdout <EOL> if stderr : <EOL> my_stderr , sys . stderr = sys . stderr , fake_stderr <EOL> try : <EOL> ret = func ( * args , ** kwargs ) <EOL> finally : <EOL> if stdout : <EOL> sys . stdout = my_stdout <EOL> if stderr : <EOL> sys . stderr = my_stderr <EOL> if both : <EOL> del sys . stdall <EOL> return inner_wrapper <EOL> return mocked_streams_decorator </s>
