Skip to content

Commit 97559a2

Browse files
authored
Merge pull request #2 from pedro823/fix/buffers
Fix parsing of newlines
2 parents b5d9149 + de42844 commit 97559a2

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

lib/prod.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,27 @@ def __init__(self, sock: socket.socket, chunk_size: int = 1024):
1515
self.chunk_size = chunk_size
1616

1717
def readline(self):
18-
parts = list(self.buffer)
18+
if len(self.buffer) > 0:
19+
return self.buffer.popleft()
20+
21+
parts = []
1922
while True:
2023
part = self.socket.recv(self.chunk_size).decode('utf8')
2124
if len(part) == 0:
2225
# EOF
23-
if len(parts) > 0:
24-
return ''.join(parts)
2526
return ''
2627

2728
split_part = part.split('\n')
29+
2830
if len(split_part) == 1:
2931
# no newlines yet
3032
parts.append(part)
3133
continue
3234

35+
# foobar\n gets split to ['foobar', '']
36+
if split_part[-1] == '':
37+
split_part.pop()
38+
3339
if len(parts) > 0:
3440
parts.append(split_part[0])
3541
whole_part = ''.join(parts)

0 commit comments

Comments
 (0)