From 8a3a8d6fe4a8680126a62f90f619da5093c937e4 Mon Sep 17 00:00:00 2001 From: Zane Bitter Date: Wed, 5 Dec 2012 11:15:42 +0100 Subject: [PATCH] Fix importing of quantum resources subpackage Apparently pkgutil.walk_packages(path) will return packages outside the given path if they have the same names as the ones inside - so having a quantum subpackage results in importing the global quantum package from Quantum itself instead. Fix this by passing a package prefix to give a fully qualified name. Also make sure that submodules are added as an attribute to their immediate parent package, not to some grandparent package with '.'s in the attribute name. bug 1085725 Change-Id: Ie571100898a33ed4fe15d1878c7b16db4caf4fa5 Signed-off-by: Zane Bitter --- heat/common/plugin_loader.py | 21 ++++++++++++++------- heat/tests/test_plugin_loader.py | 5 +++-- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/heat/common/plugin_loader.py b/heat/common/plugin_loader.py index 04f8f24b..c68b310d 100644 --- a/heat/common/plugin_loader.py +++ b/heat/common/plugin_loader.py @@ -59,27 +59,34 @@ def _import_module(importer, module_name, package): Import a module dynamically into the specified package, given its name and PEP302 Importer object (which knows the path to look in). ''' - fullname = _module_name(package.__name__, module_name) # Duplicate copies of modules are bad, so check if this has already been # imported statically - if fullname in sys.modules: - return sys.modules[fullname] + if module_name in sys.modules: + return sys.modules[module_name] - loader = importer.find_module(fullname) + loader = importer.find_module(module_name) if loader is None: return None - module = loader.load_module(fullname) + module = loader.load_module(module_name) + # Make this accessible through the parent package for static imports - setattr(package, module_name, module) + local_name = module_name.partition(package.__name__ + '.')[2] + module_components = local_name.split('.') + parent = reduce(getattr, module_components[:-1], package) + setattr(parent, module_components[-1], module) + return module def load_modules(package, ignore_error=False): '''Dynamically load all modules from a given package.''' path = package.__path__ - for importer, module_name, is_package in pkgutil.walk_packages(path): + pkg_prefix = package.__name__ + '.' + + for importer, module_name, is_package in pkgutil.walk_packages(path, + pkg_prefix): try: module = _import_module(importer, module_name, package) except ImportError as ex: diff --git a/heat/tests/test_plugin_loader.py b/heat/tests/test_plugin_loader.py index d458d324..ca4feced 100644 --- a/heat/tests/test_plugin_loader.py +++ b/heat/tests/test_plugin_loader.py @@ -56,11 +56,12 @@ class PluginLoaderTest(unittest.TestCase): def test_import_module_existing(self): import heat.engine.service + existing = heat.engine.service importer = pkgutil.ImpImporter(heat.engine.__path__[0]) loaded = plugin_loader._import_module(importer, - 'service', + 'heat.engine.service', heat.engine) - self.assertTrue(loaded is heat.engine.service) + self.assertTrue(loaded is existing) def test_import_module_garbage(self): importer = pkgutil.ImpImporter(heat.engine.__path__[0]) -- 2.45.2