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