Created
September 26, 2013 10:27
-
-
Save chengui/6712381 to your computer and use it in GitHub Desktop.
redirect std stream with decorator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
def redirect_stderr_stdout(stderr=sys.stderr, stdout=sys.stdout): | |
def wrap(f): | |
def newf(*args, **kwargs): | |
old_stderr, old_stdout = sys.stderr, sys.stdout | |
sys.stderr = stderr | |
sys.stdout = stdout | |
try: | |
return f(*args, **kwargs) | |
finally: | |
sys.stderr, sys.stdout = old_stderr, old_stdout | |
return newf | |
return wrap | |
@redirect_stderr_stdout | |
def fun(): | |
print "hello world" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment