test_iostream.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. from pybind11_tests import iostream as m
  2. import sys
  3. from contextlib import contextmanager
  4. try:
  5. # Python 3
  6. from io import StringIO
  7. except ImportError:
  8. # Python 2
  9. try:
  10. from cStringIO import StringIO
  11. except ImportError:
  12. from StringIO import StringIO
  13. try:
  14. # Python 3.4
  15. from contextlib import redirect_stdout
  16. except ImportError:
  17. @contextmanager
  18. def redirect_stdout(target):
  19. original = sys.stdout
  20. sys.stdout = target
  21. yield
  22. sys.stdout = original
  23. try:
  24. # Python 3.5
  25. from contextlib import redirect_stderr
  26. except ImportError:
  27. @contextmanager
  28. def redirect_stderr(target):
  29. original = sys.stderr
  30. sys.stderr = target
  31. yield
  32. sys.stderr = original
  33. def test_captured(capsys):
  34. msg = "I've been redirected to Python, I hope!"
  35. m.captured_output(msg)
  36. stdout, stderr = capsys.readouterr()
  37. assert stdout == msg
  38. assert stderr == ''
  39. m.captured_output_default(msg)
  40. stdout, stderr = capsys.readouterr()
  41. assert stdout == msg
  42. assert stderr == ''
  43. m.captured_err(msg)
  44. stdout, stderr = capsys.readouterr()
  45. assert stdout == ''
  46. assert stderr == msg
  47. def test_captured_large_string(capsys):
  48. # Make this bigger than the buffer used on the C++ side: 1024 chars
  49. msg = "I've been redirected to Python, I hope!"
  50. msg = msg * (1024 // len(msg) + 1)
  51. m.captured_output_default(msg)
  52. stdout, stderr = capsys.readouterr()
  53. assert stdout == msg
  54. assert stderr == ''
  55. def test_guard_capture(capsys):
  56. msg = "I've been redirected to Python, I hope!"
  57. m.guard_output(msg)
  58. stdout, stderr = capsys.readouterr()
  59. assert stdout == msg
  60. assert stderr == ''
  61. def test_series_captured(capture):
  62. with capture:
  63. m.captured_output("a")
  64. m.captured_output("b")
  65. assert capture == "ab"
  66. def test_flush(capfd):
  67. msg = "(not flushed)"
  68. msg2 = "(flushed)"
  69. with m.ostream_redirect():
  70. m.noisy_function(msg, flush=False)
  71. stdout, stderr = capfd.readouterr()
  72. assert stdout == ''
  73. m.noisy_function(msg2, flush=True)
  74. stdout, stderr = capfd.readouterr()
  75. assert stdout == msg + msg2
  76. m.noisy_function(msg, flush=False)
  77. stdout, stderr = capfd.readouterr()
  78. assert stdout == msg
  79. def test_not_captured(capfd):
  80. msg = "Something that should not show up in log"
  81. stream = StringIO()
  82. with redirect_stdout(stream):
  83. m.raw_output(msg)
  84. stdout, stderr = capfd.readouterr()
  85. assert stdout == msg
  86. assert stderr == ''
  87. assert stream.getvalue() == ''
  88. stream = StringIO()
  89. with redirect_stdout(stream):
  90. m.captured_output(msg)
  91. stdout, stderr = capfd.readouterr()
  92. assert stdout == ''
  93. assert stderr == ''
  94. assert stream.getvalue() == msg
  95. def test_err(capfd):
  96. msg = "Something that should not show up in log"
  97. stream = StringIO()
  98. with redirect_stderr(stream):
  99. m.raw_err(msg)
  100. stdout, stderr = capfd.readouterr()
  101. assert stdout == ''
  102. assert stderr == msg
  103. assert stream.getvalue() == ''
  104. stream = StringIO()
  105. with redirect_stderr(stream):
  106. m.captured_err(msg)
  107. stdout, stderr = capfd.readouterr()
  108. assert stdout == ''
  109. assert stderr == ''
  110. assert stream.getvalue() == msg
  111. def test_multi_captured(capfd):
  112. stream = StringIO()
  113. with redirect_stdout(stream):
  114. m.captured_output("a")
  115. m.raw_output("b")
  116. m.captured_output("c")
  117. m.raw_output("d")
  118. stdout, stderr = capfd.readouterr()
  119. assert stdout == 'bd'
  120. assert stream.getvalue() == 'ac'
  121. def test_dual(capsys):
  122. m.captured_dual("a", "b")
  123. stdout, stderr = capsys.readouterr()
  124. assert stdout == "a"
  125. assert stderr == "b"
  126. def test_redirect(capfd):
  127. msg = "Should not be in log!"
  128. stream = StringIO()
  129. with redirect_stdout(stream):
  130. m.raw_output(msg)
  131. stdout, stderr = capfd.readouterr()
  132. assert stdout == msg
  133. assert stream.getvalue() == ''
  134. stream = StringIO()
  135. with redirect_stdout(stream):
  136. with m.ostream_redirect():
  137. m.raw_output(msg)
  138. stdout, stderr = capfd.readouterr()
  139. assert stdout == ''
  140. assert stream.getvalue() == msg
  141. stream = StringIO()
  142. with redirect_stdout(stream):
  143. m.raw_output(msg)
  144. stdout, stderr = capfd.readouterr()
  145. assert stdout == msg
  146. assert stream.getvalue() == ''
  147. def test_redirect_err(capfd):
  148. msg = "StdOut"
  149. msg2 = "StdErr"
  150. stream = StringIO()
  151. with redirect_stderr(stream):
  152. with m.ostream_redirect(stdout=False):
  153. m.raw_output(msg)
  154. m.raw_err(msg2)
  155. stdout, stderr = capfd.readouterr()
  156. assert stdout == msg
  157. assert stderr == ''
  158. assert stream.getvalue() == msg2
  159. def test_redirect_both(capfd):
  160. msg = "StdOut"
  161. msg2 = "StdErr"
  162. stream = StringIO()
  163. stream2 = StringIO()
  164. with redirect_stdout(stream):
  165. with redirect_stderr(stream2):
  166. with m.ostream_redirect():
  167. m.raw_output(msg)
  168. m.raw_err(msg2)
  169. stdout, stderr = capfd.readouterr()
  170. assert stdout == ''
  171. assert stderr == ''
  172. assert stream.getvalue() == msg
  173. assert stream2.getvalue() == msg2