Skip to content

Commit ad161bc

Browse files
committed
Dedup _test_eintor test_read and test_readinto
Move common code into a helper function. Validated test fails if EINTR isn't handled by removing the `err == EINTR` from `fileutils.c` `_Py_read`, causing EINTR to not be retried.
1 parent 060a670 commit ad161bc

1 file changed

Lines changed: 25 additions & 45 deletions

File tree

Lib/test/_test_eintr.py

Lines changed: 25 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -123,66 +123,46 @@ def test_waitpid(self):
123123
def test_wait4(self):
124124
self._test_wait_single(lambda pid: os.wait4(pid, 0))
125125

126-
def test_read(self):
126+
def _interrupted_reads(self):
127+
"""Make a fd which will force block on read of expected bytes."""
127128
rd, wr = os.pipe()
128129
self.addCleanup(os.close, rd)
129130
# wr closed explicitly by parent
130131

131132
# the payload below are smaller than PIPE_BUF, hence the writes will be
132133
# atomic
133-
datas = [b"hello", b"world", b"spam"]
134-
135-
code = '\n'.join((
136-
'import os, sys, time',
137-
'',
138-
'wr = int(sys.argv[1])',
139-
'datas = %r' % datas,
140-
'sleep_time = %r' % self.sleep_time,
141-
'',
142-
'for data in datas:',
143-
' # let the parent block on read()',
144-
' time.sleep(sleep_time)',
145-
' os.write(wr, data)',
146-
))
134+
data = [b"hello", b"world", b"spam"]
147135

148-
proc = self.subprocess(code, str(wr), pass_fds=[wr])
149-
with kill_on_error(proc):
150-
os.close(wr)
151-
for data in datas:
152-
self.assertEqual(data, os.read(rd, len(data)))
153-
self.assertEqual(proc.wait(), 0)
154-
155-
def test_readinto(self):
156-
rd, wr = os.pipe()
157-
self.addCleanup(os.close, rd)
158-
# wr closed explicitly by parent
136+
code = textwrap.dedent(f"""
137+
import os, sys, time
159138
160-
# the payload below are smaller than PIPE_BUF, hence the writes will be
161-
# atomic
162-
datas = [b"hello", b"world", b"spam"]
139+
wr = int(sys.argv[1])
140+
data = {data!r}
141+
sleep_time = {self.sleep_time!r}
163142
164-
code = '\n'.join((
165-
'import os, sys, time',
166-
'',
167-
'wr = int(sys.argv[1])',
168-
'datas = %r' % datas,
169-
'sleep_time = %r' % self.sleep_time,
170-
'',
171-
'for data in datas:',
172-
' # let the parent block on read()',
173-
' time.sleep(sleep_time)',
174-
' os.write(wr, data)',
175-
))
143+
for datum in data:
144+
# let the parent block on read()
145+
time.sleep(sleep_time)
146+
os.write(wr, datum)
147+
""")
176148

177149
proc = self.subprocess(code, str(wr), pass_fds=[wr])
178150
with kill_on_error(proc):
179151
os.close(wr)
180-
for data in datas:
181-
buffer = bytearray(len(data))
182-
self.assertEqual(os.readinto(rd, buffer), len(data))
183-
self.assertEqual(buffer, data)
152+
for datum in data:
153+
yield rd, datum
184154
self.assertEqual(proc.wait(), 0)
185155

156+
def test_read(self):
157+
for fd, expected in self._interrupted_reads():
158+
self.assertEqual(expected, os.read(fd, len(expected)))
159+
160+
def test_readinto(self):
161+
for fd, expected in self._interrupted_reads():
162+
buffer = bytearray(len(expected))
163+
self.assertEqual(os.readinto(fd, buffer), len(expected))
164+
self.assertEqual(buffer, expected)
165+
186166
def test_write(self):
187167
rd, wr = os.pipe()
188168
self.addCleanup(os.close, wr)

0 commit comments

Comments
 (0)