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:
parent
f3fed01a7e
commit
3e25f633fe
3 changed files with 20 additions and 12 deletions
|
@ -19,7 +19,7 @@
|
||||||
from __future__ import (absolute_import, division, print_function)
|
from __future__ import (absolute_import, division, print_function)
|
||||||
__metaclass__ = type
|
__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
|
# to_bytes and to_unicode were written by Toshio Kuratomi for the
|
||||||
# python-kitchen library https://pypi.python.org/pypi/kitchen
|
# 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
|
# 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):
|
def to_unicode(obj, encoding='utf-8', errors='replace', nonstring=None):
|
||||||
'''Convert an object into a :class:`unicode` string
|
'''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
|
# Could use isbasestring/isunicode here but we want this code to be as
|
||||||
# fast as possible
|
# fast as possible
|
||||||
if isinstance(obj, (string_types, text_type)):
|
if isinstance(obj, basestring):
|
||||||
if isinstance(obj, text_type):
|
if isinstance(obj, text_type):
|
||||||
return obj
|
return obj
|
||||||
if encoding in _UTF8_ALIASES:
|
if encoding in _UTF8_ALIASES:
|
||||||
|
@ -112,7 +115,7 @@ def to_unicode(obj, encoding='utf-8', errors='replace', nonstring=None):
|
||||||
simple = None
|
simple = None
|
||||||
if not simple:
|
if not simple:
|
||||||
try:
|
try:
|
||||||
simple = str(obj)
|
simple = text_type(obj)
|
||||||
except UnicodeError:
|
except UnicodeError:
|
||||||
try:
|
try:
|
||||||
simple = obj.__str__()
|
simple = obj.__str__()
|
||||||
|
@ -123,7 +126,7 @@ def to_unicode(obj, encoding='utf-8', errors='replace', nonstring=None):
|
||||||
return simple
|
return simple
|
||||||
elif nonstring in ('repr', 'strict'):
|
elif nonstring in ('repr', 'strict'):
|
||||||
obj_repr = repr(obj)
|
obj_repr = repr(obj)
|
||||||
if isinstance(obj_repr, str):
|
if isinstance(obj_repr, binary_type):
|
||||||
obj_repr = text_type(obj_repr, encoding, errors)
|
obj_repr = text_type(obj_repr, encoding, errors)
|
||||||
if nonstring == 'repr':
|
if nonstring == 'repr':
|
||||||
return obj_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
|
# Could use isbasestring, isbytestring here but we want this to be as fast
|
||||||
# as possible
|
# as possible
|
||||||
if isinstance(obj, (string_types, text_type)):
|
if isinstance(obj, basestring):
|
||||||
if isinstance(obj, str):
|
if isinstance(obj, binary_type):
|
||||||
return obj
|
return obj
|
||||||
return obj.encode(encoding, errors)
|
return obj.encode(encoding, errors)
|
||||||
if not nonstring:
|
if not nonstring:
|
||||||
nonstring = 'simplerepr'
|
nonstring = 'simplerepr'
|
||||||
|
|
||||||
if nonstring == 'empty':
|
if nonstring == 'empty':
|
||||||
return ''
|
return b''
|
||||||
elif nonstring == 'passthru':
|
elif nonstring == 'passthru':
|
||||||
return obj
|
return obj
|
||||||
elif nonstring == 'simplerepr':
|
elif nonstring == 'simplerepr':
|
||||||
|
@ -222,7 +225,7 @@ def to_bytes(obj, encoding='utf-8', errors='replace', nonstring=None):
|
||||||
try:
|
try:
|
||||||
simple = obj.__unicode__()
|
simple = obj.__unicode__()
|
||||||
except (AttributeError, UnicodeError):
|
except (AttributeError, UnicodeError):
|
||||||
simple = ''
|
simple = b''
|
||||||
if isinstance(simple, text_type):
|
if isinstance(simple, text_type):
|
||||||
simple = simple.encode(encoding, 'replace')
|
simple = simple.encode(encoding, 'replace')
|
||||||
return simple
|
return simple
|
||||||
|
@ -230,7 +233,7 @@ def to_bytes(obj, encoding='utf-8', errors='replace', nonstring=None):
|
||||||
try:
|
try:
|
||||||
obj_repr = obj.__repr__()
|
obj_repr = obj.__repr__()
|
||||||
except (AttributeError, UnicodeError):
|
except (AttributeError, UnicodeError):
|
||||||
obj_repr = ''
|
obj_repr = b''
|
||||||
if isinstance(obj_repr, text_type):
|
if isinstance(obj_repr, text_type):
|
||||||
obj_repr = obj_repr.encode(encoding, errors)
|
obj_repr = obj_repr.encode(encoding, errors)
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -19,7 +19,7 @@
|
||||||
from __future__ import (absolute_import, division, print_function)
|
from __future__ import (absolute_import, division, print_function)
|
||||||
__metaclass__ = type
|
__metaclass__ = type
|
||||||
|
|
||||||
from six.moves import builtins
|
from six import PY2
|
||||||
from yaml.scanner import ScannerError
|
from yaml.scanner import ScannerError
|
||||||
|
|
||||||
from ansible.compat.tests import unittest
|
from ansible.compat.tests import unittest
|
||||||
|
@ -80,6 +80,11 @@ class TestDataLoaderWithVault(unittest.TestCase):
|
||||||
3135306561356164310a343937653834643433343734653137383339323330626437313562306630
|
3135306561356164310a343937653834643433343734653137383339323330626437313562306630
|
||||||
3035
|
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')
|
output = self._loader.load_from_file('dummy_vault.txt')
|
||||||
self.assertEqual(output, dict(foo='bar'))
|
self.assertEqual(output, dict(foo='bar'))
|
||||||
|
|
|
@ -132,7 +132,7 @@ class TestVaultEditor(unittest.TestCase):
|
||||||
# verify decrypted content
|
# verify decrypted content
|
||||||
f = open(v10_file.name, "rb")
|
f = open(v10_file.name, "rb")
|
||||||
fdata = to_unicode(f.read())
|
fdata = to_unicode(f.read())
|
||||||
f.cloes()
|
f.close()
|
||||||
|
|
||||||
os.unlink(v10_file.name)
|
os.unlink(v10_file.name)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue