diff --git a/salt/loader.py b/salt/loader.py index f45d3462dfb2..c076a2a744ea 100644 --- a/salt/loader.py +++ b/salt/loader.py @@ -1092,7 +1092,7 @@ def _mod_type(module_path): def _inject_into_mod(mod, name, value, force_lock=False): - ''' + """ Inject a variable into a module. This is used to inject "globals" like ``__salt__``, ``__pillar``, or ``grains``. @@ -1124,7 +1124,7 @@ def _inject_into_mod(mod, name, value, force_lock=False): module. If ``False`` (the default), this function checks for the module's variable without acquiring the lock and only acquires the lock if a new proxy has to be created and injected. - ''' + """ old_value = getattr(mod, name, None) # We use a double-checked locking scheme in order to avoid taking the lock # when a proxy object has already been injected. @@ -1256,10 +1256,14 @@ def __init__( for k, v in six.iteritems(self.pack): if v is None: # if the value of a pack is None, lets make an empty dict - value = thread_local_proxy.ThreadLocalProxy.unproxy(self.context_dict.get(k, {})) + value = thread_local_proxy.ThreadLocalProxy.unproxy( + self.context_dict.get(k, {}) + ) self.context_dict[k] = value - self.pack[k] = salt.utils.context.NamespacedDictWrapper(self.context_dict, k) + self.pack[k] = salt.utils.context.NamespacedDictWrapper( + self.context_dict, k + ) self.whitelist = whitelist self.virtual_enable = virtual_enable @@ -1537,18 +1541,24 @@ def clear(self): def __prep_mod_opts(self, opts): """ Strip out of the opts any logger instance - ''' - if '__grains__' not in self.pack: - _grains = thread_local_proxy.ThreadLocalProxy.unproxy(opts.get('grains', {})) + """ + if "__grains__" not in self.pack: + _grains = thread_local_proxy.ThreadLocalProxy.unproxy( + opts.get("grains", {}) + ) - self.context_dict['grains'] = _grains - self.pack['__grains__'] = salt.utils.context.NamespacedDictWrapper(self.context_dict, 'grains') + self.context_dict["grains"] = _grains + self.pack["__grains__"] = salt.utils.context.NamespacedDictWrapper( + self.context_dict, "grains" + ) - if '__pillar__' not in self.pack: - pillar = thread_local_proxy.ThreadLocalProxy.unproxy(opts.get('pillar', {})) + if "__pillar__" not in self.pack: + pillar = thread_local_proxy.ThreadLocalProxy.unproxy(opts.get("pillar", {})) - self.context_dict['pillar'] = pillar - self.pack['__pillar__'] = salt.utils.context.NamespacedDictWrapper(self.context_dict, 'pillar') + self.context_dict["pillar"] = pillar + self.pack["__pillar__"] = salt.utils.context.NamespacedDictWrapper( + self.context_dict, "pillar" + ) mod_opts = {} for key, val in list(opts.items()): diff --git a/salt/utils/json.py b/salt/utils/json.py index f7a257d5f73c..6a596c2f7548 100644 --- a/salt/utils/json.py +++ b/salt/utils/json.py @@ -118,18 +118,20 @@ def dump(obj, fp, **kwargs): You can pass an alternate json module (loaded via import_json() above) using the _json_module argument) - ''' - json_module = kwargs.pop('_json_module', json) - orig_enc_func = kwargs.pop('default', lambda x: x) + """ + json_module = kwargs.pop("_json_module", json) + orig_enc_func = kwargs.pop("default", lambda x: x) def _enc_func(_obj): return orig_enc_func(thread_local_proxy.ThreadLocalProxy.unproxy(_obj)) - if 'ensure_ascii' not in kwargs: - kwargs['ensure_ascii'] = False + if "ensure_ascii" not in kwargs: + kwargs["ensure_ascii"] = False if six.PY2: obj = salt.utils.data.encode(obj) - return json_module.dump(obj, fp, default=_enc_func, **kwargs) # future lint: blacklisted-function + return json_module.dump( + obj, fp, default=_enc_func, **kwargs + ) # future lint: blacklisted-function def dumps(obj, **kwargs): @@ -146,15 +148,17 @@ def dumps(obj, **kwargs): You can pass an alternate json module (loaded via import_json() above) using the _json_module argument) - ''' - json_module = kwargs.pop('_json_module', json) - orig_enc_func = kwargs.pop('default', lambda x: x) + """ + json_module = kwargs.pop("_json_module", json) + orig_enc_func = kwargs.pop("default", lambda x: x) def _enc_func(_obj): return orig_enc_func(thread_local_proxy.ThreadLocalProxy.unproxy(_obj)) - if 'ensure_ascii' not in kwargs: - kwargs['ensure_ascii'] = False + if "ensure_ascii" not in kwargs: + kwargs["ensure_ascii"] = False if six.PY2: obj = salt.utils.data.encode(obj) - return json_module.dumps(obj, default=_enc_func, **kwargs) # future lint: blacklisted-function + return json_module.dumps( + obj, default=_enc_func, **kwargs + ) # future lint: blacklisted-function diff --git a/salt/utils/msgpack.py b/salt/utils/msgpack.py index 4dd380f55fb9..f6a2c4250c64 100644 --- a/salt/utils/msgpack.py +++ b/salt/utils/msgpack.py @@ -96,8 +96,8 @@ def pack(o, stream, **kwargs): By default, this function uses the msgpack module and falls back to msgpack_pure, if the msgpack is not available. - ''' - orig_enc_func = kwargs.pop('default', lambda x: x) + """ + orig_enc_func = kwargs.pop("default", lambda x: x) def _enc_func(obj): return orig_enc_func(thread_local_proxy.ThreadLocalProxy.unproxy(obj)) @@ -115,8 +115,8 @@ def packb(o, **kwargs): By default, this function uses the msgpack module and falls back to msgpack_pure, if the msgpack is not available. - ''' - orig_enc_func = kwargs.pop('default', lambda x: x) + """ + orig_enc_func = kwargs.pop("default", lambda x: x) def _enc_func(obj): return orig_enc_func(thread_local_proxy.ThreadLocalProxy.unproxy(obj)) diff --git a/salt/utils/thread_local_proxy.py b/salt/utils/thread_local_proxy.py index 8744dbe3870b..9239cfa1edaf 100644 --- a/salt/utils/thread_local_proxy.py +++ b/salt/utils/thread_local_proxy.py @@ -1,11 +1,11 @@ # -*- coding: utf-8 -*- -''' +""" Proxy object that can reference different values depending on the current thread of execution. ..versionadded:: 2018.3.4 -''' +""" # Import python libs from __future__ import absolute_import @@ -16,7 +16,7 @@ class ThreadLocalProxy(object): - ''' + """ Proxy that delegates all operations to its referenced object. The referenced object is hold through a thread-local variable, so that this proxy may refer to different objects in different threads of execution. @@ -39,13 +39,13 @@ class ThreadLocalProxy(object): This class has primarily been designed for use by the Salt loader, but it might also be useful in other places. - ''' + """ - __slots__ = ['_thread_local', '_last_reference', '_fallback_to_shared'] + __slots__ = ["_thread_local", "_last_reference", "_fallback_to_shared"] @staticmethod def get_reference(proxy): - ''' + """ Return the object that is referenced by the specified proxy. If the proxy has not been bound to a reference for the current thread, @@ -64,18 +64,17 @@ def get_reference(proxy): specified object is not an instance of `ThreadLocalProxy`, the behavior is unspecified. Typically, an ``AttributeError`` is going to be raised. - ''' - thread_local = object.__getattribute__(proxy, '_thread_local') + """ + thread_local = object.__getattribute__(proxy, "_thread_local") try: return thread_local.reference except AttributeError: - fallback_to_shared = object.__getattribute__( - proxy, '_fallback_to_shared') + fallback_to_shared = object.__getattribute__(proxy, "_fallback_to_shared") if fallback_to_shared: # If the reference has never been set in the current thread of # execution, we use the reference that has been last set by any # thread. - reference = object.__getattribute__(proxy, '_last_reference') + reference = object.__getattribute__(proxy, "_last_reference") # We save the reference in the thread local so that future # calls to get_reference will have consistent results. ThreadLocalProxy.set_reference(proxy, reference) @@ -89,11 +88,12 @@ def get_reference(proxy): # For this reason, we raise an AttributeError with an error # message explaining the problem. raise AttributeError( - 'The proxy object has not been bound to a reference in this thread of execution.') + "The proxy object has not been bound to a reference in this thread of execution." + ) @staticmethod def set_reference(proxy, new_reference): - ''' + """ Set the reference to be used the current thread of execution. After calling this function, the specified proxy will act like it was @@ -108,7 +108,7 @@ def set_reference(proxy, new_reference): new_reference: reference the proxy should point to for the current thread after calling this function. - ''' + """ # If the new reference is itself a proxy, we have to ensure that it does # not refer to this proxy. If it does, we simply return because updating # the reference would result in an inifite loop when trying to use the @@ -118,13 +118,13 @@ def set_reference(proxy, new_reference): if possible_proxy is proxy: return possible_proxy = ThreadLocalProxy.get_reference(possible_proxy) - thread_local = object.__getattribute__(proxy, '_thread_local') + thread_local = object.__getattribute__(proxy, "_thread_local") thread_local.reference = new_reference - object.__setattr__(proxy, '_last_reference', new_reference) + object.__setattr__(proxy, "_last_reference", new_reference) @staticmethod def unset_reference(proxy): - ''' + """ Unset the reference to be used by the current thread of execution. After calling this function, the specified proxy will act like the @@ -135,13 +135,13 @@ def unset_reference(proxy): specified object is not an instance of `ThreadLocalProxy`, the behavior is unspecified. Typically, an ``AttributeError`` is going to be raised. - ''' - thread_local = object.__getattribute__(proxy, '_thread_local') + """ + thread_local = object.__getattribute__(proxy, "_thread_local") del thread_local.reference @staticmethod def unproxy(possible_proxy): - ''' + """ Unwrap and return the object referenced by a proxy. This function is very similar to :func:`get_reference`, but works for @@ -154,13 +154,13 @@ def unproxy(possible_proxy): possible_proxy: object that might or might not be a proxy. - ''' + """ while isinstance(possible_proxy, ThreadLocalProxy): possible_proxy = ThreadLocalProxy.get_reference(possible_proxy) return possible_proxy def __init__(self, initial_reference, fallback_to_shared=False): - ''' + """ Create a proxy object that references the specified object. initial_reference: @@ -175,9 +175,9 @@ def __init__(self, initial_reference, fallback_to_shared=False): reference last set by any thread. If ``False`` (the default), an exception is raised when the proxy is used in a thread without first initializing the reference in this thread. - ''' - object.__setattr__(self, '_thread_local', threading.local()) - object.__setattr__(self, '_fallback_to_shared', fallback_to_shared) + """ + object.__setattr__(self, "_thread_local", threading.local()) + object.__setattr__(self, "_fallback_to_shared", fallback_to_shared) ThreadLocalProxy.set_reference(self, initial_reference) def __repr__(self): @@ -541,11 +541,11 @@ def __ior__(self, other): def __neg__(self): reference = ThreadLocalProxy.get_reference(self) - return - reference + return -reference def __pos__(self): reference = ThreadLocalProxy.get_reference(self) - return + reference + return +reference def __abs__(self): reference = ThreadLocalProxy.get_reference(self) @@ -553,7 +553,7 @@ def __abs__(self): def __invert__(self): reference = ThreadLocalProxy.get_reference(self) - return ~ reference + return ~reference def __complex__(self): reference = ThreadLocalProxy.get_reference(self) diff --git a/tests/unit/test_loader.py b/tests/unit/test_loader.py index 41e779298848..ed3648eebf08 100644 --- a/tests/unit/test_loader.py +++ b/tests/unit/test_loader.py @@ -27,6 +27,7 @@ import salt.utils.files import salt.utils.stringutils import salt.utils.thread_local_proxy as thread_local_proxy + # pylint: disable=import-error,no-name-in-module,redefined-builtin from salt.ext import six from salt.ext.six.moves import range @@ -1497,25 +1498,25 @@ def setUpClass(cls): def test__inject_into_mod(self): class test_module(object): - name = 'inject_into_mod.test.module' + name = "inject_into_mod.test.module" # First path, Force is not true, proxy doesn't exist -- also takes the path of Force True and proxy not exist - salt.loader._inject_into_mod(test_module, '__opts__', self.opts) - self.assertTrue(hasattr(test_module, '__opts__')) + salt.loader._inject_into_mod(test_module, "__opts__", self.opts) + self.assertTrue(hasattr(test_module, "__opts__")) self.assertIsInstance(test_module.__opts__, thread_local_proxy.ThreadLocalProxy) self.assertEqual(self.opts, test_module.__opts__) foo = test_module.__opts__ # Second path, Force is not true, proxy exists - salt.loader._inject_into_mod(test_module, '__opts__', self.opts) + salt.loader._inject_into_mod(test_module, "__opts__", self.opts) self.assertIsInstance(test_module.__opts__, thread_local_proxy.ThreadLocalProxy) self.assertEqual(self.opts, test_module.__opts__) bar = test_module.__opts__ self.assertIs(foo, bar) self.assertEqual(foo, bar) - foo['yes'] = 'no' - self.assertIn('yes', bar) + foo["yes"] = "no" + self.assertIn("yes", bar) # Final path, Force is true, proxy exists - salt.loader._inject_into_mod(test_module, '__opts__', self.opts, True) + salt.loader._inject_into_mod(test_module, "__opts__", self.opts, True) diff --git a/tests/unit/utils/test_thread_local_proxy.py b/tests/unit/utils/test_thread_local_proxy.py index 0cd77379aed8..ebcfeb07d147 100644 --- a/tests/unit/utils/test_thread_local_proxy.py +++ b/tests/unit/utils/test_thread_local_proxy.py @@ -11,15 +11,15 @@ class ThreadLocalProxyTestCase(TestCase): - ''' + """ Test case for salt.utils.thread_local_proxy module. - ''' + """ def test_set_reference_avoid_loop(self): - ''' + """ Test that passing another proxy (or the same proxy) to set_reference does not results in a recursive proxy loop. - ''' + """ test_obj1 = 1 test_obj2 = 2 proxy1 = thread_local_proxy.ThreadLocalProxy(test_obj1)