3
0
mirror of https://github.com/jlu5/PyLink.git synced 2024-11-01 09:19:23 +01:00

Irc: prevent rare UnicodeDecodeError by decoding individual lines, not the entire socket.recv() output

Such an error is rare, but it did occur twice since I've started developing this :)
This commit is contained in:
James Lu 2015-07-19 23:46:31 -07:00
parent fe9c317f2c
commit 06d17d578b

11
main.py
View File

@ -112,18 +112,19 @@ class Irc():
self.initVars() self.initVars()
def run(self): def run(self):
buf = "" buf = b""
data = "" data = b""
while (time.time() - self.lastping) < self.pingtimeout: while (time.time() - self.lastping) < self.pingtimeout:
log.debug('(%s) time_since_last_ping: %s', self.name, (time.time() - self.lastping)) log.debug('(%s) time_since_last_ping: %s', self.name, (time.time() - self.lastping))
log.debug('(%s) self.pingtimeout: %s', self.name, self.pingtimeout) log.debug('(%s) self.pingtimeout: %s', self.name, self.pingtimeout)
data = self.socket.recv(2048).decode("utf-8") data = self.socket.recv(2048)
buf += data buf += data
if self.connected and not data: if self.connected and not data:
log.warn('(%s) No data received and self.connected is not set; disconnecting!', self.name) log.warn('(%s) No data received and self.connected is not set; disconnecting!', self.name)
break break
while '\n' in buf: while b'\n' in buf:
line, buf = buf.split('\n', 1) line, buf = buf.split(b'\n', 1)
line = line.decode("utf-8")
log.debug("(%s) <- %s", self.name, line) log.debug("(%s) <- %s", self.name, line)
hook_args = self.proto.handle_events(self, line) hook_args = self.proto.handle_events(self, line)
# Only call our hooks if there's data to process. Handlers that support # Only call our hooks if there's data to process. Handlers that support