Skip to content
Snippets Groups Projects
Commit 873e0277 authored by nickolas360's avatar nickolas360
Browse files

Decode bytes after full line received

Wait to decode bytes received from the server until a full line has
been received. Data received from the server could end in the middle
of a UTF-8 multibyte sequence, and decoding the data without waiting
for the rest of the line would result in a decoding error.
parent ac94916f
No related branches found
No related tags found
No related merge requests found
Loading
Loading
@@ -88,7 +88,7 @@ class IRCBot(object):
 
# Initializes attributes.
def _init_attributes(self):
self._buffer = ""
self._buffer = b""
self.socket = None
self.hostname = None
self.port = None
Loading
Loading
@@ -893,13 +893,14 @@ class IRCBot(object):
 
# Reads a line from the socket.
def readline(self):
while "\r\n" not in self._buffer:
while b"\r\n" not in self._buffer:
data = self.socket.recv(1024)
if not data:
return
self._buffer += data.decode("utf8", "ignore")
self._buffer += data
 
line, self._buffer = self._buffer.split("\r\n", 1)
line_bytes, self._buffer = self._buffer.split(b"\r\n", 1)
line = line_bytes.decode("utf8", "ignore")
if self.debug_print:
self.print_function(line)
return line
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment