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
|
chunk = body
|
||||||
while chunk:
|
while chunk:
|
||||||
chunk = body.read(CHUNKSIZE)
|
chunk = body.read(CHUNKSIZE)
|
||||||
if chunk == '':
|
if not chunk:
|
||||||
break
|
break
|
||||||
yield chunk
|
yield chunk
|
||||||
|
|
||||||
|
@@ -360,9 +360,12 @@ def get_data_file(args):
|
|||||||
return None
|
return None
|
||||||
if not sys.stdin.isatty():
|
if not sys.stdin.isatty():
|
||||||
# (2) image data is provided through standard input
|
# (2) image data is provided through standard input
|
||||||
|
image = sys.stdin
|
||||||
|
if hasattr(sys.stdin, 'buffer'):
|
||||||
|
image = sys.stdin.buffer
|
||||||
if msvcrt:
|
if msvcrt:
|
||||||
msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY)
|
msvcrt.setmode(image.fileno(), os.O_BINARY)
|
||||||
return sys.stdin
|
return image
|
||||||
else:
|
else:
|
||||||
# (3) no image data provided
|
# (3) no image data provided
|
||||||
return None
|
return None
|
||||||
|
@@ -16,6 +16,8 @@
|
|||||||
import re
|
import re
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
import six
|
||||||
|
|
||||||
|
|
||||||
class BaseException(Exception):
|
class BaseException(Exception):
|
||||||
"""An error occurred."""
|
"""An error occurred."""
|
||||||
@@ -177,6 +179,8 @@ def from_response(response, body=None):
|
|||||||
details = ': '.join(details_temp)
|
details = ': '.join(details_temp)
|
||||||
return cls(details=details)
|
return cls(details=details)
|
||||||
elif body:
|
elif body:
|
||||||
|
if six.PY3:
|
||||||
|
body = body.decode('utf-8')
|
||||||
details = body.replace('\n\n', '\n')
|
details = body.replace('\n\n', '\n')
|
||||||
return cls(details=details)
|
return cls(details=details)
|
||||||
|
|
||||||
|
@@ -68,3 +68,11 @@ class TestHTTPExceptions(testtools.TestCase):
|
|||||||
self.assertIsInstance(err, exc.HTTPNotFound)
|
self.assertIsInstance(err, exc.HTTPNotFound)
|
||||||
self.assertEqual("404 Entity Not Found: Entity could not be found",
|
self.assertEqual("404 Entity Not Found: Entity could not be found",
|
||||||
err.details)
|
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')
|
test_client = http.HTTPClient(endpoint, token=u'adc123')
|
||||||
self.assertEqual(600.0, test_client.timeout)
|
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):
|
def test_http_chunked_request(self):
|
||||||
text = "Ok"
|
text = "Ok"
|
||||||
data = six.StringIO(text)
|
data = six.StringIO(text)
|
||||||
|
@@ -574,7 +574,7 @@ class ShellStdinHandlingTests(testtools.TestCase):
|
|||||||
|
|
||||||
self.assertIn('data', self.collected_args[1])
|
self.assertIn('data', self.collected_args[1])
|
||||||
self.assertIsInstance(self.collected_args[1]['data'], file_type)
|
self.assertIsInstance(self.collected_args[1]['data'], file_type)
|
||||||
self.assertEqual('Some Data',
|
self.assertEqual(b'Some Data',
|
||||||
self.collected_args[1]['data'].read())
|
self.collected_args[1]['data'].read())
|
||||||
|
|
||||||
finally:
|
finally:
|
||||||
@@ -599,7 +599,7 @@ class ShellStdinHandlingTests(testtools.TestCase):
|
|||||||
|
|
||||||
self.assertIn('data', self.collected_args[1])
|
self.assertIn('data', self.collected_args[1])
|
||||||
self.assertIsInstance(self.collected_args[1]['data'], file_type)
|
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())
|
self.collected_args[1]['data'].read())
|
||||||
|
|
||||||
finally:
|
finally:
|
||||||
|
Reference in New Issue
Block a user