test_iostream.py
Go to the documentation of this file.
1 from __future__ import annotations
2 
3 import sys
4 from contextlib import redirect_stderr, redirect_stdout
5 from io import StringIO
6 
7 import pytest
8 
9 from pybind11_tests import iostream as m
10 
11 
12 def test_captured(capsys):
13  msg = "I've been redirected to Python, I hope!"
14  m.captured_output(msg)
15  stdout, stderr = capsys.readouterr()
16  assert stdout == msg
17  assert not stderr
18 
19  m.captured_output_default(msg)
20  stdout, stderr = capsys.readouterr()
21  assert stdout == msg
22  assert not stderr
23 
24  m.captured_err(msg)
25  stdout, stderr = capsys.readouterr()
26  assert not stdout
27  assert stderr == msg
28 
29 
31  # Make this bigger than the buffer used on the C++ side: 1024 chars
32  msg = "I've been redirected to Python, I hope!"
33  msg = msg * (1024 // len(msg) + 1)
34 
35  m.captured_output_default(msg)
36  stdout, stderr = capsys.readouterr()
37  assert stdout == msg
38  assert not stderr
39 
40 
42  msg = "\u07ff"
43  msg = "" + msg * (1024 // len(msg) + 1)
44 
45  m.captured_output_default(msg)
46  stdout, stderr = capsys.readouterr()
47  assert stdout == msg
48  assert not stderr
49 
50 
52  msg = "\u07ff"
53  msg = "1" + msg * (1024 // len(msg) + 1)
54 
55  m.captured_output_default(msg)
56  stdout, stderr = capsys.readouterr()
57  assert stdout == msg
58  assert not stderr
59 
60 
62  msg = "\uffff"
63  msg = "" + msg * (1024 // len(msg) + 1)
64 
65  m.captured_output_default(msg)
66  stdout, stderr = capsys.readouterr()
67  assert stdout == msg
68  assert not stderr
69 
70 
72  msg = "\uffff"
73  msg = "1" + msg * (1024 // len(msg) + 1)
74 
75  m.captured_output_default(msg)
76  stdout, stderr = capsys.readouterr()
77  assert stdout == msg
78  assert not stderr
79 
80 
82  msg = "\uffff"
83  msg = "12" + msg * (1024 // len(msg) + 1)
84 
85  m.captured_output_default(msg)
86  stdout, stderr = capsys.readouterr()
87  assert stdout == msg
88  assert not stderr
89 
90 
92  msg = "\U0010ffff"
93  msg = "" + msg * (1024 // len(msg) + 1)
94 
95  m.captured_output_default(msg)
96  stdout, stderr = capsys.readouterr()
97  assert stdout == msg
98  assert not stderr
99 
100 
102  msg = "\U0010ffff"
103  msg = "1" + msg * (1024 // len(msg) + 1)
104 
105  m.captured_output_default(msg)
106  stdout, stderr = capsys.readouterr()
107  assert stdout == msg
108  assert not stderr
109 
110 
112  msg = "\U0010ffff"
113  msg = "12" + msg * (1024 // len(msg) + 1)
114 
115  m.captured_output_default(msg)
116  stdout, stderr = capsys.readouterr()
117  assert stdout == msg
118  assert not stderr
119 
120 
122  msg = "\U0010ffff"
123  msg = "123" + msg * (1024 // len(msg) + 1)
124 
125  m.captured_output_default(msg)
126  stdout, stderr = capsys.readouterr()
127  assert stdout == msg
128  assert not stderr
129 
130 
131 def test_guard_capture(capsys):
132  msg = "I've been redirected to Python, I hope!"
133  m.guard_output(msg)
134  stdout, stderr = capsys.readouterr()
135  assert stdout == msg
136  assert not stderr
137 
138 
139 def test_series_captured(capture):
140  with capture:
141  m.captured_output("a")
142  m.captured_output("b")
143  assert capture == "ab"
144 
145 
146 def test_flush(capfd):
147  msg = "(not flushed)"
148  msg2 = "(flushed)"
149 
150  with m.ostream_redirect():
151  m.noisy_function(msg, flush=False)
152  stdout, stderr = capfd.readouterr()
153  assert not stdout
154 
155  m.noisy_function(msg2, flush=True)
156  stdout, stderr = capfd.readouterr()
157  assert stdout == msg + msg2
158 
159  m.noisy_function(msg, flush=False)
160 
161  stdout, stderr = capfd.readouterr()
162  assert stdout == msg
163 
164 
165 def test_not_captured(capfd):
166  msg = "Something that should not show up in log"
167  stream = StringIO()
168  with redirect_stdout(stream):
169  m.raw_output(msg)
170  stdout, stderr = capfd.readouterr()
171  assert stdout == msg
172  assert not stderr
173  assert not stream.getvalue()
174 
175  stream = StringIO()
176  with redirect_stdout(stream):
177  m.captured_output(msg)
178  stdout, stderr = capfd.readouterr()
179  assert not stdout
180  assert not stderr
181  assert stream.getvalue() == msg
182 
183 
184 def test_err(capfd):
185  msg = "Something that should not show up in log"
186  stream = StringIO()
187  with redirect_stderr(stream):
188  m.raw_err(msg)
189  stdout, stderr = capfd.readouterr()
190  assert not stdout
191  assert stderr == msg
192  assert not stream.getvalue()
193 
194  stream = StringIO()
195  with redirect_stderr(stream):
196  m.captured_err(msg)
197  stdout, stderr = capfd.readouterr()
198  assert not stdout
199  assert not stderr
200  assert stream.getvalue() == msg
201 
202 
204  stream = StringIO()
205  with redirect_stdout(stream):
206  m.captured_output("a")
207  m.raw_output("b")
208  m.captured_output("c")
209  m.raw_output("d")
210  stdout, stderr = capfd.readouterr()
211  assert stdout == "bd"
212  assert stream.getvalue() == "ac"
213 
214 
215 def test_dual(capsys):
216  m.captured_dual("a", "b")
217  stdout, stderr = capsys.readouterr()
218  assert stdout == "a"
219  assert stderr == "b"
220 
221 
222 def test_redirect(capfd):
223  msg = "Should not be in log!"
224  stream = StringIO()
225  with redirect_stdout(stream):
226  m.raw_output(msg)
227  stdout, stderr = capfd.readouterr()
228  assert stdout == msg
229  assert not stream.getvalue()
230 
231  stream = StringIO()
232  with redirect_stdout(stream), m.ostream_redirect():
233  m.raw_output(msg)
234  stdout, stderr = capfd.readouterr()
235  assert not stdout
236  assert stream.getvalue() == msg
237 
238  stream = StringIO()
239  with redirect_stdout(stream):
240  m.raw_output(msg)
241  stdout, stderr = capfd.readouterr()
242  assert stdout == msg
243  assert not stream.getvalue()
244 
245 
246 def test_redirect_err(capfd):
247  msg = "StdOut"
248  msg2 = "StdErr"
249 
250  stream = StringIO()
251  with redirect_stderr(stream), m.ostream_redirect(stdout=False):
252  m.raw_output(msg)
253  m.raw_err(msg2)
254  stdout, stderr = capfd.readouterr()
255  assert stdout == msg
256  assert not stderr
257  assert stream.getvalue() == msg2
258 
259 
261  msg = "StdOut"
262  msg2 = "StdErr"
263 
264  stream = StringIO()
265  stream2 = StringIO()
266  with redirect_stdout(stream), redirect_stderr(stream2), m.ostream_redirect():
267  m.raw_output(msg)
268  m.raw_err(msg2)
269  stdout, stderr = capfd.readouterr()
270  assert not stdout
271  assert not stderr
272  assert stream.getvalue() == msg
273  assert stream2.getvalue() == msg2
274 
275 
276 @pytest.mark.skipif(sys.platform.startswith("emscripten"), reason="Requires threads")
278  with m.ostream_redirect(stdout=True, stderr=False):
279  # start some threads
280  threads = []
281 
282  # start some threads
283  for _j in range(20):
284  threads.append(m.TestThread())
285 
286  # give the threads some time to fail
287  threads[0].sleep()
288 
289  # stop all the threads
290  for t in threads:
291  t.stop()
292 
293  for t in threads:
294  t.join()
295 
296  # if a thread segfaults, we don't get here
297  assert True
test_iostream.test_captured_utf8_4byte_offset3
def test_captured_utf8_4byte_offset3(capsys)
Definition: test_iostream.py:121
test_iostream.test_captured_utf8_2byte_offset1
def test_captured_utf8_2byte_offset1(capsys)
Definition: test_iostream.py:51
test_iostream.test_captured_utf8_3byte_offset0
def test_captured_utf8_3byte_offset0(capsys)
Definition: test_iostream.py:61
test_iostream.test_captured_utf8_4byte_offset2
def test_captured_utf8_4byte_offset2(capsys)
Definition: test_iostream.py:111
test_iostream.test_guard_capture
def test_guard_capture(capsys)
Definition: test_iostream.py:131
test_iostream.test_captured_utf8_4byte_offset0
def test_captured_utf8_4byte_offset0(capsys)
Definition: test_iostream.py:91
test_iostream.test_err
def test_err(capfd)
Definition: test_iostream.py:184
gtsam::range
Double_ range(const Point2_ &p, const Point2_ &q)
Definition: slam/expressions.h:30
test_iostream.test_redirect_err
def test_redirect_err(capfd)
Definition: test_iostream.py:246
test_iostream.test_redirect_both
def test_redirect_both(capfd)
Definition: test_iostream.py:260
test_iostream.test_captured
def test_captured(capsys)
Definition: test_iostream.py:12
test_iostream.test_dual
def test_dual(capsys)
Definition: test_iostream.py:215
test_iostream.test_captured_large_string
def test_captured_large_string(capsys)
Definition: test_iostream.py:30
test_iostream.test_flush
def test_flush(capfd)
Definition: test_iostream.py:146
test_iostream.test_multi_captured
def test_multi_captured(capfd)
Definition: test_iostream.py:203
test_iostream.test_captured_utf8_3byte_offset1
def test_captured_utf8_3byte_offset1(capsys)
Definition: test_iostream.py:71
test_iostream.test_series_captured
def test_series_captured(capture)
Definition: test_iostream.py:139
test_iostream.test_redirect
def test_redirect(capfd)
Definition: test_iostream.py:222
test_iostream.test_captured_utf8_4byte_offset1
def test_captured_utf8_4byte_offset1(capsys)
Definition: test_iostream.py:101
test_iostream.test_captured_utf8_2byte_offset0
def test_captured_utf8_2byte_offset0(capsys)
Definition: test_iostream.py:41
len
size_t len(handle h)
Get the length of a Python object.
Definition: pytypes.h:2448
test_iostream.test_threading
def test_threading()
Definition: test_iostream.py:277
test_iostream.test_not_captured
def test_not_captured(capfd)
Definition: test_iostream.py:165
test_iostream.test_captured_utf8_3byte_offset2
def test_captured_utf8_3byte_offset2(capsys)
Definition: test_iostream.py:81


gtsam
Author(s):
autogenerated on Wed Mar 19 2025 03:06:17