tests: make Std{err,out}Buffer context managers stackable
Allow use StdoutBuffer context manager inside another similar context manager. This allows running the tests by a test runner which collect the output using similar trick.
This commit is contained in:
parent
54dcec2cf5
commit
cdb9205eef
@ -25,31 +25,35 @@ import sys
|
|||||||
|
|
||||||
class StdoutBuffer(object):
|
class StdoutBuffer(object):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
self.orig_stdout = None
|
||||||
if sys.version_info[0] >= 3:
|
if sys.version_info[0] >= 3:
|
||||||
self.stdout = io.StringIO()
|
self.stdout = io.StringIO()
|
||||||
else:
|
else:
|
||||||
self.stdout = io.BytesIO()
|
self.stdout = io.BytesIO()
|
||||||
|
|
||||||
def __enter__(self):
|
def __enter__(self):
|
||||||
|
self.orig_stdout = sys.stdout
|
||||||
sys.stdout = self.stdout
|
sys.stdout = self.stdout
|
||||||
return self.stdout
|
return self.stdout
|
||||||
|
|
||||||
def __exit__(self, exc_type, exc_val, exc_tb):
|
def __exit__(self, exc_type, exc_val, exc_tb):
|
||||||
sys.stdout = sys.__stdout__
|
sys.stdout = self.orig_stdout
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
class StderrBuffer(object):
|
class StderrBuffer(object):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
self.orig_stderr = None
|
||||||
if sys.version_info[0] >= 3:
|
if sys.version_info[0] >= 3:
|
||||||
self.stderr = io.StringIO()
|
self.stderr = io.StringIO()
|
||||||
else:
|
else:
|
||||||
self.stderr = io.BytesIO()
|
self.stderr = io.BytesIO()
|
||||||
|
|
||||||
def __enter__(self):
|
def __enter__(self):
|
||||||
|
self.orig_stderr = sys.stderr
|
||||||
sys.stderr = self.stderr
|
sys.stderr = self.stderr
|
||||||
return self.stderr
|
return self.stderr
|
||||||
|
|
||||||
def __exit__(self, exc_type, exc_val, exc_tb):
|
def __exit__(self, exc_type, exc_val, exc_tb):
|
||||||
sys.stderr = sys.__stderr__
|
sys.stderr = self.orig_stderr
|
||||||
return False
|
return False
|
||||||
|
Loading…
Reference in New Issue
Block a user