On Tue, 26 Dec 2006 03:21:57 -0800, Luis Armendariz wrote: > On Tuesday, 26.12.06 at 21:28, Steven D'Aprano wrote: >> >> # WARNING: untested >> def run_without_stdout(*args, **kwargs): >> function = args[0] >> args = args[1:] >> savestdout = sys.stdout >> sys.stdout = cStringIO.StringIO() >> result = None >> try: >> result = function(*args, **kwargs) >> finally: >> # don't forget to restore stdout, or you >> # really will regret it... >> sys.stdout = savestdout >> return result >> > > There's no need for savestdout. There's a backup copy in sys.__stdout__
You shouldn't assume that sys.stdout is standard output when the function is called. It may already have been reassigned to something else. My code will restore sys.stdout to whatever state it was in before the function ran. -- Steven. -- http://mail.python.org/mailman/listinfo/python-list