From 1d37145c24c01e99409e59d470ec902382aac41f Mon Sep 17 00:00:00 2001 From: annatisch Date: Thu, 12 May 2016 09:27:49 -0700 Subject: [PATCH] removed base64 encoding/decoding --- .../Python.Tests/AcceptanceTests/array_tests.py | 4 +++- .../AcceptanceTests/dictionary_tests.py | 6 +++--- .../Python.Tests/AcceptanceTests/string_tests.py | 6 +++--- .../Python.Tests/AcceptanceTests/url_tests.py | 2 +- .../Python/msrest/msrest/serialization.py | 13 ++++++++----- 5 files changed, 18 insertions(+), 13 deletions(-) diff --git a/AutoRest/Generators/Python/Python.Tests/AcceptanceTests/array_tests.py b/AutoRest/Generators/Python/Python.Tests/AcceptanceTests/array_tests.py index 0b9a56ff2c9f8..c919eeb450ebe 100644 --- a/AutoRest/Generators/Python/Python.Tests/AcceptanceTests/array_tests.py +++ b/AutoRest/Generators/Python/Python.Tests/AcceptanceTests/array_tests.py @@ -208,7 +208,9 @@ def test_array(self): with self.assertRaises(DeserializationError): client.array.get_date_time_invalid_chars() - test_array = ['a string that gets encoded with base64url', 'test string', 'Lorem ipsum'] + test_array = ['a string that gets encoded with base64url'.encode(), + 'test string'.encode(), + 'Lorem ipsum'.encode()] self.assertEqual(client.array.get_base64_url(), test_array) diff --git a/AutoRest/Generators/Python/Python.Tests/AcceptanceTests/dictionary_tests.py b/AutoRest/Generators/Python/Python.Tests/AcceptanceTests/dictionary_tests.py index a3dc4cef36f45..477591ffa9f4f 100644 --- a/AutoRest/Generators/Python/Python.Tests/AcceptanceTests/dictionary_tests.py +++ b/AutoRest/Generators/Python/Python.Tests/AcceptanceTests/dictionary_tests.py @@ -182,9 +182,9 @@ def test_dictionary_primitive_types(self): bytes_result = self.client.dictionary.get_byte_invalid_null() self.assertEqual(bytes_null, bytes_result) - test_dict = {'0': 'a string that gets encoded with base64url', - '1': 'test string', - '2': 'Lorem ipsum'} + test_dict = {'0': 'a string that gets encoded with base64url'.encode(), + '1': 'test string'.encode(), + '2': 'Lorem ipsum'.encode()} self.assertEqual(self.client.dictionary.get_base64_url(), test_dict) def test_basic_dictionary_parsing(self): diff --git a/AutoRest/Generators/Python/Python.Tests/AcceptanceTests/string_tests.py b/AutoRest/Generators/Python/Python.Tests/AcceptanceTests/string_tests.py index c012b3e9407cd..93c4d937535cc 100644 --- a/AutoRest/Generators/Python/Python.Tests/AcceptanceTests/string_tests.py +++ b/AutoRest/Generators/Python/Python.Tests/AcceptanceTests/string_tests.py @@ -104,10 +104,10 @@ def test_string(self): self.assertEqual(Colors.redcolor, client.enum.get_not_expandable()) client.enum.put_not_expandable(Colors.redcolor) - self.assertEqual(client.string.get_base64_encoded(), 'a string that gets encoded with base64') - self.assertEqual(client.string.get_base64_url_encoded(), 'a string that gets encoded with base64url') + self.assertEqual(client.string.get_base64_encoded(), 'a string that gets encoded with base64'.encode()) + self.assertEqual(client.string.get_base64_url_encoded(), 'a string that gets encoded with base64url'.encode()) self.assertIsNone(client.string.get_null_base64_url_encoded()) - client.string.put_base64_url_encoded('a string that gets encoded with base64url') + client.string.put_base64_url_encoded('a string that gets encoded with base64url'.encode()) if __name__ == '__main__': diff --git a/AutoRest/Generators/Python/Python.Tests/AcceptanceTests/url_tests.py b/AutoRest/Generators/Python/Python.Tests/AcceptanceTests/url_tests.py index 9eee73e422f22..04e4c94e359a4 100644 --- a/AutoRest/Generators/Python/Python.Tests/AcceptanceTests/url_tests.py +++ b/AutoRest/Generators/Python/Python.Tests/AcceptanceTests/url_tests.py @@ -101,7 +101,7 @@ def test_url_path(self): with self.assertRaises(ValidationError): self.client.paths.enum_null(None) - self.client.paths.base64_url("lorem") + self.client.paths.base64_url("lorem".encode()) def test_url_query(self): diff --git a/ClientRuntimes/Python/msrest/msrest/serialization.py b/ClientRuntimes/Python/msrest/msrest/serialization.py index ef16b011e4b7d..b2c7e67ca57ac 100644 --- a/ClientRuntimes/Python/msrest/msrest/serialization.py +++ b/ClientRuntimes/Python/msrest/msrest/serialization.py @@ -447,6 +447,10 @@ def serialize_unicode(self, data): :param data: Object to be serialized. :rtype: str """ + try: + return data.value + except AttributeError: + pass try: if isinstance(data, unicode): return data.encode(encoding='utf-8') @@ -549,7 +553,7 @@ def serialize_base64(attr, **kwargs): :param attr: Object to be serialized. :rtype: str """ - encoded = b64encode(attr.encode()).decode() + encoded = b64encode(attr).decode('ascii') return encoded.strip('=').replace('+', '-').replace('/', '_') @staticmethod @@ -1005,11 +1009,10 @@ def deserialize_base64(attr): :rtype: bytearray :raises: TypeError if string format invalid. """ - pad_count = 3 - (len(attr) + 3) % 4 - padding = ['='] * pad_count - attr = attr + ''.join(padding) + padding = '=' * (3 - (len(attr) + 3) % 4) + attr = attr + padding encoded = attr.replace('-', '+').replace('_', '/') - return b64decode(encoded).decode() + return b64decode(encoded) @staticmethod def deserialize_decimal(attr):