def capture_output(data=None):
"""
with capture_output as (stdout, stderr):
some_action()
print stdout.getvalue(), stderr.getvalue()
"""
in_ = BytesIO(data or b"")
err = BytesIO()
out = BytesIO()
old_in = sys.stdin
old_err = sys.stderr
old_out = sys.stdout
sys.stdin = in_
sys.stderr = err
sys.stdout = out
try:
yield (out, err)
finally:
sys.stdin = old_in
sys.stderr = old_err
sys.stdout = old_out
评论列表
文章目录