From 5ffc575796e1e042b1a5667d49a78ac436b8584c Mon Sep 17 00:00:00 2001 From: immerrr Date: Sun, 5 Apr 2015 00:19:11 +0200 Subject: [PATCH 1/2] Improve virtualenv support & egg-link resolution - add sys_path= kwarg to Script & Evaluator constructors - store sys_path for each evaluator instance - add get_venv_path(venv) function to determine virtualenv's path: - by default, dump path from live python interpreter - use old path extension variant for fallback - in old variant, use addsitedir to load .pth extension files - look for egg-link files in all directories in path --- jedi/api/__init__.py | 10 +++- jedi/evaluate/__init__.py | 10 +++- jedi/evaluate/compiled/__init__.py | 12 ++--- jedi/evaluate/imports.py | 7 ++- jedi/evaluate/sys_path.py | 85 +++++++++++++++++++++++------- test/test_regression.py | 5 +- 6 files changed, 96 insertions(+), 33 deletions(-) diff --git a/jedi/api/__init__.py b/jedi/api/__init__.py index a5b0b1098..d8bd9e77f 100644 --- a/jedi/api/__init__.py +++ b/jedi/api/__init__.py @@ -34,6 +34,7 @@ from jedi.evaluate.helpers import FakeName, get_module_names from jedi.evaluate.finder import global_names_dict_generator, filter_definition_names from jedi.evaluate import analysis +from jedi.evaluate.sys_path import get_venv_path # Jedi uses lots and lots of recursion. By setting this a little bit higher, we # can remove some "maximum recursion depth" errors. @@ -75,7 +76,8 @@ class Script(object): :type encoding: str """ def __init__(self, source=None, line=None, column=None, path=None, - encoding='utf-8', source_path=None, source_encoding=None): + encoding='utf-8', source_path=None, source_encoding=None, + sys_path=None): if source_path is not None: warnings.warn("Use path instead of source_path.", DeprecationWarning) path = source_path @@ -109,7 +111,11 @@ def __init__(self, source=None, line=None, column=None, path=None, self._parser = UserContextParser(self._grammar, self.source, path, self._pos, self._user_context, self._parsed_callback) - self._evaluator = Evaluator(self._grammar) + if sys_path is None: + venv = os.getenv('VIRTUAL_ENV') + if venv: + sys_path = list(get_venv_path(venv)) + sys.path + self._evaluator = Evaluator(self._grammar, sys_path=sys_path) debug.speed('init') def _parsed_callback(self, parser): diff --git a/jedi/evaluate/__init__.py b/jedi/evaluate/__init__.py index a959d05f1..37c20fc13 100644 --- a/jedi/evaluate/__init__.py +++ b/jedi/evaluate/__init__.py @@ -61,6 +61,7 @@ """ import copy +import sys from itertools import chain from jedi.parser import tree @@ -79,7 +80,7 @@ class Evaluator(object): - def __init__(self, grammar): + def __init__(self, grammar, sys_path=None): self.grammar = grammar self.memoize_cache = {} # for memoize decorators # To memorize modules -> equals `sys.modules`. @@ -88,6 +89,13 @@ def __init__(self, grammar): self.recursion_detector = recursion.RecursionDetector() self.execution_recursion_detector = recursion.ExecutionRecursionDetector() self.analysis = [] + if sys_path is None: + sys_path = sys.path + self.sys_path = copy.copy(sys_path) + try: + self.sys_path.remove('') + except ValueError: + pass def wrap(self, element): if isinstance(element, tree.Class): diff --git a/jedi/evaluate/compiled/__init__.py b/jedi/evaluate/compiled/__init__.py index 67d3f3711..7224067f6 100644 --- a/jedi/evaluate/compiled/__init__.py +++ b/jedi/evaluate/compiled/__init__.py @@ -10,7 +10,6 @@ from jedi._compatibility import builtins as _builtins, unicode from jedi import debug from jedi.cache import underscore_memoization, memoize_method -from jedi.evaluate.sys_path import get_sys_path from jedi.parser.tree import Param, Base, Operator, zero_position_modifier from jedi.evaluate.helpers import FakeName from . import fake @@ -309,15 +308,12 @@ def parent(self, value): pass # Just ignore this, FakeName tries to overwrite the parent attribute. -def dotted_from_fs_path(fs_path, sys_path=None): +def dotted_from_fs_path(fs_path, sys_path): """ Changes `/usr/lib/python3.4/email/utils.py` to `email.utils`. I.e. compares the path with sys.path and then returns the dotted_path. If the path is not in the sys.path, just returns None. """ - if sys_path is None: - sys_path = get_sys_path() - if os.path.basename(fs_path).startswith('__init__.'): # We are calculating the path. __init__ files are not interesting. fs_path = os.path.dirname(fs_path) @@ -341,13 +337,13 @@ def dotted_from_fs_path(fs_path, sys_path=None): return _path_re.sub('', fs_path[len(path):].lstrip(os.path.sep)).replace(os.path.sep, '.') -def load_module(path=None, name=None): +def load_module(evaluator, path=None, name=None): + sys_path = evaluator.sys_path if path is not None: - dotted_path = dotted_from_fs_path(path) + dotted_path = dotted_from_fs_path(path, sys_path=sys_path) else: dotted_path = name - sys_path = get_sys_path() if dotted_path is None: p, _, dotted_path = path.partition(os.path.sep) sys_path.insert(0, p) diff --git a/jedi/evaluate/imports.py b/jedi/evaluate/imports.py index c14a50c0d..3c2268668 100644 --- a/jedi/evaluate/imports.py +++ b/jedi/evaluate/imports.py @@ -342,7 +342,7 @@ def _do_import(self, import_path, sys_path): module_file.close() if module_file is None and not module_path.endswith('.py'): - module = compiled.load_module(module_path) + module = compiled.load_module(self._evaluator, module_path) else: module = _load_module(self._evaluator, module_path, source, sys_path) @@ -440,12 +440,15 @@ def load(source): with open(path, 'rb') as f: source = f.read() else: - return compiled.load_module(path) + return compiled.load_module(evaluator, path) p = path p = fast.FastParser(evaluator.grammar, common.source_to_unicode(source), p) cache.save_parser(path, p) return p.module + if sys_path is None: + sys_path = evaluator.sys_path + cached = cache.load_parser(path) module = load(source) if cached is None else cached.module module = evaluator.wrap(module) diff --git a/jedi/evaluate/sys_path.py b/jedi/evaluate/sys_path.py index 7cfbd57b0..382d29ff3 100644 --- a/jedi/evaluate/sys_path.py +++ b/jedi/evaluate/sys_path.py @@ -1,6 +1,9 @@ import glob import os import sys +from subprocess import check_output +from ast import literal_eval +from site import addsitedir from jedi._compatibility import exec_function, unicode from jedi.parser import tree @@ -11,24 +14,66 @@ from jedi import cache -def get_sys_path(): - def check_virtual_env(sys_path): - """ Add virtualenv's site-packages to the `sys.path`.""" - venv = os.getenv('VIRTUAL_ENV') - if not venv: - return - venv = os.path.abspath(venv) - p = _get_venv_sitepackages(venv) - if p not in sys_path: - sys_path.insert(0, p) - - # Add all egg-links from the virtualenv. +def get_venv_path(venv): + """Get sys.path for specified virtual environment.""" + try: + sys_path = _get_venv_path_online(venv) + except Exception as e: + debug.warning("Error when getting venv path: %s" % e) + sys_path = _get_venv_path_offline(venv) + with common.ignored(ValueError): + sys_path.remove('') + return _get_sys_path_with_egglinks(sys_path) + + +def _get_sys_path_with_egglinks(sys_path): + """Find all paths including those referenced by egg-links. + + Egg-link-referenced directories are inserted into path immediately after + the directory on which their links were found. Such directories are not + taken into consideration by normal import mechanism, but they are traversed + when doing pkg_resources.require. + """ + result = [] + for p in sys_path: + result.append(p) for egg_link in glob.glob(os.path.join(p, '*.egg-link')): with open(egg_link) as fd: - sys_path.insert(0, fd.readline().rstrip()) + for line in fd: + line = line.strip() + if line: + result.append(os.path.join(p, line)) + # pkg_resources package only interprets the first + # non-empty line in egg-link files. + break + return result + + +def _get_venv_path_offline(venv): + """Get sys.path for venv without starting up the interpreter.""" + venv = os.path.abspath(venv) + sitedir = _get_venv_sitepackages(venv) + sys.path, old_sys_path = [], sys.path + try: + addsitedir(sitedir) + return sys.path + finally: + sys.path = old_sys_path + + +def _get_venv_path_online(venv): + """Get sys.path for venv by running its python interpreter.""" + venv = os.path.abspath(os.path.expanduser(venv)) + for python_binary in ('python', 'python3', 'python.exe', + 'python3.exe'): + python_path = os.path.join(venv, 'bin', python_binary) + if os.path.isfile(python_path): + break + else: + raise RuntimeError("Cannot find python executable in venv: %s" % venv) + command = [python_path, '-c', 'import sys; print(sys.path)'] + return literal_eval(check_output(command)) - check_virtual_env(sys.path) - return [p for p in sys.path if p != ""] def _get_venv_sitepackages(venv): @@ -109,7 +154,6 @@ def _paths_from_list_modifications(module_path, trailer1, trailer2): name = trailer1.children[1].value if name not in ['insert', 'append']: return [] - arg = trailer2.children[1] if name == 'insert' and len(arg.children) in (3, 4): # Possible trailing comma. arg = arg.children[2] @@ -117,6 +161,9 @@ def _paths_from_list_modifications(module_path, trailer1, trailer2): def _check_module(evaluator, module): + """ + Detect sys.path modifications within module. + """ def get_sys_path_powers(names): for name in names: power = name.parent.parent @@ -128,10 +175,12 @@ def get_sys_path_powers(names): if isinstance(n, tree.Name) and n.value == 'path': yield name, power - sys_path = list(get_sys_path()) # copy + sys_path = list(evaluator.sys_path) # copy try: possible_names = module.used_names['path'] except KeyError: + # module.used_names is MergedNamesDict whose getitem never throws + # keyerror, this is superfluous. pass else: for name, power in get_sys_path_powers(possible_names): @@ -148,7 +197,7 @@ def sys_path_with_modifications(evaluator, module): if module.path is None: # Support for modules without a path is bad, therefore return the # normal path. - return list(get_sys_path()) + return list(evaluator.sys_path) curdir = os.path.abspath(os.curdir) with common.ignored(OSError): diff --git a/test/test_regression.py b/test/test_regression.py index fa81c8af4..94132acd9 100644 --- a/test/test_regression.py +++ b/test/test_regression.py @@ -66,8 +66,9 @@ def test_add_dynamic_mods(self): src1 = "def r(a): return a" # Other fictional modules in another place in the fs. src2 = 'from .. import setup; setup.r(1)' - imports.load_module(os.path.abspath(fname), src2) - result = Script(src1, path='../setup.py').goto_definitions() + script = Script(src1, path='../setup.py') + imports.load_module(script._evaluator, os.path.abspath(fname), src2) + result = script.goto_definitions() assert len(result) == 1 assert result[0].description == 'class int' From 96b87b5f94421704c484e7317db4812b9a8f290e Mon Sep 17 00:00:00 2001 From: immerrr Date: Wed, 8 Apr 2015 19:46:42 +0200 Subject: [PATCH 2/2] _get_venv_path_online: prevent PYTHON* vars leaking into subprocess (-E) --- jedi/evaluate/sys_path.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jedi/evaluate/sys_path.py b/jedi/evaluate/sys_path.py index 382d29ff3..38406c814 100644 --- a/jedi/evaluate/sys_path.py +++ b/jedi/evaluate/sys_path.py @@ -71,7 +71,7 @@ def _get_venv_path_online(venv): break else: raise RuntimeError("Cannot find python executable in venv: %s" % venv) - command = [python_path, '-c', 'import sys; print(sys.path)'] + command = [python_path, '-E', '-c', 'import sys; print(sys.path)'] return literal_eval(check_output(command))