def _get_call_dict(function, self, context, *args, **kw): wrapped_func = safe_utils.get_wrapped_function(function) call_dict = inspect.getcallargs(wrapped_func, self, context, *args, **kw) # self can't be serialized and shouldn't be in the # payload call_dict.pop('self', None) return _cleanse_dict(call_dict)
def decorated_function(self, context, *args, **kwargs): wrapped_func = safe_utils.get_wrapped_function(function) keyed_args = inspect.getcallargs(wrapped_func, self, context, *args, **kwargs) instance_uuid = keyed_args['instance']['uuid'] event_name = '{0}_{1}'.format(prefix, function.__name__) with EventReporter(context, event_name, instance_uuid): return function(self, context, *args, **kwargs)
def test_closure(self): closure = get_closure() func = safe_utils.get_wrapped_function(closure) func_code = func.__code__ self.assertEqual(4, len(func_code.co_varnames)) self.assertIn('self', func_code.co_varnames) self.assertIn('instance', func_code.co_varnames) self.assertIn('red', func_code.co_varnames) self.assertIn('blue', func_code.co_varnames)
def _get_call_dict(function, self, context, *args, **kw): wrapped_func = safe_utils.get_wrapped_function(function) call_dict = inspect.getcallargs(wrapped_func, self, context, *args, **kw) # self can't be serialized and shouldn't be in the # payload call_dict.pop('self', None) # NOTE(gibi) remove context as well as it contains sensitive information # and it can also contain circular references call_dict.pop('context', None) return _cleanse_dict(call_dict)
def test_single_wrapped(self): @self._wrapper def wrapped(self, instance, red=None, blue=None): pass func = safe_utils.get_wrapped_function(wrapped) func_code = func.__code__ self.assertEqual(4, len(func_code.co_varnames)) self.assertIn('self', func_code.co_varnames) self.assertIn('instance', func_code.co_varnames) self.assertIn('red', func_code.co_varnames) self.assertIn('blue', func_code.co_varnames)
def test_double_wrapped(self): @self._wrapper @self._wrapper def wrapped(self, instance, red=None, blue=None): pass func = safe_utils.get_wrapped_function(wrapped) func_code = func.__code__ self.assertEqual(4, len(func_code.co_varnames)) self.assertIn("self", func_code.co_varnames) self.assertIn("instance", func_code.co_varnames) self.assertIn("red", func_code.co_varnames) self.assertIn("blue", func_code.co_varnames)
def _decorator(f): base_f = safe_utils.get_wrapped_function(f) argspec = getargspec(base_f) if argspec[1] or argspec[2] or set(args) <= set(argspec[0]): # NOTE (ndipanov): We can't really tell if correct stuff will # be passed if it's a function with *args or **kwargs so # we still carry on and hope for the best return dec(f) else: raise TypeError("Decorated function %(f_name)s does not " "have the arguments expected by the " "decorator %(d_name)s" % {'f_name': base_f.__name__, 'd_name': dec.__name__})
def _decorator(f): base_f = safe_utils.get_wrapped_function(f) arg_names, a, kw, _default = inspect.getargspec(base_f) if a or kw or set(args) <= set(arg_names): # NOTE (ndipanov): We can't really tell if correct stuff will # be passed if it's a function with *args or **kwargs so # we still carry on and hope for the best return dec(f) else: raise TypeError("Decorated function %(f_name)s does not " "have the arguments expected by the " "decorator %(d_name)s" % {'f_name': base_f.__name__, 'd_name': dec.__name__})
def test_public_api_signatures(self): driver_methods = dict(driver.HyperVClusterDriver.__dict__, **base_driver.HyperVDriver.__dict__) for attr in driver_methods: class_member = getattr(driver.HyperVClusterDriver, attr) if callable(class_member): mocked_method = mock.patch.object( driver.HyperVClusterDriver, attr, safe_utils.get_wrapped_function(class_member)) mocked_method.start() self.addCleanup(mocked_method.stop) self.assertPublicAPISignatures(nova_base_driver.ComputeDriver, driver.HyperVClusterDriver)
def test_public_api_signatures(self): # NOTE(claudiub): wrapped functions do not keep the same signature in # Python 2.7, which causes this test to fail. Instead, we should # compare the public API signatures of the unwrapped methods. for attr in driver.HyperVDriver.__dict__: class_member = getattr(driver.HyperVDriver, attr) if callable(class_member): mocked_method = mock.patch.object( driver.HyperVDriver, attr, safe_utils.get_wrapped_function(class_member)) mocked_method.start() self.addCleanup(mocked_method.stop) self.assertPublicAPISignatures(base_driver.ComputeDriver, driver.HyperVDriver)