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
This commit is contained in:
@@ -63,7 +63,7 @@ class _BaseHTTPClient(object):
|
||||
chunk = body
|
||||
while chunk:
|
||||
chunk = body.read(CHUNKSIZE)
|
||||
if chunk == '':
|
||||
if not chunk:
|
||||
break
|
||||
yield chunk
|
||||
|
||||
|
@@ -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
|
||||
|
@@ -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)
|
||||
|
||||
|
@@ -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)
|
||||
|
@@ -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)
|
||||
|
@@ -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:
|
||||
|
Reference in New Issue
Block a user