Skip to content

Instantly share code, notes, and snippets.

@gnuton
Created December 2, 2014 15:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gnuton/513c67283bc4458dd3bd to your computer and use it in GitHub Desktop.
Save gnuton/513c67283bc4458dd3bd to your computer and use it in GitHub Desktop.
Hijack STDERR and STDOUT stream - useful in tests
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