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  @contextmanager
22  def redirect_stdout(target):
23  original = sys.stdout
24  sys.stdout = target
25  yield
26  sys.stdout = original
27 
28 try:
29  # Python 3.5
30  from contextlib import redirect_stderr
31 except ImportError:
32  @contextmanager
33  def redirect_stderr(target):
34  original = sys.stderr
35  sys.stderr = target
36  yield
37  sys.stderr = original
38 
39 
40 def test_captured(capsys):
41  msg = "I've been redirected to Python, I hope!"
42  m.captured_output(msg)
43  stdout, stderr = capsys.readouterr()
44  assert stdout == msg
45  assert stderr == ''
46 
47  m.captured_output_default(msg)
48  stdout, stderr = capsys.readouterr()
49  assert stdout == msg
50  assert stderr == ''
51 
52  m.captured_err(msg)
53  stdout, stderr = capsys.readouterr()
54  assert stdout == ''
55  assert stderr == msg
56 
57 
59  # Make this bigger than the buffer used on the C++ side: 1024 chars
60  msg = "I've been redirected to Python, I hope!"
61  msg = msg * (1024 // len(msg) + 1)
62 
63  m.captured_output_default(msg)
64  stdout, stderr = capsys.readouterr()
65  assert stdout == msg
66  assert stderr == ''
67 
68 
69 def test_guard_capture(capsys):
70  msg = "I've been redirected to Python, I hope!"
71  m.guard_output(msg)
72  stdout, stderr = capsys.readouterr()
73  assert stdout == msg
74  assert stderr == ''
75 
76 
77 def test_series_captured(capture):
78  with capture:
79  m.captured_output("a")
80  m.captured_output("b")
81  assert capture == "ab"
82 
83 
84 def test_flush(capfd):
85  msg = "(not flushed)"
86  msg2 = "(flushed)"
87 
88  with m.ostream_redirect():
89  m.noisy_function(msg, flush=False)
90  stdout, stderr = capfd.readouterr()
91  assert stdout == ''
92 
93  m.noisy_function(msg2, flush=True)
94  stdout, stderr = capfd.readouterr()
95  assert stdout == msg + msg2
96 
97  m.noisy_function(msg, flush=False)
98 
99  stdout, stderr = capfd.readouterr()
100  assert stdout == msg
101 
102 
103 def test_not_captured(capfd):
104  msg = "Something that should not show up in log"
105  stream = StringIO()
106  with redirect_stdout(stream):
107  m.raw_output(msg)
108  stdout, stderr = capfd.readouterr()
109  assert stdout == msg
110  assert stderr == ''
111  assert stream.getvalue() == ''
112 
113  stream = StringIO()
114  with redirect_stdout(stream):
115  m.captured_output(msg)
116  stdout, stderr = capfd.readouterr()
117  assert stdout == ''
118  assert stderr == ''
119  assert stream.getvalue() == msg
120 
121 
122 def test_err(capfd):
123  msg = "Something that should not show up in log"
124  stream = StringIO()
125  with redirect_stderr(stream):
126  m.raw_err(msg)
127  stdout, stderr = capfd.readouterr()
128  assert stdout == ''
129  assert stderr == msg
130  assert stream.getvalue() == ''
131 
132  stream = StringIO()
133  with redirect_stderr(stream):
134  m.captured_err(msg)
135  stdout, stderr = capfd.readouterr()
136  assert stdout == ''
137  assert stderr == ''
138  assert stream.getvalue() == msg
139 
140 
142  stream = StringIO()
143  with redirect_stdout(stream):
144  m.captured_output("a")
145  m.raw_output("b")
146  m.captured_output("c")
147  m.raw_output("d")
148  stdout, stderr = capfd.readouterr()
149  assert stdout == 'bd'
150  assert stream.getvalue() == 'ac'
151 
152 
153 def test_dual(capsys):
154  m.captured_dual("a", "b")
155  stdout, stderr = capsys.readouterr()
156  assert stdout == "a"
157  assert stderr == "b"
158 
159 
160 def test_redirect(capfd):
161  msg = "Should not be in log!"
162  stream = StringIO()
163  with redirect_stdout(stream):
164  m.raw_output(msg)
165  stdout, stderr = capfd.readouterr()
166  assert stdout == msg
167  assert stream.getvalue() == ''
168 
169  stream = StringIO()
170  with redirect_stdout(stream):
171  with m.ostream_redirect():
172  m.raw_output(msg)
173  stdout, stderr = capfd.readouterr()
174  assert stdout == ''
175  assert stream.getvalue() == msg
176 
177  stream = StringIO()
178  with redirect_stdout(stream):
179  m.raw_output(msg)
180  stdout, stderr = capfd.readouterr()
181  assert stdout == msg
182  assert stream.getvalue() == ''
183 
184 
185 def test_redirect_err(capfd):
186  msg = "StdOut"
187  msg2 = "StdErr"
188 
189  stream = StringIO()
190  with redirect_stderr(stream):
191  with m.ostream_redirect(stdout=False):
192  m.raw_output(msg)
193  m.raw_err(msg2)
194  stdout, stderr = capfd.readouterr()
195  assert stdout == msg
196  assert stderr == ''
197  assert stream.getvalue() == msg2
198 
199 
201  msg = "StdOut"
202  msg2 = "StdErr"
203 
204  stream = StringIO()
205  stream2 = StringIO()
206  with redirect_stdout(stream):
207  with redirect_stderr(stream2):
208  with m.ostream_redirect():
209  m.raw_output(msg)
210  m.raw_err(msg2)
211  stdout, stderr = capfd.readouterr()
212  assert stdout == ''
213  assert stderr == ''
214  assert stream.getvalue() == msg
215  assert stream2.getvalue() == msg2
def redirect_stderr(target)
def test_guard_capture(capsys)
def test_captured(capsys)
def test_captured_large_string(capsys)
def test_series_captured(capture)
def test_redirect(capfd)
def test_not_captured(capfd)
def test_err(capfd)
def test_redirect_both(capfd)
def test_redirect_err(capfd)
def test_multi_captured(capfd)
def redirect_stdout(target)
def test_flush(capfd)
size_t len(handle h)
Definition: pytypes.h:1514
def test_dual(capsys)


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:46:03