mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-05-08 06:12:51 +00:00
Move uses of to_bytes, to_text, to_native to use the module_utils version (#17423)
We couldn't copy to_unicode, to_bytes, to_str into module_utils because of licensing. So once created it we had two sets of functions that did the same things but had different implementations. To remedy that, this change removes the ansible.utils.unicode versions of those functions.
This commit is contained in:
@@ -32,16 +32,18 @@
|
||||
making backwards compatibility guarantees. The API may change between
|
||||
releases. Do not use this unless you are willing to port your module code.
|
||||
"""
|
||||
import codecs
|
||||
|
||||
from ansible.module_utils.six import PY3, text_type, binary_type
|
||||
|
||||
import codecs
|
||||
|
||||
try:
|
||||
codecs.lookup_error('surrogateescape')
|
||||
HAS_SURROGATEESCAPE = True
|
||||
except LookupError:
|
||||
HAS_SURROGATEESCAPE = False
|
||||
|
||||
|
||||
def to_bytes(obj, encoding='utf-8', errors=None, nonstring='simplerepr'):
|
||||
"""Make sure that a string is a byte string
|
||||
|
||||
@@ -109,7 +111,14 @@ def to_bytes(obj, encoding='utf-8', errors=None, nonstring='simplerepr'):
|
||||
# Note: We do these last even though we have to call to_bytes again on the
|
||||
# value because we're optimizing the common case
|
||||
if nonstring == 'simplerepr':
|
||||
value = str(obj)
|
||||
try:
|
||||
value = str(obj)
|
||||
except UnicodeError:
|
||||
try:
|
||||
value = repr(obj)
|
||||
except UnicodeError:
|
||||
# Giving up
|
||||
return to_bytes('')
|
||||
elif nonstring == 'passthru':
|
||||
return obj
|
||||
elif nonstring == 'empty':
|
||||
@@ -122,6 +131,7 @@ def to_bytes(obj, encoding='utf-8', errors=None, nonstring='simplerepr'):
|
||||
|
||||
return to_bytes(value, encoding, errors)
|
||||
|
||||
|
||||
def to_text(obj, encoding='utf-8', errors=None, nonstring='simplerepr'):
|
||||
"""Make sure that a string is a text string
|
||||
|
||||
@@ -175,7 +185,14 @@ def to_text(obj, encoding='utf-8', errors=None, nonstring='simplerepr'):
|
||||
# Note: We do these last even though we have to call to_text again on the
|
||||
# value because we're optimizing the common case
|
||||
if nonstring == 'simplerepr':
|
||||
value = str(obj)
|
||||
try:
|
||||
value = str(obj)
|
||||
except UnicodeError:
|
||||
try:
|
||||
value = repr(obj)
|
||||
except UnicodeError:
|
||||
# Giving up
|
||||
return u''
|
||||
elif nonstring == 'passthru':
|
||||
return obj
|
||||
elif nonstring == 'empty':
|
||||
@@ -187,6 +204,7 @@ def to_text(obj, encoding='utf-8', errors=None, nonstring='simplerepr'):
|
||||
|
||||
return to_text(value, encoding, errors)
|
||||
|
||||
|
||||
#: :py:func:`to_native`
|
||||
#: Transform a variable into the native str type for the python version
|
||||
#:
|
||||
|
||||
@@ -805,7 +805,7 @@ class AnsibleModule(object):
|
||||
if not HAVE_SELINUX or not self.selinux_enabled():
|
||||
return context
|
||||
try:
|
||||
ret = selinux.matchpathcon(to_native(path, errors='strict'), mode)
|
||||
ret = selinux.matchpathcon(to_native(path, errors='surrogate_or_strict'), mode)
|
||||
except OSError:
|
||||
return context
|
||||
if ret[0] == -1:
|
||||
@@ -820,7 +820,7 @@ class AnsibleModule(object):
|
||||
if not HAVE_SELINUX or not self.selinux_enabled():
|
||||
return context
|
||||
try:
|
||||
ret = selinux.lgetfilecon_raw(to_native(path, errors='strict'))
|
||||
ret = selinux.lgetfilecon_raw(to_native(path, errors='surrogate_or_strict'))
|
||||
except OSError:
|
||||
e = get_exception()
|
||||
if e.errno == errno.ENOENT:
|
||||
@@ -2121,10 +2121,10 @@ class AnsibleModule(object):
|
||||
to_clean_args = args
|
||||
if PY2:
|
||||
if isinstance(args, text_type):
|
||||
to_clean_args = args.encode('utf-8')
|
||||
to_clean_args = to_bytes(args)
|
||||
else:
|
||||
if isinstance(args, binary_type):
|
||||
to_clean_args = args.decode('utf-8', errors='replace')
|
||||
to_clean_args = to_text(args)
|
||||
if isinstance(args, (text_type, binary_type)):
|
||||
to_clean_args = shlex.split(to_clean_args)
|
||||
|
||||
@@ -2193,11 +2193,7 @@ class AnsibleModule(object):
|
||||
if not binary_data:
|
||||
data += '\n'
|
||||
if isinstance(data, text_type):
|
||||
if PY3:
|
||||
errors = 'surrogateescape'
|
||||
else:
|
||||
errors = 'strict'
|
||||
data = data.encode('utf-8', errors=errors)
|
||||
data = to_bytes(data)
|
||||
cmd.stdin.write(data)
|
||||
cmd.stdin.close()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user