From 490f17c7f959ce153765c1f033fdc30becf0faf7 Mon Sep 17 00:00:00 2001 From: Toshio Kuratomi Date: Wed, 7 Aug 2019 09:11:56 -0500 Subject: [PATCH] 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) --- lib/ansible/utils/unsafe_proxy.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/ansible/utils/unsafe_proxy.py b/lib/ansible/utils/unsafe_proxy.py index 35528f317bf..42ee8058e2f 100644 --- a/lib/ansible/utils/unsafe_proxy.py +++ b/lib/ansible/utils/unsafe_proxy.py @@ -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