From 473be114f3781562b2b82b717ce73e19d22d06a5 Mon Sep 17 00:00:00 2001 From: Dave Halter Date: Sat, 23 Dec 2017 17:10:57 +0100 Subject: [PATCH] Move even more import stuff to a separate function --- jedi/evaluate/imports.py | 49 +++++++++++++++++++++++----------------- 1 file changed, 28 insertions(+), 21 deletions(-) diff --git a/jedi/evaluate/imports.py b/jedi/evaluate/imports.py index aadc2162..912a9ae6 100644 --- a/jedi/evaluate/imports.py +++ b/jedi/evaluate/imports.py @@ -308,8 +308,16 @@ class Importer(object): except KeyError: pass - def _find_module(*args, **kwargs): - module_file, module_path, is_pkg = find_module(*args, **kwargs) + def _find_module(sys_path=None, **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 if is_pkg: @@ -355,30 +363,29 @@ class Importer(object): for path in paths: # At the moment we are only using one path. So this is # not important to be correct. - try: - if not isinstance(path, list): - path = [path] - code, module_path, is_pkg = \ - _find_module(import_parts[-1], path, fullname=module_name) + if not isinstance(path, list): + path = [path] + code, module_path, is_pkg = _find_module( + string=import_parts[-1], + path=path, + fullname=module_name + ) + if module_path is not None: break - except ImportError: - module_path = None - if module_path is None: + else: _add_error(self.module_context, import_path[-1]) return NO_CONTEXTS else: parent_module = None - try: - debug.dbg('search_module %s in %s', import_parts[-1], self.file_path) - # Override the sys.path. It works only good that way. - # Injecting the path directly into `find_module` did not work. - sys.path, temp = sys_path, sys.path - try: - code, module_path, is_pkg = \ - _find_module(import_parts[-1], fullname=module_name) - finally: - sys.path = temp - except ImportError: + debug.dbg('search_module %s in %s', import_parts[-1], self.file_path) + # 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 = _find_module( + string=import_parts[-1], + fullname=module_name, + sys_path=sys_path, + ) + if module_path is None: # The module is not a package. _add_error(self.module_context, import_path[-1]) return NO_CONTEXTS