From 997bec1bb982f610a2a278ab18e3033c8eb2accd Mon Sep 17 00:00:00 2001 From: Andrew Laski Date: Thu, 10 Dec 2015 11:39:48 -0500 Subject: [PATCH] Replace safe_utils.getcallargs with inspect.getcallargs safe_utils.getcallargs was written to support python2.6 which did not have inspect.getcallargs. Now that support for python2.6 has been dropped it should be replaced with inspect.getcallargs. Change-Id: Idf5b9a7f4d10b81b1be9aed26505e3acaa6f7e24 --- nova/compute/manager.py | 13 +++-- nova/exception.py | 6 +- nova/safe_utils.py | 33 ----------- nova/tests/unit/test_safeutils.py | 94 ------------------------------- 4 files changed, 10 insertions(+), 136 deletions(-) diff --git a/nova/compute/manager.py b/nova/compute/manager.py index 4f165be7e796..c4d5528f5bea 100644 --- a/nova/compute/manager.py +++ b/nova/compute/manager.py @@ -28,6 +28,7 @@ terminating it. import base64 import contextlib import functools +import inspect import socket import sys import time @@ -288,8 +289,8 @@ def errors_out_migration(function): except Exception as ex: with excutils.save_and_reraise_exception(): wrapped_func = safe_utils.get_wrapped_function(function) - keyed_args = safe_utils.getcallargs(wrapped_func, self, - context, *args, **kwargs) + keyed_args = inspect.getcallargs(wrapped_func, self, context, + *args, **kwargs) migration = keyed_args['migration'] # NOTE(rajesht): If InstanceNotFound error is thrown from @@ -330,8 +331,8 @@ def reverts_task_state(function): except Exception: with excutils.save_and_reraise_exception(): wrapped_func = safe_utils.get_wrapped_function(function) - keyed_args = safe_utils.getcallargs(wrapped_func, self, - context, *args, **kwargs) + keyed_args = inspect.getcallargs(wrapped_func, self, context, + *args, **kwargs) # NOTE(mriedem): 'instance' must be in keyed_args because we # have utils.expects_func_args('instance') decorating this # method. @@ -390,8 +391,8 @@ def wrap_instance_event(function): @functools.wraps(function) def decorated_function(self, context, *args, **kwargs): wrapped_func = safe_utils.get_wrapped_function(function) - keyed_args = safe_utils.getcallargs(wrapped_func, self, context, *args, - **kwargs) + keyed_args = inspect.getcallargs(wrapped_func, self, context, *args, + **kwargs) instance_uuid = keyed_args['instance']['uuid'] event_name = 'compute_{0}'.format(function.__name__) diff --git a/nova/exception.py b/nova/exception.py index c01e68e37650..45bf49788eaf 100644 --- a/nova/exception.py +++ b/nova/exception.py @@ -23,6 +23,7 @@ SHOULD include dedicated exception logging. """ import functools +import inspect import sys from oslo_config import cfg @@ -91,9 +92,8 @@ def wrap_exception(notifier=None, get_notifier=None): if notifier or get_notifier: payload = dict(exception=e) wrapped_func = safe_utils.get_wrapped_function(f) - call_dict = safe_utils.getcallargs(wrapped_func, self, - context, *args, - **kw) + 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) diff --git a/nova/safe_utils.py b/nova/safe_utils.py index 8fafdc0f70d5..64885a32aa6c 100644 --- a/nova/safe_utils.py +++ b/nova/safe_utils.py @@ -17,39 +17,6 @@ """Utilities and helper functions that won't produce circular imports.""" -import inspect - - -def getcallargs(function, *args, **kwargs): - """This is a simplified inspect.getcallargs (2.7+). - - It should be replaced when python >= 2.7 is standard. - - This method can only properly grab arguments which are passed in as - keyword arguments, or given names by the method being called. This means - that an ``*arg`` in a method signature and any arguments captured by it - will be left out of the results. - - As an example: if function is defined as function(red, blue, green, *args) - then arguments captured by *args won't be returned. If function is called - as function('red', 'blue', 'green') those will all be captured. - """ - keyed_args = {} - argnames, varargs, keywords, defaults = inspect.getargspec(function) - - keyed_args.update(kwargs) - - remaining_argnames = filter(lambda x: x not in keyed_args, argnames) - keyed_args.update(dict(zip(remaining_argnames, args))) - - if defaults: - num_defaults = len(defaults) - for argname, value in zip(argnames[-num_defaults:], defaults): - if argname not in keyed_args: - keyed_args[argname] = value - - return keyed_args - def get_wrapped_function(function): """Get the method at the bottom of a stack of decorators.""" diff --git a/nova/tests/unit/test_safeutils.py b/nova/tests/unit/test_safeutils.py index cf0d496bd984..1215c30b4386 100644 --- a/nova/tests/unit/test_safeutils.py +++ b/nova/tests/unit/test_safeutils.py @@ -18,100 +18,6 @@ from nova import safe_utils from nova import test -class GetCallArgsTestCase(test.NoDBTestCase): - def _test_func(self, instance, red=None, blue=None): - pass - - def test_all_kwargs(self): - args = (self,) - kwargs = {'instance': {'uuid': 1}, 'red': 3, 'blue': 4} - callargs = safe_utils.getcallargs(self._test_func, *args, **kwargs) - # implicit self counts as an arg - self.assertEqual(4, len(callargs)) - self.assertIn('instance', callargs) - self.assertEqual({'uuid': 1}, callargs['instance']) - self.assertIn('red', callargs) - self.assertEqual(3, callargs['red']) - self.assertIn('blue', callargs) - self.assertEqual(4, callargs['blue']) - - def test_all_args(self): - args = (self, {'uuid': 1}, 3, 4) - kwargs = {} - callargs = safe_utils.getcallargs(self._test_func, *args, **kwargs) - # implicit self counts as an arg - self.assertEqual(4, len(callargs)) - self.assertIn('instance', callargs) - self.assertEqual({'uuid': 1}, callargs['instance']) - self.assertIn('red', callargs) - self.assertEqual(3, callargs['red']) - self.assertIn('blue', callargs) - self.assertEqual(4, callargs['blue']) - - def test_mixed_args(self): - args = (self, {'uuid': 1}, 3) - kwargs = {'blue': 4} - callargs = safe_utils.getcallargs(self._test_func, *args, **kwargs) - # implicit self counts as an arg - self.assertEqual(4, len(callargs)) - self.assertIn('instance', callargs) - self.assertEqual({'uuid': 1}, callargs['instance']) - self.assertIn('red', callargs) - self.assertEqual(3, callargs['red']) - self.assertIn('blue', callargs) - self.assertEqual(4, callargs['blue']) - - def test_partial_kwargs(self): - args = (self,) - kwargs = {'instance': {'uuid': 1}, 'red': 3} - callargs = safe_utils.getcallargs(self._test_func, *args, **kwargs) - # implicit self counts as an arg - self.assertEqual(4, len(callargs)) - self.assertIn('instance', callargs) - self.assertEqual({'uuid': 1}, callargs['instance']) - self.assertIn('red', callargs) - self.assertEqual(3, callargs['red']) - self.assertIn('blue', callargs) - self.assertIsNone(callargs['blue']) - - def test_partial_args(self): - args = (self, {'uuid': 1}, 3) - kwargs = {} - callargs = safe_utils.getcallargs(self._test_func, *args, **kwargs) - # implicit self counts as an arg - self.assertEqual(4, len(callargs)) - self.assertIn('instance', callargs) - self.assertEqual({'uuid': 1}, callargs['instance']) - self.assertIn('red', callargs) - self.assertEqual(3, callargs['red']) - self.assertIn('blue', callargs) - self.assertIsNone(callargs['blue']) - - def test_partial_mixed_args(self): - args = (self, 3) - kwargs = {'instance': {'uuid': 1}} - callargs = safe_utils.getcallargs(self._test_func, *args, **kwargs) - self.assertEqual(4, len(callargs)) - self.assertIn('instance', callargs) - self.assertEqual({'uuid': 1}, callargs['instance']) - self.assertIn('red', callargs) - self.assertEqual(3, callargs['red']) - self.assertIn('blue', callargs) - self.assertIsNone(callargs['blue']) - - def test_no_named_args(self): - def _fake(*args, **kwargs): - pass - - # This is not captured by getcallargs - args = (3,) - kwargs = {'instance': {'uuid': 1}} - callargs = safe_utils.getcallargs(_fake, *args, **kwargs) - self.assertEqual(1, len(callargs)) - self.assertIn('instance', callargs) - self.assertEqual({'uuid': 1}, callargs['instance']) - - class WrappedCodeTestCase(test.NoDBTestCase): """Test the get_wrapped_function utility method."""