@contextmanager
def swap_stdout():
 old_stdout = sys.stdout
 if PY3:
  fake_stream = StringIO()
 else:
  fake_stream = BytesIO()
 sys.stdout = fake_stream
 (yield fake_stream)
 sys.stdout = old_stdout

@ contextmanager def swap_stdout ( ) : old_stdout = sys . stdout if PY3 : fake_stream = StringIO ( ) else : fake_stream = BytesIO ( ) sys . stdout = fake_stream ( yield fake_stream ) sys . stdout = old_stdout
context manager that temporarily replaces stdout for tests that need to verify output .