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 base64
import contextlib import contextlib
import functools import functools
import inspect
import socket import socket
import sys import sys
import time import time
@ -288,8 +289,8 @@ def errors_out_migration(function):
except Exception as ex: except Exception as ex:
with excutils.save_and_reraise_exception(): with excutils.save_and_reraise_exception():
wrapped_func = safe_utils.get_wrapped_function(function) wrapped_func = safe_utils.get_wrapped_function(function)
keyed_args = safe_utils.getcallargs(wrapped_func, self, keyed_args = inspect.getcallargs(wrapped_func, self, context,
context, *args, **kwargs) *args, **kwargs)
migration = keyed_args['migration'] migration = keyed_args['migration']
# NOTE(rajesht): If InstanceNotFound error is thrown from # NOTE(rajesht): If InstanceNotFound error is thrown from
@ -330,8 +331,8 @@ def reverts_task_state(function):
except Exception: except Exception:
with excutils.save_and_reraise_exception(): with excutils.save_and_reraise_exception():
wrapped_func = safe_utils.get_wrapped_function(function) wrapped_func = safe_utils.get_wrapped_function(function)
keyed_args = safe_utils.getcallargs(wrapped_func, self, keyed_args = inspect.getcallargs(wrapped_func, self, context,
context, *args, **kwargs) *args, **kwargs)
# NOTE(mriedem): 'instance' must be in keyed_args because we # NOTE(mriedem): 'instance' must be in keyed_args because we
# have utils.expects_func_args('instance') decorating this # have utils.expects_func_args('instance') decorating this
# method. # method.
@ -390,8 +391,8 @@ def wrap_instance_event(function):
@functools.wraps(function) @functools.wraps(function)
def decorated_function(self, context, *args, **kwargs): def decorated_function(self, context, *args, **kwargs):
wrapped_func = safe_utils.get_wrapped_function(function) wrapped_func = safe_utils.get_wrapped_function(function)
keyed_args = safe_utils.getcallargs(wrapped_func, self, context, *args, keyed_args = inspect.getcallargs(wrapped_func, self, context, *args,
**kwargs) **kwargs)
instance_uuid = keyed_args['instance']['uuid'] instance_uuid = keyed_args['instance']['uuid']
event_name = 'compute_{0}'.format(function.__name__) event_name = 'compute_{0}'.format(function.__name__)

View File

@ -23,6 +23,7 @@ SHOULD include dedicated exception logging.
""" """
import functools import functools
import inspect
import sys import sys
from oslo_config import cfg from oslo_config import cfg
@ -91,9 +92,8 @@ def wrap_exception(notifier=None, get_notifier=None):
if notifier or get_notifier: if notifier or get_notifier:
payload = dict(exception=e) payload = dict(exception=e)
wrapped_func = safe_utils.get_wrapped_function(f) wrapped_func = safe_utils.get_wrapped_function(f)
call_dict = safe_utils.getcallargs(wrapped_func, self, call_dict = inspect.getcallargs(wrapped_func, self,
context, *args, context, *args, **kw)
**kw)
# self can't be serialized and shouldn't be in the # self can't be serialized and shouldn't be in the
# payload # payload
call_dict.pop('self', None) call_dict.pop('self', None)

View File

@ -17,39 +17,6 @@
"""Utilities and helper functions that won't produce circular imports.""" """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): def get_wrapped_function(function):
"""Get the method at the bottom of a stack of decorators.""" """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 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): class WrappedCodeTestCase(test.NoDBTestCase):
"""Test the get_wrapped_function utility method.""" """Test the get_wrapped_function utility method."""