forked from VimPlug/jedi
Fix test_imports tests, now zip imports work again
This commit is contained in:
+52
-54
@@ -16,6 +16,7 @@ import os
|
||||
from parso.python import tree
|
||||
from parso.tree import search_ancestor
|
||||
from parso import python_bytes_to_unicode
|
||||
from parso.file_io import KnownContentFileIO
|
||||
|
||||
from jedi._compatibility import (FileNotFoundError, ImplicitNSInfo,
|
||||
force_unicode, unicode)
|
||||
@@ -441,24 +442,20 @@ def import_module(evaluator, import_names, parent_module_context, sys_path):
|
||||
This method is very similar to importlib's `_gcd_import`.
|
||||
"""
|
||||
if import_names[0] in settings.auto_import_modules:
|
||||
module = _load_module(
|
||||
evaluator,
|
||||
import_names=import_names,
|
||||
sys_path=sys_path,
|
||||
)
|
||||
module = _load_builtin_module(evaluator, import_names, sys_path)
|
||||
return ContextSet([module])
|
||||
|
||||
module_name = '.'.join(import_names)
|
||||
if parent_module_context is None:
|
||||
# Override the sys.path. It works only good that way.
|
||||
# Injecting the path directly into `find_module` did not work.
|
||||
code, module_path, is_pkg = evaluator.compiled_subprocess.get_module_info(
|
||||
file_io_or_ns, is_pkg = evaluator.compiled_subprocess.get_module_info(
|
||||
string=import_names[-1],
|
||||
full_name=module_name,
|
||||
sys_path=sys_path,
|
||||
is_global_search=True,
|
||||
)
|
||||
if module_path is None:
|
||||
if is_pkg is None:
|
||||
raise JediImportError(import_names)
|
||||
else:
|
||||
try:
|
||||
@@ -473,23 +470,32 @@ def import_module(evaluator, import_names, parent_module_context, sys_path):
|
||||
# not important to be correct.
|
||||
if not isinstance(path, list):
|
||||
path = [path]
|
||||
code, module_path, is_pkg = evaluator.compiled_subprocess.get_module_info(
|
||||
file_io_or_ns, is_pkg = evaluator.compiled_subprocess.get_module_info(
|
||||
string=import_names[-1],
|
||||
path=path,
|
||||
full_name=module_name,
|
||||
is_global_search=False,
|
||||
)
|
||||
if module_path is not None:
|
||||
if is_pkg is not None:
|
||||
break
|
||||
else:
|
||||
raise JediImportError(import_names)
|
||||
|
||||
module = _load_module(
|
||||
evaluator, module_path, code, sys_path,
|
||||
import_names=import_names,
|
||||
safe_module_name=True,
|
||||
is_package=is_pkg,
|
||||
)
|
||||
if isinstance(file_io_or_ns, ImplicitNSInfo):
|
||||
from jedi.evaluate.context.namespace import ImplicitNamespaceContext
|
||||
module = ImplicitNamespaceContext(
|
||||
evaluator,
|
||||
fullname=file_io_or_ns.name,
|
||||
paths=file_io_or_ns.paths,
|
||||
)
|
||||
elif file_io_or_ns is None:
|
||||
module = _load_builtin_module(evaluator, import_names, sys_path)
|
||||
else:
|
||||
module = _load_python_module(
|
||||
evaluator, file_io_or_ns, sys_path,
|
||||
import_names=import_names,
|
||||
is_package=is_pkg,
|
||||
)
|
||||
|
||||
if parent_module_context is None:
|
||||
debug.dbg('global search_module %s: %s', import_names[-1], module)
|
||||
@@ -498,49 +504,41 @@ def import_module(evaluator, import_names, parent_module_context, sys_path):
|
||||
return ContextSet([module])
|
||||
|
||||
|
||||
def _load_module(evaluator, path=None, code=None, sys_path=None,
|
||||
import_names=None, safe_module_name=False, is_package=False):
|
||||
if import_names is None:
|
||||
dotted_name = None
|
||||
else:
|
||||
dotted_name = '.'.join(import_names)
|
||||
def _load_python_module(evaluator, file_io, sys_path=None,
|
||||
import_names=None, is_package=False):
|
||||
try:
|
||||
return evaluator.module_cache.get_from_path(path)
|
||||
return evaluator.module_cache.get_from_path(file_io.path)
|
||||
except KeyError:
|
||||
pass
|
||||
|
||||
if isinstance(path, ImplicitNSInfo):
|
||||
from jedi.evaluate.context.namespace import ImplicitNamespaceContext
|
||||
module = ImplicitNamespaceContext(
|
||||
evaluator,
|
||||
fullname=path.name,
|
||||
paths=path.paths,
|
||||
)
|
||||
else:
|
||||
if sys_path is None:
|
||||
sys_path = evaluator.get_sys_path()
|
||||
module_node = evaluator.parse(
|
||||
file_io=file_io,
|
||||
cache=True,
|
||||
diff_cache=settings.fast_parser,
|
||||
cache_path=settings.cache_directory
|
||||
)
|
||||
|
||||
if path is not None and path.endswith(('.py', '.zip', '.egg')):
|
||||
module_node = evaluator.parse(
|
||||
code=code, path=path, cache=True,
|
||||
diff_cache=settings.fast_parser,
|
||||
cache_path=settings.cache_directory)
|
||||
from jedi.evaluate.context import ModuleContext
|
||||
return ModuleContext(
|
||||
evaluator, module_node,
|
||||
path=file_io.path,
|
||||
string_names=import_names,
|
||||
code_lines=get_cached_code_lines(evaluator.grammar, file_io.path),
|
||||
is_package=is_package,
|
||||
)
|
||||
|
||||
from jedi.evaluate.context import ModuleContext
|
||||
module = ModuleContext(
|
||||
evaluator, module_node,
|
||||
path=path,
|
||||
string_names=import_names,
|
||||
code_lines=get_cached_code_lines(evaluator.grammar, path),
|
||||
is_package=is_package,
|
||||
)
|
||||
else:
|
||||
assert dotted_name is not None
|
||||
module = compiled.load_module(evaluator, dotted_name=dotted_name, sys_path=sys_path)
|
||||
if module is None:
|
||||
# The file might raise an ImportError e.g. and therefore not be
|
||||
# importable.
|
||||
raise JediImportError(import_names)
|
||||
|
||||
def _load_builtin_module(evaluator, import_names=None, sys_path=None):
|
||||
if sys_path is None:
|
||||
sys_path = evaluator.get_sys_path()
|
||||
|
||||
dotted_name = '.'.join(import_names)
|
||||
assert dotted_name is not None
|
||||
module = compiled.load_module(evaluator, dotted_name=dotted_name, sys_path=sys_path)
|
||||
if module is None:
|
||||
# The file might raise an ImportError e.g. and therefore not be
|
||||
# importable.
|
||||
raise JediImportError(import_names)
|
||||
return module
|
||||
|
||||
|
||||
@@ -576,8 +574,8 @@ def get_modules_containing_name(evaluator, modules, name):
|
||||
else:
|
||||
import_names, is_package = sys_path.transform_path_to_dotted(e_sys_path, path)
|
||||
|
||||
module = _load_module(
|
||||
evaluator, path, code,
|
||||
module = _load_python_module(
|
||||
evaluator, KnownContentFileIO(path, code),
|
||||
sys_path=e_sys_path,
|
||||
import_names=import_names,
|
||||
is_package=is_package,
|
||||
|
||||
Reference in New Issue
Block a user