summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorToshio Kuratomi <toshio@fedoraproject.org>2015-10-20 10:55:13 -0700
committerToshio Kuratomi <toshio@fedoraproject.org>2015-10-20 10:57:23 -0700
commit99e7bb35c115e8a7b3cbd0944ac60baebd7d2caf (patch)
tree0759d636ffde430313032678e10ebb1ab1f1bf80
parent3c87c44af5698992de222056c38f01d85d88bb7b (diff)
downloadansible-99e7bb35c115e8a7b3cbd0944ac60baebd7d2caf.tar.gz
Simplify UnsafeProxy as we don't need to use it for byte strings, only text
-rw-r--r--lib/ansible/vars/unsafe_proxy.py50
1 files changed, 16 insertions, 34 deletions
diff --git a/lib/ansible/vars/unsafe_proxy.py b/lib/ansible/vars/unsafe_proxy.py
index ff48292ba1..47b56db723 100644
--- a/lib/ansible/vars/unsafe_proxy.py
+++ b/lib/ansible/vars/unsafe_proxy.py
@@ -53,46 +53,28 @@
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
+from ansible.utils.unicode import to_unicode
+from ansible.compat.six import string_types, text_type
+
__all__ = ['UnsafeProxy', 'AnsibleUnsafe', 'wrap_var']
class AnsibleUnsafe(object):
__UNSAFE__ = True
-try:
- unicode
-except NameError:
- # Python 3
- class AnsibleUnsafeBytes(bytes, AnsibleUnsafe):
- pass
-
- class AnsibleUnsafeStr(str, AnsibleUnsafe):
- pass
-
- 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)
- elif obj.__class__ == str:
- return AnsibleUnsafeStr(obj)
- else:
- return obj
+class AnsibleUnsafeText(text_type, AnsibleUnsafe):
+ pass
+
+class UnsafeProxy(object):
+ def __new__(cls, obj, *args, **kwargs):
+ # In our usage we should only receive unicode strings.
+ # This conditional and conversion exists to sanity check the values
+ # we're given but we may want to take it out for testing and sanitize
+ # our input instead.
+ if isinstance(obj, string_types):
+ obj = to_unicode(obj, errors='strict')
+ return AnsibleUnsafeText(obj)
+ return obj
def _wrap_dict(v):