From 793f0b207fdf18101b19df103e0f9c21e0aaff9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?No=C3=ABlle=20Anthony?= Date: Thu, 1 Feb 2018 20:12:49 -0500 Subject: [PATCH] Update streaming.py's handle_stream This is an attempt to resolve an upstream issue with unexpected blank streams. The issue appears to be in urllib3, and causes fatal errors when the stream returned is `b''`. I believe this simple workaround will bypass the error and continue monitoring the stream. --- mastodon/streaming.py | 43 +++++++++++++++++++++++-------------------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/mastodon/streaming.py b/mastodon/streaming.py index d55ad54..c539b70 100644 --- a/mastodon/streaming.py +++ b/mastodon/streaming.py @@ -41,26 +41,29 @@ class StreamListener(object): response; a requests response object with the open stream for reading. """ - event = {} - line_buffer = bytearray() - for chunk in response.iter_content(chunk_size = 1): - if chunk: - if chunk == b'\n': - try: - line = line_buffer.decode('utf-8') - except UnicodeDecodeError as err: - six.raise_from( - MastodonMalformedEventError("Malformed UTF-8"), - err - ) - if line == '': - self._dispatch(event) - event = {} + try: + event = {} + line_buffer = bytearray() + for chunk in response.iter_content(chunk_size = 1): + if chunk: + if chunk == b'\n': + try: + line = line_buffer.decode('utf-8') + except UnicodeDecodeError as err: + six.raise_from( + MastodonMalformedEventError("Malformed UTF-8"), + err + ) + if line == '': + self._dispatch(event) + event = {} + else: + event = self._parse_line(line, event) + line_buffer = bytearray() else: - event = self._parse_line(line, event) - line_buffer = bytearray() - else: - line_buffer.extend(chunk) + line_buffer.extend(chunk) + except: + pass def _parse_line(self, line, event): if line.startswith(':'): @@ -136,4 +139,4 @@ class CallbackStreamListener(StreamListener): def on_notification(self, notification): if self.notification_handler != None: - self.notification_handler(notification) \ No newline at end of file + self.notification_handler(notification)