1 from __future__
import annotations
3 from contextlib
import redirect_stderr, redirect_stdout
4 from io
import StringIO
6 from pybind11_tests
import iostream
as m
10 msg =
"I've been redirected to Python, I hope!"
11 m.captured_output(msg)
12 stdout, stderr = capsys.readouterr()
16 m.captured_output_default(msg)
17 stdout, stderr = capsys.readouterr()
22 stdout, stderr = capsys.readouterr()
29 msg =
"I've been redirected to Python, I hope!"
30 msg = msg * (1024 //
len(msg) + 1)
32 m.captured_output_default(msg)
33 stdout, stderr = capsys.readouterr()
40 msg =
"" + msg * (1024 //
len(msg) + 1)
42 m.captured_output_default(msg)
43 stdout, stderr = capsys.readouterr()
50 msg =
"1" + msg * (1024 //
len(msg) + 1)
52 m.captured_output_default(msg)
53 stdout, stderr = capsys.readouterr()
60 msg =
"" + msg * (1024 //
len(msg) + 1)
62 m.captured_output_default(msg)
63 stdout, stderr = capsys.readouterr()
70 msg =
"1" + msg * (1024 //
len(msg) + 1)
72 m.captured_output_default(msg)
73 stdout, stderr = capsys.readouterr()
80 msg =
"12" + msg * (1024 //
len(msg) + 1)
82 m.captured_output_default(msg)
83 stdout, stderr = capsys.readouterr()
90 msg =
"" + msg * (1024 //
len(msg) + 1)
92 m.captured_output_default(msg)
93 stdout, stderr = capsys.readouterr()
100 msg =
"1" + msg * (1024 //
len(msg) + 1)
102 m.captured_output_default(msg)
103 stdout, stderr = capsys.readouterr()
110 msg =
"12" + msg * (1024 //
len(msg) + 1)
112 m.captured_output_default(msg)
113 stdout, stderr = capsys.readouterr()
120 msg =
"123" + msg * (1024 //
len(msg) + 1)
122 m.captured_output_default(msg)
123 stdout, stderr = capsys.readouterr()
129 msg =
"I've been redirected to Python, I hope!"
131 stdout, stderr = capsys.readouterr()
138 m.captured_output(
"a")
139 m.captured_output(
"b")
140 assert capture ==
"ab"
144 msg =
"(not flushed)"
147 with m.ostream_redirect():
148 m.noisy_function(msg, flush=
False)
149 stdout, stderr = capfd.readouterr()
152 m.noisy_function(msg2, flush=
True)
153 stdout, stderr = capfd.readouterr()
154 assert stdout == msg + msg2
156 m.noisy_function(msg, flush=
False)
158 stdout, stderr = capfd.readouterr()
163 msg =
"Something that should not show up in log"
165 with redirect_stdout(stream):
167 stdout, stderr = capfd.readouterr()
170 assert not stream.getvalue()
173 with redirect_stdout(stream):
174 m.captured_output(msg)
175 stdout, stderr = capfd.readouterr()
178 assert stream.getvalue() == msg
182 msg =
"Something that should not show up in log"
184 with redirect_stderr(stream):
186 stdout, stderr = capfd.readouterr()
189 assert not stream.getvalue()
192 with redirect_stderr(stream):
194 stdout, stderr = capfd.readouterr()
197 assert stream.getvalue() == msg
202 with redirect_stdout(stream):
203 m.captured_output(
"a")
205 m.captured_output(
"c")
207 stdout, stderr = capfd.readouterr()
208 assert stdout ==
"bd"
209 assert stream.getvalue() ==
"ac"
213 m.captured_dual(
"a",
"b")
214 stdout, stderr = capsys.readouterr()
220 msg =
"Should not be in log!"
222 with redirect_stdout(stream):
224 stdout, stderr = capfd.readouterr()
226 assert not stream.getvalue()
229 with redirect_stdout(stream), m.ostream_redirect():
231 stdout, stderr = capfd.readouterr()
233 assert stream.getvalue() == msg
236 with redirect_stdout(stream):
238 stdout, stderr = capfd.readouterr()
240 assert not stream.getvalue()
248 with redirect_stderr(stream), m.ostream_redirect(stdout=
False):
251 stdout, stderr = capfd.readouterr()
254 assert stream.getvalue() == msg2
263 with redirect_stdout(stream), redirect_stderr(stream2), m.ostream_redirect():
266 stdout, stderr = capfd.readouterr()
269 assert stream.getvalue() == msg
270 assert stream2.getvalue() == msg2
274 with m.ostream_redirect(stdout=
True, stderr=
False):
280 threads.append(m.TestThread())