File tree 2 files changed +39
-8
lines changed
2 files changed +39
-8
lines changed Original file line number Diff line number Diff line change
1
+ from uasyncio import StreamReader
2
+
3
+ class MockSock :
4
+
5
+ def __init__ (self , data_list ):
6
+ self .data = data_list
7
+
8
+ def readline (self ):
9
+ try :
10
+ return self .data .pop (0 )
11
+ except IndexError :
12
+ return b""
13
+
14
+
15
+ mock = MockSock ([
16
+ b"line1\n " ,
17
+ b"parts " , b"of " , b"line2\n " ,
18
+ b"unterminated" ,
19
+ ])
20
+
21
+
22
+ def func ():
23
+ sr = StreamReader (mock )
24
+ assert await sr .readline () == b"line1\n "
25
+ assert await sr .readline () == b"parts of line2\n "
26
+ assert await sr .readline () == b"unterminated"
27
+ assert await sr .readline () == b""
28
+
29
+ for i in func ():
30
+ pass
Original file line number Diff line number Diff line change @@ -92,19 +92,20 @@ def read(self, n=-1):
92
92
def readline (self ):
93
93
if __debug__ :
94
94
log .debug ("StreamReader.readline()" )
95
- # if DEBUG and __debug__:
96
- # log.debug("StreamReader.readline(): after IORead: %s", s)
95
+ buf = b""
97
96
while True :
98
97
yield IORead (self .s )
99
98
res = self .s .readline ()
100
- if res is not None :
99
+ assert res is not None
100
+ if not res :
101
+ yield IOReadDone (self .s )
102
+ break
103
+ buf += res
104
+ if buf [- 1 ] == 0x0a :
101
105
break
102
- log .warn ("Empty read" )
103
- if not res :
104
- yield IOReadDone (self .s )
105
106
if DEBUG and __debug__ :
106
- log .debug ("StreamReader.readline(): res: %s" , res )
107
- return res
107
+ log .debug ("StreamReader.readline(): %s" , buf )
108
+ return buf
108
109
109
110
def aclose (self ):
110
111
yield IOReadDone (self .s )
You can’t perform that action at this time.
0 commit comments