Skip to content

Commit 8b9d397

Browse files
authored
allow duck-typing of source streams (#77)
1 parent 986dbb7 commit 8b9d397

File tree

3 files changed

+23
-21
lines changed

3 files changed

+23
-21
lines changed

CHANGELOG.rst

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22
Changelog
33
*********
44

5-
1.3.6 -- 2018-08-29
5+
1.3.6 -- 2018-08-xx
66
===================
77

88
Bugfixes
99
--------
1010
* :class:`StreamEncryptor` and :class:`StreamDecryptor` should always report as readable if they are open.
1111
`#73 <https://github.com/aws/aws-encryption-sdk-python/issues/73>`_
12+
* Allow duck-typing of source streams.
13+
`#75 <https://github.com/aws/aws-encryption-sdk-python/issues/75>`_
1214

1315
1.3.5 -- 2018-08-01
1416
===================

src/aws_encryption_sdk/internal/utils/__init__.py

+5-12
Original file line numberDiff line numberDiff line change
@@ -127,24 +127,17 @@ def prepare_data_keys(primary_master_key, master_keys, algorithm, encryption_con
127127
return data_encryption_key, encrypted_data_keys
128128

129129

130-
try:
131-
_FILE_TYPE = file # Python 2
132-
except NameError:
133-
_FILE_TYPE = io.IOBase # Python 3 pylint: disable=invalid-name
134-
135-
136130
def prep_stream_data(data):
137-
"""Takes an input str, bytes, io.IOBase, or file object and returns an appropriate
138-
stream for _EncryptionStream objects.
131+
"""Take an input and prepare it for use as a stream.
139132
140133
:param data: Input data
141-
:type data: str, bytes, io.IOBase, or file
142134
:returns: Prepared stream
143135
:rtype: io.BytesIO
144136
"""
145-
if isinstance(data, (_FILE_TYPE, io.IOBase, six.StringIO)):
146-
return data
147-
return io.BytesIO(to_bytes(data))
137+
if isinstance(data, (six.string_types, six.binary_type)):
138+
return io.BytesIO(to_bytes(data))
139+
140+
return data
148141

149142

150143
def source_data_key_length_check(source_data_key, algorithm):

test/unit/test_utils.py

+15-8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# coding: utf-8
12
# Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
23
#
34
# Licensed under the Apache License, Version 2.0 (the "License"). You
@@ -11,6 +12,7 @@
1112
# ANY KIND, either express or implied. See the License for the specific
1213
# language governing permissions and limitations under the License.
1314
"""Test suite for aws_encryption_sdk.internal.utils"""
15+
import io
1416
import unittest
1517

1618
import pytest
@@ -28,6 +30,19 @@
2830
pytestmark = [pytest.mark.unit, pytest.mark.local]
2931

3032

33+
def test_prep_stream_data_passthrough():
34+
test = aws_encryption_sdk.internal.utils.prep_stream_data(sentinel.not_a_string_or_bytes)
35+
36+
assert test is sentinel.not_a_string_or_bytes
37+
38+
39+
@pytest.mark.parametrize("source", (u"some unicode data ловие", b"\x00\x01\x02"))
40+
def test_prep_stream_data_wrap(source):
41+
test = aws_encryption_sdk.internal.utils.prep_stream_data(source)
42+
43+
assert isinstance(test, io.BytesIO)
44+
45+
3146
class TestUtils(unittest.TestCase):
3247
def setUp(self):
3348
# Set up mock key provider and keys
@@ -235,14 +250,6 @@ def test_prepare_data_keys(self):
235250
[mock_encrypted_data_encryption_key, sentinel.encrypted_data_key_1, sentinel.encrypted_data_key_2]
236251
)
237252

238-
@patch("aws_encryption_sdk.internal.utils.to_bytes", return_value=sentinel.bytes)
239-
@patch("aws_encryption_sdk.internal.utils.io.BytesIO", return_value=sentinel.bytesio)
240-
def test_prep_stream_data(self, mock_bytesio, mock_to_bytes):
241-
test = aws_encryption_sdk.internal.utils.prep_stream_data(sentinel.data)
242-
mock_to_bytes.assert_called_once_with(sentinel.data)
243-
mock_bytesio.assert_called_once_with(sentinel.bytes)
244-
assert test is sentinel.bytesio
245-
246253
def test_source_data_key_length_check_valid(self):
247254
mock_algorithm = MagicMock()
248255
mock_algorithm.kdf_input_len = 5

0 commit comments

Comments
 (0)