mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Simplify UnsafeProxy as we don't need to use it for byte strings, only text
This commit is contained in:
parent
3c87c44af5
commit
99e7bb35c1
1 changed files with 15 additions and 33 deletions
|
@ -53,46 +53,28 @@
|
||||||
from __future__ import (absolute_import, division, print_function)
|
from __future__ import (absolute_import, division, print_function)
|
||||||
__metaclass__ = type
|
__metaclass__ = type
|
||||||
|
|
||||||
|
from ansible.utils.unicode import to_unicode
|
||||||
|
from ansible.compat.six import string_types, text_type
|
||||||
|
|
||||||
__all__ = ['UnsafeProxy', 'AnsibleUnsafe', 'wrap_var']
|
__all__ = ['UnsafeProxy', 'AnsibleUnsafe', 'wrap_var']
|
||||||
|
|
||||||
|
|
||||||
class AnsibleUnsafe(object):
|
class AnsibleUnsafe(object):
|
||||||
__UNSAFE__ = True
|
__UNSAFE__ = True
|
||||||
|
|
||||||
try:
|
class AnsibleUnsafeText(text_type, AnsibleUnsafe):
|
||||||
unicode
|
pass
|
||||||
except NameError:
|
|
||||||
# Python 3
|
|
||||||
class AnsibleUnsafeBytes(bytes, AnsibleUnsafe):
|
|
||||||
pass
|
|
||||||
|
|
||||||
class AnsibleUnsafeStr(str, AnsibleUnsafe):
|
class UnsafeProxy(object):
|
||||||
pass
|
def __new__(cls, obj, *args, **kwargs):
|
||||||
|
# In our usage we should only receive unicode strings.
|
||||||
class UnsafeProxy(object):
|
# This conditional and conversion exists to sanity check the values
|
||||||
def __new__(cls, obj, *args, **kwargs):
|
# we're given but we may want to take it out for testing and sanitize
|
||||||
if obj.__class__ == str:
|
# our input instead.
|
||||||
return AnsibleUnsafeStr(obj)
|
if isinstance(obj, string_types):
|
||||||
elif obj.__class__ == bytes:
|
obj = to_unicode(obj, errors='strict')
|
||||||
return AnsibleUnsafeBytes(obj)
|
return AnsibleUnsafeText(obj)
|
||||||
else:
|
return obj
|
||||||
return obj
|
|
||||||
else:
|
|
||||||
# Python 2
|
|
||||||
class AnsibleUnsafeStr(str, AnsibleUnsafe):
|
|
||||||
pass
|
|
||||||
|
|
||||||
class AnsibleUnsafeUnicode(unicode, AnsibleUnsafe):
|
|
||||||
pass
|
|
||||||
|
|
||||||
class UnsafeProxy(object):
|
|
||||||
def __new__(cls, obj, *args, **kwargs):
|
|
||||||
if obj.__class__ == unicode:
|
|
||||||
return AnsibleUnsafeUnicode(obj)
|
|
||||||
elif obj.__class__ == str:
|
|
||||||
return AnsibleUnsafeStr(obj)
|
|
||||||
else:
|
|
||||||
return obj
|
|
||||||
|
|
||||||
|
|
||||||
def _wrap_dict(v):
|
def _wrap_dict(v):
|
||||||
|
|
Loading…
Reference in a new issue