mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Make UnsafeProxy work on Python 3
Instead of str and unicode we have to deal with bytes and str.
This commit is contained in:
parent
af6a316d9b
commit
e511d6d458
1 changed files with 35 additions and 12 deletions
|
@ -56,13 +56,33 @@ __all__ = ['UnsafeProxy', 'AnsibleUnsafe', 'wrap_var']
|
|||
class AnsibleUnsafe(object):
|
||||
__UNSAFE__ = True
|
||||
|
||||
class AnsibleUnsafeStr(str, AnsibleUnsafe):
|
||||
try:
|
||||
unicode
|
||||
except NameError:
|
||||
# Python 3
|
||||
class AnsibleUnsafeBytes(bytes, AnsibleUnsafe):
|
||||
pass
|
||||
|
||||
class AnsibleUnsafeUnicode(unicode, AnsibleUnsafe):
|
||||
class AnsibleUnsafeStr(str, AnsibleUnsafe):
|
||||
pass
|
||||
|
||||
class UnsafeProxy(object):
|
||||
class UnsafeProxy(object):
|
||||
def __new__(cls, obj, *args, **kwargs):
|
||||
if obj.__class__ == str:
|
||||
return AnsibleUnsafeStr(obj)
|
||||
elif obj.__class__ == bytes:
|
||||
return AnsibleUnsafeBytes(obj)
|
||||
else:
|
||||
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)
|
||||
|
@ -71,18 +91,21 @@ class UnsafeProxy(object):
|
|||
else:
|
||||
return obj
|
||||
|
||||
|
||||
def _wrap_dict(v):
|
||||
for k in v.keys():
|
||||
if v[k] is not None:
|
||||
v[k] = wrap_var(v[k])
|
||||
return v
|
||||
|
||||
|
||||
def _wrap_list(v):
|
||||
for idx, item in enumerate(v):
|
||||
if item is not None:
|
||||
v[idx] = wrap_var(item)
|
||||
return v
|
||||
|
||||
|
||||
def wrap_var(v):
|
||||
if isinstance(v, dict):
|
||||
v = _wrap_dict(v)
|
||||
|
|
Loading…
Reference in a new issue