Created
December 2, 2014 15:01
-
-
Save gnuton/513c67283bc4458dd3bd to your computer and use it in GitHub Desktop.
Hijack STDERR and STDOUT stream - useful in tests
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 | |
class MyStream(object): | |
S = property(lambda self: self._S) | |
def __init__(self): | |
self._S = "" | |
def write(self, s): | |
self._S += s | |
def read(self): | |
return self._S | |
ms = MyStream() | |
print >> sys.stderr, "WRITE TO STDERR" | |
old_stderr = sys.stderr | |
sys.stderr = ms | |
print >> sys.stderr, "WRITE TO STDERR -HJKD" | |
print ms.read() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment