Merge "common: Replace deprecated importlib APIs (1/2)"
This commit is contained in:
commit
fc94b3e49a
@ -66,11 +66,11 @@ def _import_module(importer, module_name, package):
|
|||||||
if module_name in sys.modules:
|
if module_name in sys.modules:
|
||||||
return sys.modules[module_name]
|
return sys.modules[module_name]
|
||||||
|
|
||||||
loader = importer.find_module(module_name)
|
module_spec = importer.find_spec(module_name)
|
||||||
if loader is None:
|
if module_spec is None:
|
||||||
return 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
|
# Make this accessible through the parent package for static imports
|
||||||
local_name = module_name.partition(package.__name__ + '.')[2]
|
local_name = module_name.partition(package.__name__ + '.')[2]
|
||||||
|
@ -12,7 +12,7 @@
|
|||||||
# under the License.
|
# under the License.
|
||||||
|
|
||||||
|
|
||||||
import pkgutil
|
import importlib.machinery
|
||||||
import sys
|
import sys
|
||||||
from unittest import mock
|
from unittest import mock
|
||||||
|
|
||||||
@ -53,14 +53,14 @@ class PluginLoaderTest(common.HeatTestCase):
|
|||||||
def test_import_module_existing(self):
|
def test_import_module_existing(self):
|
||||||
import heat.engine.service
|
import heat.engine.service
|
||||||
existing = 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,
|
loaded = plugin_loader._import_module(importer,
|
||||||
'heat.engine.service',
|
'heat.engine.service',
|
||||||
heat.engine)
|
heat.engine)
|
||||||
self.assertIs(existing, loaded)
|
self.assertIs(existing, loaded)
|
||||||
|
|
||||||
def test_import_module_garbage(self):
|
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,
|
self.assertIsNone(plugin_loader._import_module(importer,
|
||||||
'wibble',
|
'wibble',
|
||||||
heat.engine))
|
heat.engine))
|
||||||
@ -68,7 +68,7 @@ class PluginLoaderTest(common.HeatTestCase):
|
|||||||
@mock.patch.object(plugin_loader, "_import_module", mock.MagicMock())
|
@mock.patch.object(plugin_loader, "_import_module", mock.MagicMock())
|
||||||
@mock.patch('pkgutil.walk_packages')
|
@mock.patch('pkgutil.walk_packages')
|
||||||
def test_load_modules_skip_test(self, mp):
|
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),
|
mp.return_value = ((importer, "hola.foo", None),
|
||||||
(importer, "hola.tests.test_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.object(plugin_loader, "_import_module", mock.MagicMock())
|
||||||
@mock.patch('pkgutil.walk_packages')
|
@mock.patch('pkgutil.walk_packages')
|
||||||
def test_load_modules_skip_setup(self, mp):
|
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),
|
mp.return_value = ((importer, "hola.foo", None),
|
||||||
(importer, "hola.setup", None))
|
(importer, "hola.setup", None))
|
||||||
|
Loading…
Reference in New Issue
Block a user