common: Replace deprecated importlib APIs (1/2)

Resolve the following deprecation warning:

   DeprecationWarning: find_module() is deprecated and slated for
   removal in Python 3.12; use find_spec() instead

Signed-off-by: Stephen Finucane <stephenfin@redhat.com>
Change-Id: I88005c694b94d249bce53f46df67b51c7e01adae
This commit is contained in:
Stephen Finucane 2023-09-21 10:56:58 +01:00 committed by Takashi Kajinami
parent 346f1d7611
commit 559e840053
2 changed files with 8 additions and 8 deletions

View File

@ -66,11 +66,11 @@ def _import_module(importer, module_name, package):
if module_name in sys.modules:
return sys.modules[module_name]
loader = importer.find_module(module_name)
if loader is None:
module_spec = importer.find_spec(module_name)
if module_spec is None:
return None
module = loader.load_module(module_name)
module = module_spec.loader.load_module(module_name)
# Make this accessible through the parent package for static imports
local_name = module_name.partition(package.__name__ + '.')[2]

View File

@ -12,7 +12,7 @@
# under the License.
import pkgutil
import importlib.machinery
import sys
from unittest import mock
@ -53,14 +53,14 @@ class PluginLoaderTest(common.HeatTestCase):
def test_import_module_existing(self):
import heat.engine.service
existing = heat.engine.service
importer = pkgutil.ImpImporter(heat.engine.__path__[0])
importer = importlib.machinery.FileFinder(heat.engine.__path__[0])
loaded = plugin_loader._import_module(importer,
'heat.engine.service',
heat.engine)
self.assertIs(existing, loaded)
def test_import_module_garbage(self):
importer = pkgutil.ImpImporter(heat.engine.__path__[0])
importer = importlib.machinery.FileFinder(heat.engine.__path__[0])
self.assertIsNone(plugin_loader._import_module(importer,
'wibble',
heat.engine))
@ -68,7 +68,7 @@ class PluginLoaderTest(common.HeatTestCase):
@mock.patch.object(plugin_loader, "_import_module", mock.MagicMock())
@mock.patch('pkgutil.walk_packages')
def test_load_modules_skip_test(self, mp):
importer = pkgutil.ImpImporter(heat.engine.__path__[0])
importer = importlib.machinery.FileFinder(heat.engine.__path__[0])
mp.return_value = ((importer, "hola.foo", None),
(importer, "hola.tests.test_foo", None))
@ -79,7 +79,7 @@ class PluginLoaderTest(common.HeatTestCase):
@mock.patch.object(plugin_loader, "_import_module", mock.MagicMock())
@mock.patch('pkgutil.walk_packages')
def test_load_modules_skip_setup(self, mp):
importer = pkgutil.ImpImporter(heat.engine.__path__[0])
importer = importlib.machinery.FileFinder(heat.engine.__path__[0])
mp.return_value = ((importer, "hola.foo", None),
(importer, "hola.setup", None))