test_iostream.py revision 12391
112855Sgabeblack@google.comfrom pybind11_tests import iostream as m 212855Sgabeblack@google.comimport sys 312855Sgabeblack@google.com 412855Sgabeblack@google.comfrom contextlib import contextmanager 512855Sgabeblack@google.com 612855Sgabeblack@google.comtry: 712855Sgabeblack@google.com # Python 3 812855Sgabeblack@google.com from io import StringIO 912855Sgabeblack@google.comexcept ImportError: 1012855Sgabeblack@google.com # Python 2 1112855Sgabeblack@google.com try: 1212855Sgabeblack@google.com from cStringIO import StringIO 1312855Sgabeblack@google.com except ImportError: 1412855Sgabeblack@google.com from StringIO import StringIO 1512855Sgabeblack@google.com 1612855Sgabeblack@google.comtry: 1712855Sgabeblack@google.com # Python 3.4 1812855Sgabeblack@google.com from contextlib import redirect_stdout 1912855Sgabeblack@google.comexcept ImportError: 2012855Sgabeblack@google.com @contextmanager 2112855Sgabeblack@google.com def redirect_stdout(target): 2212855Sgabeblack@google.com original = sys.stdout 2312855Sgabeblack@google.com sys.stdout = target 2412855Sgabeblack@google.com yield 2512855Sgabeblack@google.com sys.stdout = original 2612855Sgabeblack@google.com 2712855Sgabeblack@google.comtry: 2812855Sgabeblack@google.com # Python 3.5 2912855Sgabeblack@google.com from contextlib import redirect_stderr 3012855Sgabeblack@google.comexcept ImportError: 3112855Sgabeblack@google.com @contextmanager 3212855Sgabeblack@google.com def redirect_stderr(target): 3312855Sgabeblack@google.com original = sys.stderr 3412855Sgabeblack@google.com sys.stderr = target 3512855Sgabeblack@google.com yield 3612855Sgabeblack@google.com sys.stderr = original 3712855Sgabeblack@google.com 3812855Sgabeblack@google.com 3912855Sgabeblack@google.comdef test_captured(capsys): 4012855Sgabeblack@google.com msg = "I've been redirected to Python, I hope!" 4112855Sgabeblack@google.com m.captured_output(msg) 4212855Sgabeblack@google.com stdout, stderr = capsys.readouterr() 4312855Sgabeblack@google.com assert stdout == msg 4412855Sgabeblack@google.com assert stderr == '' 4512855Sgabeblack@google.com 4612855Sgabeblack@google.com m.captured_output_default(msg) 4712855Sgabeblack@google.com stdout, stderr = capsys.readouterr() 4812855Sgabeblack@google.com assert stdout == msg 4912855Sgabeblack@google.com assert stderr == '' 5012855Sgabeblack@google.com 5112855Sgabeblack@google.com m.captured_err(msg) 5212855Sgabeblack@google.com stdout, stderr = capsys.readouterr() 5312855Sgabeblack@google.com assert stdout == '' 5412855Sgabeblack@google.com assert stderr == msg 5512855Sgabeblack@google.com 5612855Sgabeblack@google.com 5712855Sgabeblack@google.comdef test_guard_capture(capsys): 5812855Sgabeblack@google.com msg = "I've been redirected to Python, I hope!" 5912855Sgabeblack@google.com m.guard_output(msg) 6012855Sgabeblack@google.com stdout, stderr = capsys.readouterr() 6112855Sgabeblack@google.com assert stdout == msg 6212855Sgabeblack@google.com assert stderr == '' 6312855Sgabeblack@google.com 6412855Sgabeblack@google.com 6512855Sgabeblack@google.comdef test_series_captured(capture): 6612855Sgabeblack@google.com with capture: 6712855Sgabeblack@google.com m.captured_output("a") 6812855Sgabeblack@google.com m.captured_output("b") 6912855Sgabeblack@google.com assert capture == "ab" 7012855Sgabeblack@google.com 7112855Sgabeblack@google.com 7212855Sgabeblack@google.comdef test_flush(capfd): 7312855Sgabeblack@google.com msg = "(not flushed)" 7412855Sgabeblack@google.com msg2 = "(flushed)" 7512855Sgabeblack@google.com 7612855Sgabeblack@google.com with m.ostream_redirect(): 7712855Sgabeblack@google.com m.noisy_function(msg, flush=False) 7812855Sgabeblack@google.com stdout, stderr = capfd.readouterr() 7912855Sgabeblack@google.com assert stdout == '' 8012855Sgabeblack@google.com 8112855Sgabeblack@google.com m.noisy_function(msg2, flush=True) 8212855Sgabeblack@google.com stdout, stderr = capfd.readouterr() 8312855Sgabeblack@google.com assert stdout == msg + msg2 8412855Sgabeblack@google.com 8512855Sgabeblack@google.com m.noisy_function(msg, flush=False) 8612855Sgabeblack@google.com 8712855Sgabeblack@google.com stdout, stderr = capfd.readouterr() 8812855Sgabeblack@google.com assert stdout == msg 8912855Sgabeblack@google.com 9012855Sgabeblack@google.com 9112855Sgabeblack@google.comdef test_not_captured(capfd): 9212855Sgabeblack@google.com msg = "Something that should not show up in log" 9312855Sgabeblack@google.com stream = StringIO() 9412855Sgabeblack@google.com with redirect_stdout(stream): 9512855Sgabeblack@google.com m.raw_output(msg) 9612855Sgabeblack@google.com stdout, stderr = capfd.readouterr() 9712855Sgabeblack@google.com assert stdout == msg 9812855Sgabeblack@google.com assert stderr == '' 9912855Sgabeblack@google.com assert stream.getvalue() == '' 10012855Sgabeblack@google.com 10112855Sgabeblack@google.com stream = StringIO() 10212855Sgabeblack@google.com with redirect_stdout(stream): 10312855Sgabeblack@google.com m.captured_output(msg) 10412855Sgabeblack@google.com stdout, stderr = capfd.readouterr() 10512855Sgabeblack@google.com assert stdout == '' 10612855Sgabeblack@google.com assert stderr == '' 10712855Sgabeblack@google.com assert stream.getvalue() == msg 10812855Sgabeblack@google.com 10912855Sgabeblack@google.com 11012855Sgabeblack@google.comdef test_err(capfd): 11112855Sgabeblack@google.com msg = "Something that should not show up in log" 11212855Sgabeblack@google.com stream = StringIO() 11312855Sgabeblack@google.com with redirect_stderr(stream): 11412855Sgabeblack@google.com m.raw_err(msg) 11512855Sgabeblack@google.com stdout, stderr = capfd.readouterr() 11612855Sgabeblack@google.com assert stdout == '' 11712855Sgabeblack@google.com assert stderr == msg 11812855Sgabeblack@google.com assert stream.getvalue() == '' 11912855Sgabeblack@google.com 12012855Sgabeblack@google.com stream = StringIO() 12112855Sgabeblack@google.com with redirect_stderr(stream): 12212855Sgabeblack@google.com m.captured_err(msg) 12312855Sgabeblack@google.com stdout, stderr = capfd.readouterr() 12412855Sgabeblack@google.com assert stdout == '' 12512855Sgabeblack@google.com assert stderr == '' 12612855Sgabeblack@google.com assert stream.getvalue() == msg 12712855Sgabeblack@google.com 12812855Sgabeblack@google.com 12912855Sgabeblack@google.comdef test_multi_captured(capfd): 13012855Sgabeblack@google.com stream = StringIO() 13112855Sgabeblack@google.com with redirect_stdout(stream): 13212855Sgabeblack@google.com m.captured_output("a") 13312855Sgabeblack@google.com m.raw_output("b") 13412855Sgabeblack@google.com m.captured_output("c") 13512855Sgabeblack@google.com m.raw_output("d") 13612855Sgabeblack@google.com stdout, stderr = capfd.readouterr() 13712855Sgabeblack@google.com assert stdout == 'bd' 13812855Sgabeblack@google.com assert stream.getvalue() == 'ac' 13912855Sgabeblack@google.com 14012855Sgabeblack@google.com 14112855Sgabeblack@google.comdef test_dual(capsys): 14212855Sgabeblack@google.com m.captured_dual("a", "b") 14312855Sgabeblack@google.com stdout, stderr = capsys.readouterr() 14412855Sgabeblack@google.com assert stdout == "a" 14512855Sgabeblack@google.com assert stderr == "b" 14612855Sgabeblack@google.com 14712855Sgabeblack@google.com 14812855Sgabeblack@google.comdef test_redirect(capfd): 14912855Sgabeblack@google.com msg = "Should not be in log!" 15012855Sgabeblack@google.com stream = StringIO() 15112855Sgabeblack@google.com with redirect_stdout(stream): 15212855Sgabeblack@google.com m.raw_output(msg) 15312855Sgabeblack@google.com stdout, stderr = capfd.readouterr() 15412855Sgabeblack@google.com assert stdout == msg 15512855Sgabeblack@google.com assert stream.getvalue() == '' 15612855Sgabeblack@google.com 15712855Sgabeblack@google.com stream = StringIO() 15812855Sgabeblack@google.com with redirect_stdout(stream): 15912855Sgabeblack@google.com with m.ostream_redirect(): 16012855Sgabeblack@google.com m.raw_output(msg) 16112855Sgabeblack@google.com stdout, stderr = capfd.readouterr() 16212855Sgabeblack@google.com assert stdout == '' 16312855Sgabeblack@google.com assert stream.getvalue() == msg 16412855Sgabeblack@google.com 16512855Sgabeblack@google.com stream = StringIO() 16612855Sgabeblack@google.com with redirect_stdout(stream): 16712855Sgabeblack@google.com m.raw_output(msg) 16812855Sgabeblack@google.com stdout, stderr = capfd.readouterr() 16912855Sgabeblack@google.com assert stdout == msg 17012855Sgabeblack@google.com assert stream.getvalue() == '' 17112855Sgabeblack@google.com 17212855Sgabeblack@google.com 17312855Sgabeblack@google.comdef test_redirect_err(capfd): 17412855Sgabeblack@google.com msg = "StdOut" 17512855Sgabeblack@google.com msg2 = "StdErr" 17612855Sgabeblack@google.com 17712855Sgabeblack@google.com stream = StringIO() 17812855Sgabeblack@google.com with redirect_stderr(stream): 17912855Sgabeblack@google.com with m.ostream_redirect(stdout=False): 18012855Sgabeblack@google.com m.raw_output(msg) 18112855Sgabeblack@google.com m.raw_err(msg2) 18212855Sgabeblack@google.com stdout, stderr = capfd.readouterr() 18312855Sgabeblack@google.com assert stdout == msg 18412855Sgabeblack@google.com assert stderr == '' 18512855Sgabeblack@google.com assert stream.getvalue() == msg2 18612855Sgabeblack@google.com 18712855Sgabeblack@google.com 18812855Sgabeblack@google.comdef test_redirect_both(capfd): 18912855Sgabeblack@google.com msg = "StdOut" 19012855Sgabeblack@google.com msg2 = "StdErr" 19112855Sgabeblack@google.com 19212855Sgabeblack@google.com stream = StringIO() 19312855Sgabeblack@google.com stream2 = StringIO() 19412855Sgabeblack@google.com with redirect_stdout(stream): 19512855Sgabeblack@google.com with redirect_stderr(stream2): 19612855Sgabeblack@google.com with m.ostream_redirect(): 19712855Sgabeblack@google.com m.raw_output(msg) 19812855Sgabeblack@google.com m.raw_err(msg2) 19912855Sgabeblack@google.com stdout, stderr = capfd.readouterr() 20012855Sgabeblack@google.com assert stdout == '' 20112855Sgabeblack@google.com assert stderr == '' 20212855Sgabeblack@google.com assert stream.getvalue() == msg 20312855Sgabeblack@google.com assert stream2.getvalue() == msg2 20412855Sgabeblack@google.com