MCPcopy Index your code
hub / github.com/python/cpython / test_write

Method test_write

Lib/test/_test_eintr.py:166–202  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

164 self.assertEqual(buffer, expected)
165
166 def test_write(self):
167 rd, wr = os.pipe()
168 self.addCleanup(os.close, wr)
169 # rd closed explicitly by parent
170
171 # we must write enough data for the write() to block
172 data = b"x" * support.PIPE_MAX_SIZE
173
174 code = '\n'.join((
175 'import io, os, sys, time',
176 '',
177 'rd = int(sys.argv[1])',
178 f'sleep_time = {self.sleep_time!r}',
179 f'data = b"x" * {support.PIPE_MAX_SIZE}',
180 'data_len = len(data)',
181 '',
182 '# let the parent block on write()',
183 'time.sleep(sleep_time)',
184 '',
185 'read_data = io.BytesIO()',
186 'while len(read_data.getvalue()) < data_len:',
187 ' chunk = os.read(rd, 2 * data_len)',
188 ' read_data.write(chunk)',
189 '',
190 'value = read_data.getvalue()',
191 'if value != data:',
192 ' raise Exception(f"read error: {len(value)}'
193 ' vs {data_len} bytes")',
194 ))
195
196 proc = self.subprocess(code, str(rd), pass_fds=[rd])
197 with kill_on_error(proc):
198 os.close(rd)
199 written = 0
200 while written < len(data):
201 written += os.write(wr, memoryview(data)[written:])
202 self.assertEqual(proc.wait(), 0)
203
204
205@unittest.skipUnless(hasattr(signal, "setitimer"), "requires setitimer()")

Callers

nothing calls this directly

Calls 10

strFunction · 0.85
addCleanupMethod · 0.80
subprocessMethod · 0.80
kill_on_errorFunction · 0.70
pipeMethod · 0.45
joinMethod · 0.45
closeMethod · 0.45
writeMethod · 0.45
assertEqualMethod · 0.45
waitMethod · 0.45

Tested by

no test coverage detected