@contextmanager
def catch_stderr():
 old_stderr = sys.stderr
 sys.stderr = rv = StringIO()
 try:
  (yield rv)
 finally:
  sys.stderr = old_stderr

@ contextmanager def catch_stderr ( ) : old_stderr = sys . stderr sys . stderr = rv = StringIO ( ) try : ( yield rv ) finally : sys . stderr = old_stderr
catch stderr in a stringio .