From 2551ff09341c90aaf9109dd56c856aa398cb5a78 Mon Sep 17 00:00:00 2001 From: zhurong Date: Thu, 26 Jul 2018 11:34:06 +0800 Subject: [PATCH] Fix watherclient error in py3 watchercliet will raise error: Recoverable error: sequence item 0: expected str instance, bytes found This patch fix this. Change-Id: I6fe21766f320b0a09a14202203a5d0451175e1d3 --- watcherclient/common/httpclient.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/watcherclient/common/httpclient.py b/watcherclient/common/httpclient.py index 6eba147..36a8469 100644 --- a/watcherclient/common/httpclient.py +++ b/watcherclient/common/httpclient.py @@ -354,7 +354,15 @@ class HTTPClient(VersionNegotiationMixin): # Read body into string if it isn't obviously image data body_str = None if resp.headers.get('Content-Type') != 'application/octet-stream': - body_str = ''.join([chunk for chunk in body_iter]) + # decoding byte to string is necessary for Python 3 compatibility + # this issues has not been found with Python 3 unit tests + # because the test creates a fake http response of type str + # the if statement satisfies test (str) and real (bytes) behavior + body_list = [ + chunk.decode("utf-8") if isinstance(chunk, bytes) + else chunk for chunk in body_iter + ] + body_str = ''.join(body_list) self.log_http_response(resp, body_str) body_iter = six.StringIO(body_str) else: