def mock_streams(which):
 both = (which == 'both')
 stdout = ((which == 'stdout') or both)
 stderr = ((which == 'stderr') or both)
 def mocked_streams_decorator(func):
  @wraps(func)
  def inner_wrapper(*args, **kwargs):
   if both:
    sys.stdall = StringIO()
    fake_stdout = CarbonCopy(cc=sys.stdall)
    fake_stderr = CarbonCopy(cc=sys.stdall)
   else:
    (fake_stdout, fake_stderr) = (StringIO(), StringIO())
   if stdout:
    (my_stdout, sys.stdout) = (sys.stdout, fake_stdout)
   if stderr:
    (my_stderr, sys.stderr) = (sys.stderr, fake_stderr)
   try:
    func(*args, **kwargs)
   finally:
    if stdout:
     sys.stdout = my_stdout
    if stderr:
     sys.stderr = my_stderr
    if both:
     del sys.stdall
  return inner_wrapper
 return mocked_streams_decorator

def mock_streams ( which ) : both = ( which == 'both' ) stdout = ( ( which == 'stdout' ) or both ) stderr = ( ( which == 'stderr' ) or both ) def mocked_streams_decorator ( func ) : @ wraps ( func ) def inner_wrapper ( * args , ** kwargs ) : if both : sys . stdall = StringIO ( ) fake_stdout = CarbonCopy ( cc = sys . stdall ) fake_stderr = CarbonCopy ( cc = sys . stdall ) else : ( fake_stdout , fake_stderr ) = ( StringIO ( ) , StringIO ( ) ) if stdout : ( my_stdout , sys . stdout ) = ( sys . stdout , fake_stdout ) if stderr : ( my_stderr , sys . stderr ) = ( sys . stderr , fake_stderr ) try : func ( * args , ** kwargs ) finally : if stdout : sys . stdout = my_stdout if stderr : sys . stderr = my_stderr if both : del sys . stdall return inner_wrapper return mocked_streams_decorator
replaces a stream with a stringio during the test .