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
This commit is contained in:
Andrew Laski 2015-12-10 11:39:48 -05:00
parent 1b757c328d
commit 997bec1bb9
4 changed files with 10 additions and 136 deletions

View File

@ -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__)

View File

@ -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)

View File

@ -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."""

View File

@ -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."""