1
0
forked from VimPlug/jedi

The import resolution for namespace packages was wrong

With this change we can now include all parents of the script, which will make
relative imports always work.

Now the whole meta_path is scanned and not just importlib's PathFinder.

Fixes #1183.
This commit is contained in:
Dave Halter
2018-07-21 00:16:10 +02:00
parent ad5170a37a
commit 4b276bae87
6 changed files with 56 additions and 22 deletions
+24 -12
View File
@@ -34,24 +34,36 @@ class DummyFile(object):
del self.loader
def find_module_py34(string, path=None, full_name=None):
def find_module_py34(string, path=None, full_name=None, is_global_search=True):
spec = None
loader = None
spec = importlib.machinery.PathFinder.find_spec(string, path)
if spec is not None:
# We try to disambiguate implicit namespace pkgs with non implicit namespace pkgs
if not spec.has_location:
full_name = string if not path else full_name
implicit_ns_info = ImplicitNSInfo(full_name, spec.submodule_search_locations._path)
return None, implicit_ns_info, False
for finder in sys.meta_path:
if is_global_search and finder != importlib.machinery.PathFinder:
p = None
else:
p = path
try:
find_spec = finder.find_spec
except AttributeError:
# These are old-school clases that still have a different API, just
# ignore those.
continue
spec = find_spec(string, p)
if spec is not None:
loader = spec.loader
if loader is None and not spec.has_location:
# This is a namespace package.
full_name = string if not path else full_name
implicit_ns_info = ImplicitNSInfo(full_name, spec.submodule_search_locations._path)
return None, implicit_ns_info, False
break
# we have found the tail end of the dotted path
loader = spec.loader
return find_module_py33(string, path, loader)
def find_module_py33(string, path=None, loader=None, full_name=None):
def find_module_py33(string, path=None, loader=None, full_name=None, is_global_search=True):
loader = loader or importlib.machinery.PathFinder.find_module(string, path)
if loader is None and path is None: # Fallback to find builtins
@@ -104,7 +116,7 @@ def find_module_py33(string, path=None, loader=None, full_name=None):
return module_file, module_path, is_package
def find_module_pre_py33(string, path=None, full_name=None):
def find_module_pre_py33(string, path=None, full_name=None, is_global_search=True):
# This import is here, because in other places it will raise a
# DeprecationWarning.
import imp