mirror of https://github.com/jesopo/irctokens
StatefulDecoder.push() should return None when given b"" (disconnect)
This commit is contained in:
parent
949b10945e
commit
3cdc163b87
|
@ -46,6 +46,10 @@ _send(irctokens.format("NICK", ["nickname"]))
|
|||
|
||||
while True:
|
||||
lines = d.push(s.recv(1024))
|
||||
if lines == None:
|
||||
print("! disconnected")
|
||||
break
|
||||
|
||||
for line in lines:
|
||||
if line.command == "PING":
|
||||
to_send = irctokens.format("PONG", [line.params[0]])
|
||||
|
|
|
@ -6,7 +6,10 @@ class StatefulDecoder(object):
|
|||
self._fallback = fallback
|
||||
self._buffer = b""
|
||||
|
||||
def push(self, data: bytes) -> typing.List[Line]:
|
||||
def push(self, data: bytes) -> typing.Optional[typing.List[Line]]:
|
||||
if not data:
|
||||
return None
|
||||
|
||||
self._buffer += data
|
||||
lines = [l.strip(b"\r") for l in self._buffer.split(b"\n")]
|
||||
self._buffer = lines.pop(-1)
|
||||
|
|
|
@ -31,3 +31,15 @@ class TestFallback(unittest.TestCase):
|
|||
self.assertEqual(len(lines), 1)
|
||||
line = irctokens.tokenise("PRIVMSG #channel hélló")
|
||||
self.assertEqual(lines[0], line)
|
||||
|
||||
class TestEmpty(unittest.TestCase):
|
||||
def test_immediate(self):
|
||||
d = irctokens.StatefulDecoder()
|
||||
lines = d.push(b"")
|
||||
self.assertIsNone(lines)
|
||||
|
||||
def test_buffer_unfinished(self):
|
||||
d = irctokens.StatefulDecoder()
|
||||
d.push(b"PRIVMSG #channel hello")
|
||||
lines = d.push(b"")
|
||||
self.assertIsNone(lines)
|
||||
|
|
Loading…
Reference in New Issue