Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for memoryview objects #3107

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion botocore/awsrequest.py
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ def reset_stream(self):
# the entire body contents again if we need to).
# Same case if the body is a string/bytes/bytearray type.

non_seekable_types = (bytes, str, bytearray)
non_seekable_types = (bytes, str, bytearray, memoryview)
if self.body is None or isinstance(self.body, non_seekable_types):
return
try:
Expand Down
4 changes: 2 additions & 2 deletions botocore/compress.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def _is_compressible_type(request_dict):
if isinstance(body, dict):
body = urlencode(body, doseq=True, encoding='utf-8').encode('utf-8')
request_dict['body'] = body
is_supported_type = isinstance(body, (str, bytes, bytearray))
is_supported_type = isinstance(body, (str, bytes, bytearray, memoryview))
return is_supported_type or hasattr(body, 'read')


Expand All @@ -90,7 +90,7 @@ def _get_body_size(body):
def _gzip_compress_body(body):
if isinstance(body, str):
return gzip_compress(body.encode('utf-8'))
elif isinstance(body, (bytes, bytearray)):
elif isinstance(body, (bytes, bytearray, memoryview)):
return gzip_compress(body)
elif hasattr(body, 'read'):
if hasattr(body, 'seek') and hasattr(body, 'tell'):
Expand Down
2 changes: 1 addition & 1 deletion botocore/httpchecksum.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def _handle_fileobj(self, fileobj):
fileobj.seek(start_position)

def handle(self, body):
if isinstance(body, (bytes, bytearray)):
if isinstance(body, (bytes, bytearray, memoryview)):
self.update(body)
else:
self._handle_fileobj(body)
Expand Down
2 changes: 1 addition & 1 deletion botocore/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3239,7 +3239,7 @@ def get_encoding_from_headers(headers, default='ISO-8859-1'):


def calculate_md5(body, **kwargs):
if isinstance(body, (bytes, bytearray)):
if isinstance(body, (bytes, bytearray, memoryview)):
binary_md5 = _calculate_md5_from_bytes(body)
else:
binary_md5 = _calculate_md5_from_file(body)
Expand Down
10 changes: 8 additions & 2 deletions botocore/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ def _validate_integer(self, param, shape, errors, name):
range_check(name, param, shape, 'invalid range', errors)

def _validate_blob(self, param, shape, errors, name):
if isinstance(param, (bytes, bytearray, str)):
if isinstance(param, (bytes, bytearray, str, memoryview)):
return
elif hasattr(param, 'read'):
# File like objects are also allowed for blob types.
Expand All @@ -328,7 +328,13 @@ def _validate_blob(self, param, shape, errors, name):
name,
'invalid type',
param=param,
valid_types=[str(bytes), str(bytearray), 'file-like object'],
valid_types=[
str(bytes),
str(bytearray),
str(str),
str(memoryview),
'file-like object',
],
)

@type_check(valid_types=(bool,))
Expand Down
5 changes: 5 additions & 0 deletions tests/integration/test_s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,11 @@ def test_can_put_object_bytearray(self):
body = bytearray(body_bytes)
self.assert_can_put_object(body)

def test_can_put_object_memoryview(self):
body_bytes = b'*' * 1024
body = memoryview(body_bytes)
self.assert_can_put_object(body)

def test_get_object_stream_wrapper(self):
self.create_object('foobarbaz', body='body contents')
response = self.client.get_object(
Expand Down
7 changes: 7 additions & 0 deletions tests/unit/test_awsrequest.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,13 @@ def test_can_reset_stream_handles_bytearray(self):
# assert the request body doesn't change after reset_stream is called
self.assertEqual(self.prepared_request.body, contents)

def test_can_reset_stream_handles_memoryview(self):
contents = memoryview(b'notastream')
self.prepared_request.body = contents
self.prepared_request.reset_stream()
# assert the request body doesn't change after reset_stream is called
self.assertEqual(self.prepared_request.body, contents)

def test_can_reset_stream(self):
contents = b'foobarbaz'
with open(self.filename, 'wb') as f:
Expand Down
4 changes: 4 additions & 0 deletions tests/unit/test_compress.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ def assert_request_compressed(request_dict, expected_body):
_request_dict(bytearray(REQUEST_BODY)),
OP_WITH_COMPRESSION,
),
(
_request_dict(memoryview(REQUEST_BODY)),
OP_WITH_COMPRESSION,
),
(
_request_dict(headers={'Content-Encoding': 'identity'}),
OP_WITH_COMPRESSION,
Expand Down
8 changes: 8 additions & 0 deletions tests/unit/test_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1391,6 +1391,14 @@ def test_add_md5_with_bytearray_object(self):
request_dict['headers']['Content-MD5'], 'OFj2IjCsPJFfMAxmQxLGPw=='
)

def test_add_md5_with_memoryview_object(self):
request_dict = {'body': memoryview(b'foobar'), 'headers': {}}
self.md5_digest.return_value = b'8X\xf6"0\xac<\x91_0\x0cfC\x12\xc6?'
conditionally_calculate_md5(request_dict)
self.assertEqual(
request_dict['headers']['Content-MD5'], 'OFj2IjCsPJFfMAxmQxLGPw=='
)

def test_skip_md5_when_flexible_checksum_context(self):
request_dict = {
'body': io.BytesIO(b'foobar'),
Expand Down
24 changes: 24 additions & 0 deletions tests/unit/test_httpchecksum.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,30 @@ def test_apply_request_checksum_flex_header_bytes(self):
apply_request_checksum(request)
self.assertIn("x-amz-checksum-crc32", request["headers"])

def test_apply_request_checksum_flex_header_bytearray(self):
request = self._build_request(bytearray(b""))
request["context"]["checksum"] = {
"request_algorithm": {
"in": "header",
"algorithm": "crc32",
"name": "x-amz-checksum-crc32",
}
}
apply_request_checksum(request)
self.assertIn("x-amz-checksum-crc32", request["headers"])

def test_apply_request_checksum_flex_header_memoryview(self):
request = self._build_request(memoryview(b""))
request["context"]["checksum"] = {
"request_algorithm": {
"in": "header",
"algorithm": "crc32",
"name": "x-amz-checksum-crc32",
}
}
apply_request_checksum(request)
self.assertIn("x-amz-checksum-crc32", request["headers"])

def test_apply_request_checksum_flex_header_readable(self):
request = self._build_request(BytesIO(b""))
request["context"]["checksum"] = {
Expand Down
8 changes: 8 additions & 0 deletions tests/unit/test_validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,14 @@ def test_validates_bytearray(self):
error_msg = errors.generate_report()
self.assertEqual(error_msg, '')

def test_validates_memoryview(self):
errors = self.get_validation_error_message(
given_shapes=self.shapes,
input_params={'Blob': memoryview(b'12345')},
)
error_msg = errors.generate_report()
self.assertEqual(error_msg, '')

def test_validates_file_like_object(self):
value = io.BytesIO(b'foo')

Expand Down