Created
June 18, 2017 21:59
-
-
Save claytantor/b4c7473bd76b3b273a77152c3e0000c9 to your computer and use it in GitHub Desktop.
capturing both stderr and std to a single list of strings.
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
from cStringIO import StringIO | |
class Capturing(list): | |
def __enter__(self): | |
self._stdout = sys.stdout | |
self._stderr = sys.stderr | |
sys.stdout = self._stringio = StringIO() | |
sys.stderr = self._stringio | |
return self | |
def __exit__(self, *args): | |
self.extend(self._stringio.getvalue().splitlines()) | |
del self._stringio # free up some memory | |
sys.stdout = self._stdout | |
sys.stderr = self._stderr |
Author
claytantor
commented
Jun 19, 2017
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment