Different _load_module API

This commit is contained in:
Dave Halter
2018-07-05 10:15:49 +02:00
parent 508ed7e5b8
commit d8c0d8e5d2
3 changed files with 18 additions and 13 deletions

View File

@@ -145,9 +145,9 @@ class Script(object):
def _get_module(self):
name = '__main__'
if self.path is not None:
n = dotted_path_in_sys_path(self._evaluator.get_sys_path(), self.path)
if n is not None:
name = n
import_names = dotted_path_in_sys_path(self._evaluator.get_sys_path(), self.path)
if import_names is not None:
name = '.'.join(import_names)
module = ModuleContext(
self._evaluator, self._module_node, self.path,

View File

@@ -373,7 +373,7 @@ class Importer(object):
module = _load_module(
self._evaluator, module_path, code, sys_path,
module_name=module_name,
import_names=import_parts,
safe_module_name=True,
)
@@ -473,9 +473,13 @@ class Importer(object):
def _load_module(evaluator, path=None, code=None, sys_path=None,
module_name=None, safe_module_name=False, auto_import=False):
import_names=None, safe_module_name=False, auto_import=False):
if import_names is None:
dotted_name = None
else:
dotted_name = '.'.join(import_names)
try:
return evaluator.module_cache.get(module_name)
return evaluator.module_cache.get(dotted_name)
except KeyError:
pass
try:
@@ -508,10 +512,11 @@ def _load_module(evaluator, path=None, code=None, sys_path=None,
code_lines=get_cached_code_lines(evaluator.grammar, path),
)
else:
module = compiled.load_module(evaluator, dotted_name=module_name, sys_path=sys_path)
assert dotted_name is not None
module = compiled.load_module(evaluator, dotted_name=dotted_name, sys_path=sys_path)
if module is not None and module_name is not None:
add_module_to_cache(evaluator, module_name, module, safe=safe_module_name)
if module is not None and dotted_name is not None:
add_module_to_cache(evaluator, dotted_name, module, safe=safe_module_name)
return module
@@ -550,11 +555,11 @@ def get_modules_containing_name(evaluator, modules, name):
code = python_bytes_to_unicode(f.read(), errors='replace')
if name in code:
e_sys_path = evaluator.get_sys_path()
module_name = sys_path.dotted_path_in_sys_path(e_sys_path, path)
import_names = sys_path.dotted_path_in_sys_path(e_sys_path, path)
module = _load_module(
evaluator, path, code,
sys_path=e_sys_path,
module_name=module_name
import_names=import_names,
)
return module

View File

@@ -198,7 +198,7 @@ def _get_buildout_script_paths(search_path):
def dotted_path_in_sys_path(sys_path, module_path):
"""
Returns the dotted path inside a sys.path.
Returns the dotted path inside a sys.path as a list of names.
"""
# First remove the suffix.
for suffix in all_suffixes():
@@ -221,6 +221,6 @@ def dotted_path_in_sys_path(sys_path, module_path):
for string in split:
if not string or '.' in string:
return None
return '.'.join(split)
return split
return None