test_iostream.py 5.7 KB

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