forked from VimPlug/jedi
Move some import parts around to refactor it
This commit is contained in:
+22
-17
@@ -308,6 +308,23 @@ class Importer(object):
|
|||||||
except KeyError:
|
except KeyError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
def _find_module(*args, **kwargs):
|
||||||
|
module_file, module_path, is_pkg = find_module(*args, **kwargs)
|
||||||
|
|
||||||
|
code = None
|
||||||
|
if is_pkg:
|
||||||
|
# In this case, we don't have a file yet. Search for the
|
||||||
|
# __init__ file.
|
||||||
|
if module_path.endswith(('.zip', '.egg')):
|
||||||
|
code = module_file.loader.get_source(module_name)
|
||||||
|
else:
|
||||||
|
module_path = get_init_path(module_path)
|
||||||
|
elif module_file:
|
||||||
|
code = module_file.read()
|
||||||
|
module_file.close()
|
||||||
|
|
||||||
|
return code, module_path, is_pkg
|
||||||
|
|
||||||
if len(import_path) > 1:
|
if len(import_path) > 1:
|
||||||
# This is a recursive way of importing that works great with
|
# This is a recursive way of importing that works great with
|
||||||
# the module cache.
|
# the module cache.
|
||||||
@@ -341,8 +358,8 @@ class Importer(object):
|
|||||||
try:
|
try:
|
||||||
if not isinstance(path, list):
|
if not isinstance(path, list):
|
||||||
path = [path]
|
path = [path]
|
||||||
module_file, module_path, is_pkg = \
|
code, module_path, is_pkg = \
|
||||||
find_module(import_parts[-1], path, fullname=module_name)
|
_find_module(import_parts[-1], path, fullname=module_name)
|
||||||
break
|
break
|
||||||
except ImportError:
|
except ImportError:
|
||||||
module_path = None
|
module_path = None
|
||||||
@@ -357,8 +374,8 @@ class Importer(object):
|
|||||||
# Injecting the path directly into `find_module` did not work.
|
# Injecting the path directly into `find_module` did not work.
|
||||||
sys.path, temp = sys_path, sys.path
|
sys.path, temp = sys_path, sys.path
|
||||||
try:
|
try:
|
||||||
module_file, module_path, is_pkg = \
|
code, module_path, is_pkg = \
|
||||||
find_module(import_parts[-1], fullname=module_name)
|
_find_module(import_parts[-1], fullname=module_name)
|
||||||
finally:
|
finally:
|
||||||
sys.path = temp
|
sys.path = temp
|
||||||
except ImportError:
|
except ImportError:
|
||||||
@@ -366,18 +383,6 @@ class Importer(object):
|
|||||||
_add_error(self.module_context, import_path[-1])
|
_add_error(self.module_context, import_path[-1])
|
||||||
return NO_CONTEXTS
|
return NO_CONTEXTS
|
||||||
|
|
||||||
code = None
|
|
||||||
if is_pkg:
|
|
||||||
# In this case, we don't have a file yet. Search for the
|
|
||||||
# __init__ file.
|
|
||||||
if module_path.endswith(('.zip', '.egg')):
|
|
||||||
code = module_file.loader.get_source(module_name)
|
|
||||||
else:
|
|
||||||
module_path = get_init_path(module_path)
|
|
||||||
elif module_file:
|
|
||||||
code = module_file.read()
|
|
||||||
module_file.close()
|
|
||||||
|
|
||||||
if isinstance(module_path, ImplicitNSInfo):
|
if isinstance(module_path, ImplicitNSInfo):
|
||||||
from jedi.evaluate.context.namespace import ImplicitNamespaceContext
|
from jedi.evaluate.context.namespace import ImplicitNamespaceContext
|
||||||
module = ImplicitNamespaceContext(
|
module = ImplicitNamespaceContext(
|
||||||
@@ -385,7 +390,7 @@ class Importer(object):
|
|||||||
fullname=module_path.name,
|
fullname=module_path.name,
|
||||||
paths=module_path.paths,
|
paths=module_path.paths,
|
||||||
)
|
)
|
||||||
elif module_file is not None or module_path.endswith(('.py', '.zip', '.egg')):
|
elif code is not None or module_path.endswith(('.py', '.zip', '.egg')):
|
||||||
module = _load_module(self._evaluator, module_path, code, sys_path, parent_module)
|
module = _load_module(self._evaluator, module_path, code, sys_path, parent_module)
|
||||||
else:
|
else:
|
||||||
module = compiled.load_module(self._evaluator, path=module_path, sys_path=sys_path)
|
module = compiled.load_module(self._evaluator, path=module_path, sys_path=sys_path)
|
||||||
|
|||||||
Reference in New Issue
Block a user