Move even more import stuff to a separate function

This commit is contained in:
Dave Halter
2017-12-23 17:10:57 +01:00
parent e2f8d53ee4
commit 473be114f3
+28 -21
View File
@@ -308,8 +308,16 @@ class Importer(object):
except KeyError: except KeyError:
pass pass
def _find_module(*args, **kwargs): def _find_module(sys_path=None, **kwargs):
module_file, module_path, is_pkg = find_module(*args, **kwargs) if sys_path is not None:
sys.path, temp = sys_path, sys.path
try:
module_file, module_path, is_pkg = find_module(**kwargs)
except ImportError:
return None, None, None
finally:
if sys_path is not None:
sys.path = temp
code = None code = None
if is_pkg: if is_pkg:
@@ -355,30 +363,29 @@ class Importer(object):
for path in paths: for path in paths:
# At the moment we are only using one path. So this is # At the moment we are only using one path. So this is
# not important to be correct. # not important to be correct.
try: if not isinstance(path, list):
if not isinstance(path, list): path = [path]
path = [path] code, module_path, is_pkg = _find_module(
code, module_path, is_pkg = \ string=import_parts[-1],
_find_module(import_parts[-1], path, fullname=module_name) path=path,
fullname=module_name
)
if module_path is not None:
break break
except ImportError: else:
module_path = None
if module_path is None:
_add_error(self.module_context, import_path[-1]) _add_error(self.module_context, import_path[-1])
return NO_CONTEXTS return NO_CONTEXTS
else: else:
parent_module = None parent_module = None
try: debug.dbg('search_module %s in %s', import_parts[-1], self.file_path)
debug.dbg('search_module %s in %s', import_parts[-1], self.file_path) # Override the sys.path. It works only good that way.
# Override the sys.path. It works only good that way. # Injecting the path directly into `find_module` did not work.
# Injecting the path directly into `find_module` did not work. code, module_path, is_pkg = _find_module(
sys.path, temp = sys_path, sys.path string=import_parts[-1],
try: fullname=module_name,
code, module_path, is_pkg = \ sys_path=sys_path,
_find_module(import_parts[-1], fullname=module_name) )
finally: if module_path is None:
sys.path = temp
except ImportError:
# The module is not a package. # The module is not a package.
_add_error(self.module_context, import_path[-1]) _add_error(self.module_context, import_path[-1])
return NO_CONTEXTS return NO_CONTEXTS