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. Note:The interface that safe_utils.getcallargs provides did not match inspect.getcallargs around the handling of the self parameter needing to be passed in. It should be brought inline with inspect.getcallargs so that it can be dropped. Nova fixed in 2910d75b28afd909af3b4ac392729ac3d5e64b65. Change-Id: I2fbec0cc4d43b2fc424460b176bf26700f05f44a
This commit is contained in:
parent
aa0c3c6f9a
commit
50618882d5
@ -22,6 +22,7 @@ SHOULD include dedicated exception logging.
|
|||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
import inspect
|
||||||
import logging
|
import logging
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
@ -31,7 +32,6 @@ import six
|
|||||||
import webob.exc
|
import webob.exc
|
||||||
|
|
||||||
from oslo_versionedobjects._i18n import _, _LE
|
from oslo_versionedobjects._i18n import _, _LE
|
||||||
from oslo_versionedobjects import safe_utils
|
|
||||||
|
|
||||||
LOG = logging.getLogger(__name__)
|
LOG = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -75,8 +75,8 @@ def wrap_exception(notifier=None, get_notifier=None):
|
|||||||
with excutils.save_and_reraise_exception():
|
with excutils.save_and_reraise_exception():
|
||||||
if notifier or get_notifier:
|
if notifier or get_notifier:
|
||||||
payload = dict(exception=e)
|
payload = dict(exception=e)
|
||||||
call_dict = safe_utils.getcallargs(f, context,
|
call_dict = inspect.getcallargs(f, self, context,
|
||||||
*args, **kw)
|
*args, **kw)
|
||||||
cleansed = _cleanse_dict(call_dict)
|
cleansed = _cleanse_dict(call_dict)
|
||||||
payload.update({'args': cleansed})
|
payload.update({'args': cleansed})
|
||||||
|
|
||||||
|
@ -1,53 +0,0 @@
|
|||||||
# Copyright 2010 United States Government as represented by the
|
|
||||||
# Administrator of the National Aeronautics and Space Administration.
|
|
||||||
# Copyright 2011 Justin Santa Barbara
|
|
||||||
# All Rights Reserved.
|
|
||||||
#
|
|
||||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
||||||
# not use this file except in compliance with the License. You may obtain
|
|
||||||
# a copy of the License at
|
|
||||||
#
|
|
||||||
# http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
#
|
|
||||||
# Unless required by applicable law or agreed to in writing, software
|
|
||||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
||||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
||||||
# License for the specific language governing permissions and limitations
|
|
||||||
# under the License.
|
|
||||||
|
|
||||||
"""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.
|
|
||||||
"""
|
|
||||||
keyed_args = {}
|
|
||||||
argnames, varargs, keywords, defaults = inspect.getargspec(function)
|
|
||||||
|
|
||||||
keyed_args.update(kwargs)
|
|
||||||
|
|
||||||
# NOTE(alaski) the implicit 'self' or 'cls' argument shows up in
|
|
||||||
# argnames but not in args or kwargs. Uses 'in' rather than '==' because
|
|
||||||
# some tests use 'self2'.
|
|
||||||
if 'self' in argnames[0] or 'cls' == argnames[0]:
|
|
||||||
# The function may not actually be a method or have im_self.
|
|
||||||
# Typically seen when it's stubbed with mox.
|
|
||||||
if inspect.ismethod(function) and hasattr(function, 'im_self'):
|
|
||||||
keyed_args[argnames[0]] = function.im_self
|
|
||||||
else:
|
|
||||||
keyed_args[argnames[0]] = None
|
|
||||||
|
|
||||||
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
|
|
@ -46,7 +46,7 @@ class ExceptionTestCase(test.TestCase):
|
|||||||
test.raise_exc, context, exc, admin_password="xxx")
|
test.raise_exc, context, exc, admin_password="xxx")
|
||||||
|
|
||||||
# wrap_exception() strips admin_password from args
|
# wrap_exception() strips admin_password from args
|
||||||
payload = {'args': {'self': None, 'context': context, 'exc': exc},
|
payload = {'args': {'self': test, 'context': context, 'exc': exc},
|
||||||
'exception': exc}
|
'exception': exc}
|
||||||
notifier.error.assert_called_once_with(context, 'raise_exc', payload)
|
notifier.error.assert_called_once_with(context, 'raise_exc', payload)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user