From 60234e68cacd016089e40b92cf218dc4fce65624 Mon Sep 17 00:00:00 2001 From: Dave Halter Date: Tue, 29 Nov 2016 18:28:28 +0100 Subject: [PATCH] Fixed sys path scanning again. --- jedi/api/classes.py | 2 +- jedi/evaluate/representation.py | 1 - jedi/evaluate/sys_path.py | 27 ++++++++++++++++++--------- 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/jedi/api/classes.py b/jedi/api/classes.py index 88e38462..9dd74746 100644 --- a/jedi/api/classes.py +++ b/jedi/api/classes.py @@ -71,7 +71,7 @@ class BaseDefinition(object): if self.in_builtin_module(): self.module_path = None else: - self.module_path = self._module.path + self.module_path = self._module.py__file__() """Shows the file path of a module. e.g. ``/usr/lib/python2.7/os.py``""" @property diff --git a/jedi/evaluate/representation.py b/jedi/evaluate/representation.py index a7219907..968f922c 100644 --- a/jedi/evaluate/representation.py +++ b/jedi/evaluate/representation.py @@ -449,7 +449,6 @@ class ModuleContext(use_metaclass(CachedMetaClass, context.TreeContext)): def __init__(self, evaluator, module_node): super(ModuleContext, self).__init__(evaluator, parent_context=None) self.module_node = module_node - self.path = None def get_node(self): return self.module_node diff --git a/jedi/evaluate/sys_path.py b/jedi/evaluate/sys_path.py index 658f66a8..b35d9728 100644 --- a/jedi/evaluate/sys_path.py +++ b/jedi/evaluate/sys_path.py @@ -9,6 +9,7 @@ from jedi.parser import ParserWithRecovery from jedi.evaluate.cache import memoize_default from jedi import debug from jedi import common +from jedi.evaluate.compiled import CompiledObject from jedi.parser.utils import load_parser, save_parser @@ -146,7 +147,7 @@ def _paths_from_list_modifications(module_path, trailer1, trailer2): return _execute_code(module_path, arg.get_code()) -def _check_module(evaluator, module): +def _check_module(evaluator, module_context): """ Detect sys.path modifications within module. """ @@ -162,8 +163,11 @@ def _check_module(evaluator, module): yield name, power sys_path = list(evaluator.sys_path) # copy + if isinstance(module_context, CompiledObject): + return sys_path + try: - possible_names = module.used_names['path'] + possible_names = module_context.get_node().used_names['path'] except KeyError: # module.used_names is MergedNamesDict whose getitem never throws # keyerror, this is superfluous. @@ -172,15 +176,20 @@ def _check_module(evaluator, module): for name, power in get_sys_path_powers(possible_names): stmt = name.get_definition() if len(power.children) >= 4: - sys_path.extend(_paths_from_list_modifications(module.path, *power.children[2:4])) + sys_path.extend( + _paths_from_list_modifications( + module_context.py__file__(), *power.children[2:4] + ) + ) elif name.get_definition().type == 'expr_stmt': sys_path.extend(_paths_from_assignment(evaluator, stmt)) return sys_path @memoize_default(evaluator_is_first_arg=True, default=[]) -def sys_path_with_modifications(evaluator, module): - if module.path is None: +def sys_path_with_modifications(evaluator, module_context): + path = module_context.py__file__() + if path is None: # Support for modules without a path is bad, therefore return the # normal path. return list(evaluator.sys_path) @@ -188,13 +197,13 @@ def sys_path_with_modifications(evaluator, module): curdir = os.path.abspath(os.curdir) #TODO why do we need a chdir? with common.ignored(OSError): - os.chdir(os.path.dirname(module.path)) + os.chdir(os.path.dirname(path)) buildout_script_paths = set() - result = _check_module(evaluator, module) - result += _detect_django_path(module.path) - for buildout_script in _get_buildout_scripts(module.path): + result = _check_module(evaluator, module_context) + result += _detect_django_path(path) + for buildout_script in _get_buildout_scripts(path): for path in _get_paths_from_buildout_script(evaluator, buildout_script): buildout_script_paths.add(path) # cleanup, back to old directory