Skip to content

Commit e3787f0

Browse files
committed
fast_io StreamReader.readinto improvement.
1 parent 16e66f7 commit e3787f0

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

fast_io/__init__.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -164,12 +164,29 @@ def read(self, n=-1):
164164
# PollEventLoop._unregister
165165
return res # Next iteration raises StopIteration and returns result
166166

167-
def readinto(self, buf, n=-1): # Experimental and not yet tested TODO
168-
yield IORead(self.polls)
169-
res = self.ios.readinto(buf, n)
170-
assert res, 'zero bytes returned' # Temporary
171-
yield IOReadDone(self.polls)
172-
return res
167+
def readinto(self, buf, n=0): # Experimental and not yet tested TODO
168+
if DEBUG and __debug__:
169+
log.debug("StreamReader.readinto() START")
170+
171+
while True:
172+
yield IORead(self.polls)
173+
if DEBUG and __debug__:
174+
log.debug("StreamReader.readinto() ... just after IORead")
175+
if n:
176+
res = self.ios.readinto(buf, n) # Call the device's readinto method
177+
else:
178+
res = self.ios.readinto(buf)
179+
if res is not None:
180+
break
181+
# This should not happen for real sockets, but can easily
182+
# happen for stream wrappers (ssl, websockets, etc.)
183+
#log.warn("Empty read")
184+
yield IOReadDone(self.polls) # uasyncio.core calls remove_reader
185+
if DEBUG and __debug__:
186+
log.debug("StreamReader.readinto() ... just after IOReadDone")
187+
# This de-registers device as a read device with poll via
188+
# PollEventLoop._unregister
189+
return res # Next iteration raises StopIteration and returns result
173190

174191
def readexactly(self, n):
175192
buf = b""

0 commit comments

Comments
 (0)