diff --git a/uasyncio/test_readline.py b/uasyncio/test_readline.py new file mode 100644 index 00000000..ac99eaf3 --- /dev/null +++ b/uasyncio/test_readline.py @@ -0,0 +1,30 @@ +from uasyncio import StreamReader + +class MockSock: + + def __init__(self, data_list): + self.data = data_list + + def readline(self): + try: + return self.data.pop(0) + except IndexError: + return b"" + + +mock = MockSock([ + b"line1\n", + b"parts ", b"of ", b"line2\n", + b"unterminated", +]) + + +def func(): + sr = StreamReader(mock) + assert await sr.readline() == b"line1\n" + assert await sr.readline() == b"parts of line2\n" + assert await sr.readline() == b"unterminated" + assert await sr.readline() == b"" + +for i in func(): + pass diff --git a/uasyncio/uasyncio/__init__.py b/uasyncio/uasyncio/__init__.py index bc6e37b5..600dfa0f 100644 --- a/uasyncio/uasyncio/__init__.py +++ b/uasyncio/uasyncio/__init__.py @@ -92,19 +92,20 @@ class StreamReader: def readline(self): if __debug__: log.debug("StreamReader.readline()") -# if DEBUG and __debug__: -# log.debug("StreamReader.readline(): after IORead: %s", s) + buf = b"" while True: yield IORead(self.s) res = self.s.readline() - if res is not None: + assert res is not None + if not res: + yield IOReadDone(self.s) + break + buf += res + if buf[-1] == 0x0a: break - log.warn("Empty read") - if not res: - yield IOReadDone(self.s) if DEBUG and __debug__: - log.debug("StreamReader.readline(): res: %s", res) - return res + log.debug("StreamReader.readline(): %s", buf) + return buf def aclose(self): yield IOReadDone(self.s)