From 150b9182512fa0a4f157586e040a7f67c1851005 Mon Sep 17 00:00:00 2001 From: likui Date: Mon, 10 May 2021 18:10:56 +0800 Subject: [PATCH] Replace getargspec with getfullargspec inspect.getargspec() is deprecated since py3 [1] https://docs.python.org/3/library/inspect.html#inspect.getargspec Change-Id: I6fbdf942dd192351e135430997c15ea92e7fb8af --- nova/cmd/common.py | 4 ++-- nova/network/neutron.py | 3 ++- nova/test.py | 6 +++--- nova/tests/unit/objects/test_objects.py | 5 +++-- nova/tests/unit/virt/libvirt/test_imagebackend.py | 8 +++++--- nova/utils.py | 8 +------- 6 files changed, 16 insertions(+), 18 deletions(-) diff --git a/nova/cmd/common.py b/nova/cmd/common.py index c3f62fa57e70..1d4d5b4612f8 100644 --- a/nova/cmd/common.py +++ b/nova/cmd/common.py @@ -18,6 +18,7 @@ """ import argparse +import inspect import traceback from oslo_log import log as logging @@ -26,7 +27,6 @@ import nova.conf import nova.db.api from nova import exception from nova.i18n import _ -from nova import utils CONF = nova.conf.CONF LOG = logging.getLogger(__name__) @@ -65,7 +65,7 @@ def validate_args(fn, *args, **kwargs): :param arg: the positional arguments supplied :param kwargs: the keyword arguments supplied """ - argspec = utils.getargspec(fn) + argspec = inspect.getfullargspec(fn) num_defaults = len(argspec.defaults or []) required_args = argspec.args[:len(argspec.args) - num_defaults] diff --git a/nova/network/neutron.py b/nova/network/neutron.py index 9c3186b52533..7db03a41ee2d 100644 --- a/nova/network/neutron.py +++ b/nova/network/neutron.py @@ -20,6 +20,7 @@ API and utilities for nova-network interactions. import copy import functools +import inspect import time import typing as ty @@ -133,7 +134,7 @@ def refresh_cache(f): Requires context and instance as function args """ - argspec = utils.getargspec(f) + argspec = inspect.getfullargspec(f) @functools.wraps(f) def wrapper(self, context, *args, **kwargs): diff --git a/nova/test.py b/nova/test.py index 1bcffd44ba24..baa19589cf20 100644 --- a/nova/test.py +++ b/nova/test.py @@ -635,8 +635,8 @@ class TestCase(base.BaseTestCase): for name in sorted(implmethods.keys()): # NOTE(stephenfin): We ignore type annotations - baseargs = utils.getargspec(basemethods[name])[:-1] - implargs = utils.getargspec(implmethods[name])[:-1] + baseargs = inspect.getfullargspec(basemethods[name])[:-1] + implargs = inspect.getfullargspec(implmethods[name])[:-1] self.assertEqual(baseargs, implargs, "%s args don't match base class %s" % @@ -709,7 +709,7 @@ class SubclassSignatureTestCase(testtools.TestCase, metaclass=abc.ABCMeta): # instead. method = getattr(method, '__wrapped__') - argspecs[name] = utils.getargspec(method) + argspecs[name] = inspect.getfullargspec(method) return argspecs diff --git a/nova/tests/unit/objects/test_objects.py b/nova/tests/unit/objects/test_objects.py index b2f4f0616d01..13d32708947a 100644 --- a/nova/tests/unit/objects/test_objects.py +++ b/nova/tests/unit/objects/test_objects.py @@ -16,6 +16,7 @@ import collections import contextlib import copy import datetime +import inspect import os import pprint @@ -1318,12 +1319,12 @@ class TestObjEqualPrims(_BaseTestCase): class TestObjMethodOverrides(test.NoDBTestCase): def test_obj_reset_changes(self): - args = utils.getargspec(base.NovaObject.obj_reset_changes) + args = inspect.getfullargspec(base.NovaObject.obj_reset_changes) obj_classes = base.NovaObjectRegistry.obj_classes() for obj_name in obj_classes: obj_class = obj_classes[obj_name][0] self.assertEqual(args, - utils.getargspec(obj_class.obj_reset_changes)) + inspect.getfullargspec(obj_class.obj_reset_changes)) class TestObjectsDefaultingOnInit(test.NoDBTestCase): diff --git a/nova/tests/unit/virt/libvirt/test_imagebackend.py b/nova/tests/unit/virt/libvirt/test_imagebackend.py index 2acd7e0afff8..e9f2707f735f 100644 --- a/nova/tests/unit/virt/libvirt/test_imagebackend.py +++ b/nova/tests/unit/virt/libvirt/test_imagebackend.py @@ -15,6 +15,7 @@ import base64 import errno +import inspect import os import shutil import tempfile @@ -38,7 +39,6 @@ from nova import objects from nova.storage import rbd_utils from nova import test from nova.tests.unit import fake_processutils -from nova import utils from nova.virt.image import model as imgmodel from nova.virt import images from nova.virt.libvirt import config as vconfig @@ -1488,8 +1488,10 @@ class RbdTestCase(_ImageTestCase, test.NoDBTestCase): self.assertEqual(fake_processutils.fake_execute_get_log(), []) def test_parent_compatible(self): - self.assertEqual(utils.getargspec(imagebackend.Image.libvirt_info), - utils.getargspec(self.image_class.libvirt_info)) + self.assertEqual( + inspect.getfullargspec(imagebackend.Image.libvirt_info), + inspect.getfullargspec(self.image_class.libvirt_info) + ) def test_image_path(self): diff --git a/nova/utils.py b/nova/utils.py index 5239dd4ba8f9..c1b4fccc475b 100644 --- a/nova/utils.py +++ b/nova/utils.py @@ -80,12 +80,6 @@ _FILE_CACHE = {} _SERVICE_TYPES = service_types.ServiceTypes() -if hasattr(inspect, 'getfullargspec'): - getargspec = inspect.getfullargspec -else: - getargspec = inspect.getargspec - - # NOTE(mikal): this seems to have to stay for now to handle os-brick # requirements. This makes me a sad panda. def get_root_helper(): @@ -553,7 +547,7 @@ def expects_func_args(*args): @functools.wraps(dec) def _decorator(f): base_f = safe_utils.get_wrapped_function(f) - argspec = getargspec(base_f) + argspec = inspect.getfullargspec(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