From 989e4b36b57f2b5566bdfc1309e781acf67ca0d7 Mon Sep 17 00:00:00 2001 From: Dmitry Bazhal Date: Mon, 21 Jan 2019 16:43:38 +0300 Subject: [PATCH 1/2] fix stream data decoding Related to https://github.com/kubernetes-client/python-base/issues/88 and https://github.com/kubernetes-client/python-base/pull/104 I suppose decoding complete data is better than decoding data chunks. --- stream/ws_client.py | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/stream/ws_client.py b/stream/ws_client.py index 1cc56cdd..706c35dc 100644 --- a/stream/ws_client.py +++ b/stream/ws_client.py @@ -41,7 +41,10 @@ def __init__(self, configuration, url, headers): header = [] self._connected = False self._channels = {} - self._all = "" + if six.PY3: + self._all = b"" + else: + self._all = "" # We just need to pass the Authorization, ignore all the other # http headers we get from the generated code @@ -98,8 +101,11 @@ def readline_channel(self, channel, timeout=None): while self.is_open() and time.time() - start < timeout: if channel in self._channels: data = self._channels[channel] - if "\n" in data: - index = data.find("\n") + newline_symbol = "\n" + if six.PY3: + newline_Symbol = b"\n" + if newline_symbol in data: + index = data.find(newline_symbol) ret = data[:index] data = data[index+1:] if data: @@ -147,9 +153,15 @@ def read_all(self): channels mapped for each input. """ out = self._all - self._all = "" + if six.PY3: + self._all = b"" + else: + self._all = "" self._channels = {} - return out + if six.PY3: + return out.decode("utf-8", "replace") + else: + return out def is_open(self): """True if the connection is still alive.""" @@ -175,10 +187,11 @@ def update(self, timeout=0): return elif op_code == ABNF.OPCODE_BINARY or op_code == ABNF.OPCODE_TEXT: data = frame.data - if six.PY3: - data = data.decode("utf-8") if len(data) > 1: - channel = ord(data[0]) + if six.PY3: + channel = data[0] + else: + channel = ord(data[0]) data = data[1:] if data: if channel in [STDOUT_CHANNEL, STDERR_CHANNEL]: From 545690ca3d6789e361e2d2f45d337030d367b2ef Mon Sep 17 00:00:00 2001 From: Dmitry Bazhal Date: Mon, 21 Jan 2019 16:57:00 +0300 Subject: [PATCH 2/2] complete data decoding --- stream/ws_client.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/stream/ws_client.py b/stream/ws_client.py index 706c35dc..d3da3765 100644 --- a/stream/ws_client.py +++ b/stream/ws_client.py @@ -80,7 +80,11 @@ def peek_channel(self, channel, timeout=0): empty string otherwise.""" self.update(timeout=timeout) if channel in self._channels: + if six.PY3: + return self._channels[channel].decode("utf-8", "replace") return self._channels[channel] + if six.PY3: + return b"" return "" def read_channel(self, channel, timeout=0): @@ -91,6 +95,8 @@ def read_channel(self, channel, timeout=0): ret = self._channels[channel] if channel in self._channels: del self._channels[channel] + if six.PY3 and isinstance(ret, bytes): + return ret.decode("utf-8", "replace") return ret def readline_channel(self, channel, timeout=None): @@ -112,7 +118,10 @@ def readline_channel(self, channel, timeout=None): self._channels[channel] = data else: del self._channels[channel] - return ret + if six.PY3 and isinstance(ret, bytes): + return ret.decode("utf-8", "replace") + else: + return ret self.update(timeout=(timeout - time.time() + start)) def write_channel(self, channel, data):