@@ -123,7 +123,8 @@ 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
@@ -148,40 +149,19 @@ def test_read(self):
148149 proc = self .subprocess (code , str (wr ), pass_fds = [wr ])
149150 with kill_on_error (proc ):
150151 os .close (wr )
151- for item in data :
152- self . assertEqual ( item , os . read ( rd , len ( item )))
152+ for datum in data :
153+ yield rd , datum
153154 self .assertEqual (proc .wait (), 0 )
154155
155- def test_readinto (self ):
156- rd , wr = os .pipe ()
157- self .addCleanup (os .close , rd )
158- # wr closed explicitly by parent
159-
160- # the payload below are smaller than PIPE_BUF, hence the writes will be
161- # atomic
162- data = [b"hello" , b"world" , b"spam" ]
163-
164- code = '\n ' .join ((
165- 'import os, sys, time' ,
166- '' ,
167- 'wr = int(sys.argv[1])' ,
168- f'data = { data !r} ' ,
169- f'sleep_time = { self .sleep_time !r} ' ,
170- '' ,
171- 'for item in data:' ,
172- ' # let the parent block on read()' ,
173- ' time.sleep(sleep_time)' ,
174- ' os.write(wr, item)' ,
175- ))
156+ def test_read (self ):
157+ for fd , expected in self ._interrupted_reads ():
158+ self .assertEqual (expected , os .read (fd , len (expected )))
176159
177- proc = self .subprocess (code , str (wr ), pass_fds = [wr ])
178- with kill_on_error (proc ):
179- os .close (wr )
180- for item in data :
181- buffer = bytearray (len (item ))
182- self .assertEqual (os .readinto (rd , buffer ), len (item ))
183- self .assertEqual (buffer , item )
184- self .assertEqual (proc .wait (), 0 )
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 )
185165
186166 def test_write (self ):
187167 rd , wr = os .pipe ()
0 commit comments