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.
This commit is contained in:
Rory Finnegan 2015-04-16 16:01:13 -04:00
parent f3fed01a7e
commit 3e25f633fe
3 changed files with 20 additions and 12 deletions

View file

@ -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:

View file

@ -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'))

View file

@ -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)