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.
pull/60210/head
Toshio Kuratomi 5 years ago committed by James Cammarata
parent d00aaf66d7
commit c1e23c22a9

@ -75,11 +75,17 @@ class AnsibleUnsafeBytes(binary_type, AnsibleUnsafe):
class UnsafeProxy(object):
def __new__(cls, obj, *args, **kwargs):
if isinstance(obj, AnsibleUnsafe):
# Already marked unsafe
return obj
# 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) and not isinstance(obj, AnsibleUnsafeBytes):
# Note that this does the wrong thing if we're *intentionall* passing a byte string to this
# function.
if isinstance(obj, string_types):
obj = AnsibleUnsafeText(to_text(obj, errors='surrogate_or_strict'))
return obj

Loading…
Cancel
Save