summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSirushti Murugesan <sirushti.murugesan@hp.com>2016-06-23 19:08:51 +0530
committerSirushti Murugesan <sirushti.murugesan@hp.com>2016-07-25 20:13:08 +0530
commit376037d3716244c32acf7fd4c096c99a8fa1b4c7 (patch)
tree53009c569e7ab7f37b3f64db563eebafb75088f0
parenta0d1cc974e78c1f3e447e847d6a2dbb50ab3c582 (diff)
downloadpython-glanceclient-376037d3716244c32acf7fd4c096c99a8fa1b4c7.tar.gz
py3: Fix encoding and use sys.stdin.buffer
* exc.py: Encode body in response before calling replace over it. * http.py: prepend the bytes literal to the empty string or else we hit bug 1342080 again in python 3. * utils.py: Use sys.stdin.buffer in python 3. Change-Id: Ieefb8c633658e507486438e5518c5d53e819027d
-rw-r--r--glanceclient/common/http.py2
-rw-r--r--glanceclient/common/utils.py7
-rw-r--r--glanceclient/exc.py4
-rw-r--r--glanceclient/tests/unit/test_exc.py8
-rw-r--r--glanceclient/tests/unit/test_http.py8
-rw-r--r--glanceclient/tests/unit/v1/test_shell.py4
6 files changed, 28 insertions, 5 deletions
diff --git a/glanceclient/common/http.py b/glanceclient/common/http.py
index 0eb42de..7556537 100644
--- a/glanceclient/common/http.py
+++ b/glanceclient/common/http.py
@@ -63,7 +63,7 @@ class _BaseHTTPClient(object):
chunk = body
while chunk:
chunk = body.read(CHUNKSIZE)
- if chunk == '':
+ if not chunk:
break
yield chunk
diff --git a/glanceclient/common/utils.py b/glanceclient/common/utils.py
index 2bdca0a..9f3a1fe 100644
--- a/glanceclient/common/utils.py
+++ b/glanceclient/common/utils.py
@@ -360,9 +360,12 @@ def get_data_file(args):
return None
if not sys.stdin.isatty():
# (2) image data is provided through standard input
+ image = sys.stdin
+ if hasattr(sys.stdin, 'buffer'):
+ image = sys.stdin.buffer
if msvcrt:
- msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY)
- return sys.stdin
+ msvcrt.setmode(image.fileno(), os.O_BINARY)
+ return image
else:
# (3) no image data provided
return None
diff --git a/glanceclient/exc.py b/glanceclient/exc.py
index 29189e4..c8616c3 100644
--- a/glanceclient/exc.py
+++ b/glanceclient/exc.py
@@ -16,6 +16,8 @@
import re
import sys
+import six
+
class BaseException(Exception):
"""An error occurred."""
@@ -177,6 +179,8 @@ def from_response(response, body=None):
details = ': '.join(details_temp)
return cls(details=details)
elif body:
+ if six.PY3:
+ body = body.decode('utf-8')
details = body.replace('\n\n', '\n')
return cls(details=details)
diff --git a/glanceclient/tests/unit/test_exc.py b/glanceclient/tests/unit/test_exc.py
index 575c62b..9a2d01f 100644
--- a/glanceclient/tests/unit/test_exc.py
+++ b/glanceclient/tests/unit/test_exc.py
@@ -68,3 +68,11 @@ class TestHTTPExceptions(testtools.TestCase):
self.assertIsInstance(err, exc.HTTPNotFound)
self.assertEqual("404 Entity Not Found: Entity could not be found",
err.details)
+
+ def test_format_no_content_type(self):
+ mock_resp = mock.Mock()
+ mock_resp.status_code = 400
+ mock_resp.headers = {'content-type': 'application/octet-stream'}
+ body = b'Error \n\n'
+ err = exc.from_response(mock_resp, body)
+ self.assertEqual('Error \n', err.details)
diff --git a/glanceclient/tests/unit/test_http.py b/glanceclient/tests/unit/test_http.py
index 28ae8e1..020e146 100644
--- a/glanceclient/tests/unit/test_http.py
+++ b/glanceclient/tests/unit/test_http.py
@@ -239,6 +239,14 @@ class TestClient(testtools.TestCase):
test_client = http.HTTPClient(endpoint, token=u'adc123')
self.assertEqual(600.0, test_client.timeout)
+ def test__chunk_body_exact_size_chunk(self):
+ test_client = http._BaseHTTPClient()
+ bytestring = b'x' * http.CHUNKSIZE
+ data = six.BytesIO(bytestring)
+ chunk = list(test_client._chunk_body(data))
+ self.assertEqual(1, len(chunk))
+ self.assertEqual([bytestring], chunk)
+
def test_http_chunked_request(self):
text = "Ok"
data = six.StringIO(text)
diff --git a/glanceclient/tests/unit/v1/test_shell.py b/glanceclient/tests/unit/v1/test_shell.py
index 93f3fe6..95bbd07 100644
--- a/glanceclient/tests/unit/v1/test_shell.py
+++ b/glanceclient/tests/unit/v1/test_shell.py
@@ -574,7 +574,7 @@ class ShellStdinHandlingTests(testtools.TestCase):
self.assertIn('data', self.collected_args[1])
self.assertIsInstance(self.collected_args[1]['data'], file_type)
- self.assertEqual('Some Data',
+ self.assertEqual(b'Some Data',
self.collected_args[1]['data'].read())
finally:
@@ -599,7 +599,7 @@ class ShellStdinHandlingTests(testtools.TestCase):
self.assertIn('data', self.collected_args[1])
self.assertIsInstance(self.collected_args[1]['data'], file_type)
- self.assertEqual('Some Data\n',
+ self.assertEqual(b'Some Data\n',
self.collected_args[1]['data'].read())
finally: