From 6b9a7ed82cef0b89edb00b19e991fe6ec5d057d5 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Wed, 24 Aug 2016 09:44:03 +0300 Subject: Add support for discovering .pth file created by certain namespace packages. --- astroid/modutils.py | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) (limited to 'astroid/modutils.py') diff --git a/astroid/modutils.py b/astroid/modutils.py index 77081b5b..3334e1e3 100644 --- a/astroid/modutils.py +++ b/astroid/modutils.py @@ -742,6 +742,22 @@ class ImpFinder(Finder): return path +class DynamicImpFinder(ImpFinder): + + def find_module(self, modname, module_parts, processed, submodule_path): + if _is_namespace(modname) and modname in sys.modules: + submodule_path = sys.modules[modname].__path__ + return ModuleSpec(name=modname, location='', + origin='namespace', + type=ModuleType.PY_NAMESPACE, + submodule_search_locations=submodule_path) + + + def contribute_to_path(self, spec, processed): + return spec.submodule_search_locations + + + class ZipFinder(Finder): def __init__(self, path): @@ -776,12 +792,17 @@ class PEP420SpecFinder(Finder): return spec.submodule_search_locations return None +_SPEC_FINDERS = ( + ImpFinder, + ZipFinder, +) +if _HAS_MACHINERY and sys.version_info[:2] > (3, 3): + _SPEC_FINDERS += (PEP420SpecFinder, ) +_SPEC_FINDERS += (DynamicImpFinder, ) -def _find_spec_with_path(search_path, modname, module_parts, processed, submodule_path): - finders = [finder(search_path) for finder in (ImpFinder, ZipFinder)] - if _HAS_MACHINERY and sys.version_info[:2] > (3, 3): - finders.append(PEP420SpecFinder(search_path)) +def _find_spec_with_path(search_path, modname, module_parts, processed, submodule_path): + finders = [finder(search_path) for finder in _SPEC_FINDERS] for finder in finders: spec = finder.find_module(modname, module_parts, processed, submodule_path) if spec is None: -- cgit v1.2.1