Skip to content

Commit

Permalink
Merge 88fa35d into 8775d90
Browse files Browse the repository at this point in the history
  • Loading branch information
mfussenegger committed Feb 10, 2015
2 parents 8775d90 + 88fa35d commit 208ef9f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
30 changes: 19 additions & 11 deletions jedi/evaluate/sys_path.py
Expand Up @@ -9,6 +9,10 @@
from jedi import common


buildout_scripts = set()
buildout_script_paths = set()


def get_sys_path():
def check_virtual_env(sys_path):
""" Add virtualenv's site-packages to the `sys.path`."""
Expand Down Expand Up @@ -144,24 +148,28 @@ def sys_path_with_modifications(evaluator, module):

result = _check_module(evaluator, module)
result += _detect_django_path(module.path)
# buildout scripts often contain the same sys.path modifications
# the set here is used to avoid duplicate sys.path entries
buildout_paths = set()
for module_path in _get_buildout_scripts(module.path):
for buildout_script in _get_buildout_scripts(module.path):
if buildout_script in buildout_scripts:
continue
buildout_scripts.add(buildout_script)
try:
with open(module_path, 'rb') as f:
with open(buildout_script, 'rb') as f:
source = f.read()
except IOError:
pass
else:
p = Parser(evaluator.grammar, common.source_to_unicode(source), module_path)
for path in _check_module(p.module):
if path not in buildout_paths:
buildout_paths.add(path)
result.append(path)
p = Parser(evaluator.grammar,
common.source_to_unicode(source),
buildout_script)
try:
for path in _check_module(evaluator, p.module):
if path not in buildout_script_paths:
buildout_script_paths.add(path)
except Exception:
debug.dbg('Could not check module: "%s"', buildout_script)
# cleanup, back to old directory
os.chdir(curdir)
return list(result)
return list(result) + list(buildout_script_paths)


def _traverse_parents(path):
Expand Down
13 changes: 13 additions & 0 deletions test/test_evaluate/test_buildout_detection.py
Expand Up @@ -4,6 +4,7 @@
from jedi._compatibility import u
from jedi.evaluate.sys_path import (_get_parent_dir_with_file,
_get_buildout_scripts,
sys_path_with_modifications,
_check_module)
from jedi.evaluate import Evaluator
from jedi.parser import Parser, load_grammar
Expand Down Expand Up @@ -53,6 +54,18 @@ def test_path_from_invalid_sys_path_assignment():
assert 'invalid' not in paths


@cwd_at('test/test_evaluate/buildout_project/src/proj_name/')
def test_sys_path_with_modifications():
SRC = dedent(u("""
import os
"""))
grammar = load_grammar()
p = Parser(grammar, SRC)
p.module.path = os.path.abspath(os.path.join(os.curdir, 'module_name.py'))
paths = sys_path_with_modifications(Evaluator(grammar), p.module)
assert '/tmp/.buildout/eggs/important_package.egg' in paths


def test_path_from_sys_path_assignment():
SRC = dedent(u("""
#!/usr/bin/python
Expand Down

0 comments on commit 208ef9f

Please sign in to comment.