From 3e25f633fe3d2c6ea9a89c0f2d41f009752aa404 Mon Sep 17 00:00:00 2001 From: Rory Finnegan Date: Thu, 16 Apr 2015 16:01:13 -0400 Subject: [PATCH] Applied some stashed fixes. * Fixed file.close() typo in test_vault_editor * Updated unicode.py to redefine basestring properly in python3 and fixed a couple missed py27 specific code. * Realized the patch in test_data_loader was still failing cause we are passing the string 'builtins.open' and not actually using it in that file and soe instead of failing in py34 it would fail in py27. --- v2/ansible/utils/unicode.py | 21 ++++++++++++--------- v2/test/parsing/test_data_loader.py | 9 +++++++-- v2/test/parsing/vault/test_vault_editor.py | 2 +- 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/v2/ansible/utils/unicode.py b/v2/ansible/utils/unicode.py index e6f43d799c2..2cff2e5e45c 100644 --- a/v2/ansible/utils/unicode.py +++ b/v2/ansible/utils/unicode.py @@ -19,7 +19,7 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type -from six import string_types, text_type, binary_type +from six import string_types, text_type, binary_type, PY3 # to_bytes and to_unicode were written by Toshio Kuratomi for the # python-kitchen library https://pypi.python.org/pypi/kitchen @@ -37,6 +37,9 @@ _LATIN1_ALIASES = frozenset(('latin-1', 'LATIN-1', 'latin1', 'LATIN1', # EXCEPTION_CONVERTERS is defined below due to using to_unicode +if PY3: + basestring = (str, bytes) + def to_unicode(obj, encoding='utf-8', errors='replace', nonstring=None): '''Convert an object into a :class:`unicode` string @@ -90,7 +93,7 @@ def to_unicode(obj, encoding='utf-8', errors='replace', nonstring=None): ''' # Could use isbasestring/isunicode here but we want this code to be as # fast as possible - if isinstance(obj, (string_types, text_type)): + if isinstance(obj, basestring): if isinstance(obj, text_type): return obj if encoding in _UTF8_ALIASES: @@ -112,7 +115,7 @@ def to_unicode(obj, encoding='utf-8', errors='replace', nonstring=None): simple = None if not simple: try: - simple = str(obj) + simple = text_type(obj) except UnicodeError: try: simple = obj.__str__() @@ -123,7 +126,7 @@ def to_unicode(obj, encoding='utf-8', errors='replace', nonstring=None): return simple elif nonstring in ('repr', 'strict'): obj_repr = repr(obj) - if isinstance(obj_repr, str): + if isinstance(obj_repr, binary_type): obj_repr = text_type(obj_repr, encoding, errors) if nonstring == 'repr': return obj_repr @@ -199,15 +202,15 @@ def to_bytes(obj, encoding='utf-8', errors='replace', nonstring=None): ''' # Could use isbasestring, isbytestring here but we want this to be as fast # as possible - if isinstance(obj, (string_types, text_type)): - if isinstance(obj, str): + if isinstance(obj, basestring): + if isinstance(obj, binary_type): return obj return obj.encode(encoding, errors) if not nonstring: nonstring = 'simplerepr' if nonstring == 'empty': - return '' + return b'' elif nonstring == 'passthru': return obj elif nonstring == 'simplerepr': @@ -222,7 +225,7 @@ def to_bytes(obj, encoding='utf-8', errors='replace', nonstring=None): try: simple = obj.__unicode__() except (AttributeError, UnicodeError): - simple = '' + simple = b'' if isinstance(simple, text_type): simple = simple.encode(encoding, 'replace') return simple @@ -230,7 +233,7 @@ def to_bytes(obj, encoding='utf-8', errors='replace', nonstring=None): try: obj_repr = obj.__repr__() except (AttributeError, UnicodeError): - obj_repr = '' + obj_repr = b'' if isinstance(obj_repr, text_type): obj_repr = obj_repr.encode(encoding, errors) else: diff --git a/v2/test/parsing/test_data_loader.py b/v2/test/parsing/test_data_loader.py index 5117150b4fe..b9c37cdd0c7 100644 --- a/v2/test/parsing/test_data_loader.py +++ b/v2/test/parsing/test_data_loader.py @@ -19,7 +19,7 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type -from six.moves import builtins +from six import PY2 from yaml.scanner import ScannerError from ansible.compat.tests import unittest @@ -80,6 +80,11 @@ class TestDataLoaderWithVault(unittest.TestCase): 3135306561356164310a343937653834643433343734653137383339323330626437313562306630 3035 """ - with patch('builtins.open', mock_open(read_data=vaulted_data)): + if PY2: + builtins_name = '__builtin__' + else: + builtins_name = 'builtins' + + with patch(builtins_name + '.open', mock_open(read_data=vaulted_data)): output = self._loader.load_from_file('dummy_vault.txt') self.assertEqual(output, dict(foo='bar')) diff --git a/v2/test/parsing/vault/test_vault_editor.py b/v2/test/parsing/vault/test_vault_editor.py index fd52ca2490e..2ddf3de27a2 100644 --- a/v2/test/parsing/vault/test_vault_editor.py +++ b/v2/test/parsing/vault/test_vault_editor.py @@ -132,7 +132,7 @@ class TestVaultEditor(unittest.TestCase): # verify decrypted content f = open(v10_file.name, "rb") fdata = to_unicode(f.read()) - f.cloes() + f.close() os.unlink(v10_file.name)