mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-25 14:20:22 -07: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:
parent
7a9395b5e0
commit
4ed88512e4
89 changed files with 759 additions and 894 deletions
|
@ -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
|
||||
#:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue