def mock_stdout_or_stderr():
 if PY2:
  return StringIO()
 buf = BytesIO()
 writer = codecs.getwriter('utf_8')(buf)
 writer.buffer = buf
 writer.getvalue = (lambda : buf.getvalue())
 return writer

def mock_stdout_or_stderr ( ) : if PY2 : return StringIO ( ) buf = BytesIO ( ) writer = codecs . getwriter ( 'utf_8' ) ( buf ) writer . buffer = buf writer . getvalue = ( lambda : buf . getvalue ( ) ) return writer
use this to make a mock stdout/stderr (e .