Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bring back more objects that were exposes in _text #69090

Merged
merged 1 commit into from
Apr 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/ansible/module_utils/_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@
"""

# Backwards compat for people still calling it from this package
import codecs

from ansible.module_utils.six import PY3, text_type, binary_type

from ansible.module_utils.common.text.converters import to_bytes, to_native, to_text
126 changes: 63 additions & 63 deletions lib/ansible/module_utils/common/text/converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,69 +30,6 @@
'surrogate_then_replace'))


def _json_encode_fallback(obj):
if isinstance(obj, Set):
return list(obj)
elif isinstance(obj, datetime.datetime):
return obj.isoformat()
raise TypeError("Cannot json serialize %s" % to_native(obj))


def jsonify(data, **kwargs):
for encoding in ("utf-8", "latin-1"):
try:
return json.dumps(data, encoding=encoding, default=_json_encode_fallback, **kwargs)
# Old systems using old simplejson module does not support encoding keyword.
except TypeError:
try:
new_data = container_to_text(data, encoding=encoding)
except UnicodeDecodeError:
continue
return json.dumps(new_data, default=_json_encode_fallback, **kwargs)
except UnicodeDecodeError:
continue
raise UnicodeError('Invalid unicode encoding encountered')


def container_to_bytes(d, encoding='utf-8', errors='surrogate_or_strict'):
''' Recursively convert dict keys and values to byte str

Specialized for json return because this only handles, lists, tuples,
and dict container types (the containers that the json module returns)
'''

if isinstance(d, text_type):
return to_bytes(d, encoding=encoding, errors=errors)
elif isinstance(d, dict):
return dict(container_to_bytes(o, encoding, errors) for o in iteritems(d))
elif isinstance(d, list):
return [container_to_bytes(o, encoding, errors) for o in d]
elif isinstance(d, tuple):
return tuple(container_to_bytes(o, encoding, errors) for o in d)
else:
return d


def container_to_text(d, encoding='utf-8', errors='surrogate_or_strict'):
"""Recursively convert dict keys and values to byte str

Specialized for json return because this only handles, lists, tuples,
and dict container types (the containers that the json module returns)
"""

if isinstance(d, binary_type):
# Warning, can traceback
return to_text(d, encoding=encoding, errors=errors)
elif isinstance(d, dict):
return dict(container_to_text(o, encoding, errors) for o in iteritems(d))
elif isinstance(d, list):
return [container_to_text(o, encoding, errors) for o in d]
elif isinstance(d, tuple):
return tuple(container_to_text(o, encoding, errors) for o in d)
else:
return d


def to_bytes(obj, encoding='utf-8', errors=None, nonstring='simplerepr'):
"""Make sure that a string is a byte string

Expand Down Expand Up @@ -320,3 +257,66 @@ def to_text(obj, encoding='utf-8', errors=None, nonstring='simplerepr'):
to_native = to_text
else:
to_native = to_bytes


def _json_encode_fallback(obj):
if isinstance(obj, Set):
return list(obj)
elif isinstance(obj, datetime.datetime):
return obj.isoformat()
raise TypeError("Cannot json serialize %s" % to_native(obj))


def jsonify(data, **kwargs):
for encoding in ("utf-8", "latin-1"):
try:
return json.dumps(data, encoding=encoding, default=_json_encode_fallback, **kwargs)
# Old systems using old simplejson module does not support encoding keyword.
except TypeError:
try:
new_data = container_to_text(data, encoding=encoding)
except UnicodeDecodeError:
continue
return json.dumps(new_data, default=_json_encode_fallback, **kwargs)
except UnicodeDecodeError:
continue
raise UnicodeError('Invalid unicode encoding encountered')


def container_to_bytes(d, encoding='utf-8', errors='surrogate_or_strict'):
''' Recursively convert dict keys and values to byte str

Specialized for json return because this only handles, lists, tuples,
and dict container types (the containers that the json module returns)
'''

if isinstance(d, text_type):
return to_bytes(d, encoding=encoding, errors=errors)
elif isinstance(d, dict):
return dict(container_to_bytes(o, encoding, errors) for o in iteritems(d))
elif isinstance(d, list):
return [container_to_bytes(o, encoding, errors) for o in d]
elif isinstance(d, tuple):
return tuple(container_to_bytes(o, encoding, errors) for o in d)
else:
return d


def container_to_text(d, encoding='utf-8', errors='surrogate_or_strict'):
"""Recursively convert dict keys and values to byte str

Specialized for json return because this only handles, lists, tuples,
and dict container types (the containers that the json module returns)
"""

if isinstance(d, binary_type):
# Warning, can traceback
return to_text(d, encoding=encoding, errors=errors)
elif isinstance(d, dict):
return dict(container_to_text(o, encoding, errors) for o in iteritems(d))
elif isinstance(d, list):
return [container_to_text(o, encoding, errors) for o in d]
elif isinstance(d, tuple):
return tuple(container_to_text(o, encoding, errors) for o in d)
else:
return d