File: fix-py3-str-bytes-error.patch

package info (click to toggle)
python-watcherclient 2.1.0-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 840 kB
  • sloc: python: 7,638; makefile: 43; sh: 30
file content (31 lines) | stat: -rw-r--r-- 1,454 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
Description: Fix watherclient error in py3
 watchercliet will raise error:
 Recoverable error: sequence item 0: expected str instance, bytes found
 This patch fix this.
Author: zhurong <aaronzhu1121@gmail.com>
Date: Fri, 27 Jul 2018 09:27:56 +0000
Change-Id: I6fe21766f320b0a09a14202203a5d0451175e1d3
Origin: upstream, https://review.openstack.org/#/c/585938/
Last-Update: 2018-09-05

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 @@
         # 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: