Allow RedirectStdStreams to behave correctly using with. Allow flushing streams.

This commit is contained in:
Pedro Algarvio 2012-12-01 22:59:43 +00:00
parent c4e485a8bf
commit f51f35c148

View File

@ -74,6 +74,7 @@ class RedirectStdStreams(object):
def __enter__(self):
self.redirect()
return self
def __exit__(self, exc_type, exc_value, traceback):
self.unredirect()
@ -90,14 +91,33 @@ class RedirectStdStreams(object):
def unredirect(self):
if not self.__redirected:
return
try:
self.__stdout.flush()
self.__stdout.close()
except ValueError:
# already closed?
pass
try:
self.__stderr.flush()
self.__stderr.close()
except ValueError:
# already closed?
pass
sys.stdout = self.old_stdout
sys.stderr = self.old_stderr
def flush(self):
if self.__redirected:
try:
self.__stdout.flush()
except:
pass
try:
self.__stderr.flush()
except:
pass
class TestsLoggingHandler(object):
'''