Browse code

Improve performane of UnsafeProxy __new__

This adds an early return to the __new__ method of the UnsafeProxy object
which avoids creating the unsafe object if the incoming object is already
unsafe.

(cherry picked from commit c1e23c22a9fedafaaa88c2119b26dc123ff1392e)

Toshio Kuratomi authored on 2019/08/07 23:11:56
Showing 1 changed files
... ...
@@ -75,11 +75,17 @@ class AnsibleUnsafeBytes(binary_type, AnsibleUnsafe):
75 75
 
76 76
 class UnsafeProxy(object):
77 77
     def __new__(cls, obj, *args, **kwargs):
78
+        if isinstance(obj, AnsibleUnsafe):
79
+            # Already marked unsafe
80
+            return obj
81
+
78 82
         # In our usage we should only receive unicode strings.
79 83
         # This conditional and conversion exists to sanity check the values
80 84
         # we're given but we may want to take it out for testing and sanitize
81 85
         # our input instead.
82
-        if isinstance(obj, string_types) and not isinstance(obj, AnsibleUnsafeBytes):
86
+        # Note that this does the wrong thing if we're *intentionall* passing a byte string to this
87
+        # function.
88
+        if isinstance(obj, string_types):
83 89
             obj = AnsibleUnsafeText(to_text(obj, errors='surrogate_or_strict'))
84 90
         return obj
85 91