From af6a316d9beda69ce50e8396c24b4f74ff905109 Mon Sep 17 00:00:00 2001 From: Marius Gedminas Date: Tue, 13 Oct 2015 09:37:48 +0300 Subject: [PATCH 1/2] Remove unused import Fixes ImportError: No module named '__builtin__' on Python 3. --- lib/ansible/vars/unsafe_proxy.py | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/ansible/vars/unsafe_proxy.py b/lib/ansible/vars/unsafe_proxy.py index 9d39af565b..ee5e7e875d 100644 --- a/lib/ansible/vars/unsafe_proxy.py +++ b/lib/ansible/vars/unsafe_proxy.py @@ -52,7 +52,6 @@ __all__ = ['UnsafeProxy', 'AnsibleUnsafe', 'wrap_var'] -import __builtin__ class AnsibleUnsafe(object): __UNSAFE__ = True From e511d6d458e247ea183bc750d5d7fc1df099c28c Mon Sep 17 00:00:00 2001 From: Marius Gedminas Date: Tue, 13 Oct 2015 09:41:08 +0300 Subject: [PATCH 2/2] Make UnsafeProxy work on Python 3 Instead of str and unicode we have to deal with bytes and str. --- lib/ansible/vars/unsafe_proxy.py | 47 ++++++++++++++++++++++++-------- 1 file changed, 35 insertions(+), 12 deletions(-) diff --git a/lib/ansible/vars/unsafe_proxy.py b/lib/ansible/vars/unsafe_proxy.py index ee5e7e875d..19707e10b3 100644 --- a/lib/ansible/vars/unsafe_proxy.py +++ b/lib/ansible/vars/unsafe_proxy.py @@ -56,20 +56,41 @@ __all__ = ['UnsafeProxy', 'AnsibleUnsafe', 'wrap_var'] class AnsibleUnsafe(object): __UNSAFE__ = True -class AnsibleUnsafeStr(str, AnsibleUnsafe): - pass +try: + unicode +except NameError: + # Python 3 + class AnsibleUnsafeBytes(bytes, AnsibleUnsafe): + pass -class AnsibleUnsafeUnicode(unicode, 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 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): for k in v.keys(): @@ -77,12 +98,14 @@ def _wrap_dict(v): 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)