forked from VimPlug/jedi
Rewrote sys_path._paths_from_assignment.
This commit is contained in:
@@ -46,25 +46,31 @@ def _execute_code(module_path, code):
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
def _paths_from_assignment(statement):
|
def _paths_from_assignment(evaluator, statement):
|
||||||
"""
|
"""
|
||||||
extracts the assigned strings from an assignment that looks as follows::
|
Extracts the assigned strings from an assignment that looks as follows::
|
||||||
|
|
||||||
>>> sys.path[0:0] = ['module/path', 'another/module/path']
|
>>> sys.path[0:0] = ['module/path', 'another/module/path']
|
||||||
"""
|
|
||||||
|
|
||||||
names = statement.get_defined_names()
|
This function is in general pretty tolerant (and therefore 'buggy').
|
||||||
if len(names) != 1:
|
However, it's not a big issue usually to add more paths to Jedi's sys_path,
|
||||||
return []
|
because it will only affect Jedi in very random situations and by adding
|
||||||
if [unicode(x) for x in names[0].names] != ['sys', 'path']:
|
more paths than necessary, it usually benefits the general user.
|
||||||
return []
|
"""
|
||||||
expressions = statement.expression_list()
|
for exp_list, operator in statement.assignment_details:
|
||||||
if len(expressions) != 1 or not isinstance(expressions[0], pr.Array):
|
if len(exp_list) != 1 or not isinstance(exp_list[0], pr.Call):
|
||||||
return
|
continue
|
||||||
stmts = (s for s in expressions[0].values if isinstance(s, pr.Statement))
|
if unicode(exp_list[0].name) != 'sys.path':
|
||||||
expression_lists = (s.expression_list() for s in stmts)
|
continue
|
||||||
return [e.value for exprs in expression_lists for e in exprs
|
# TODO at this point we ignore all ways what could be assigned to
|
||||||
if isinstance(e, pr.Literal) and e.value]
|
# sys.path or an execution of it. Here we could do way more
|
||||||
|
# complicated checks.
|
||||||
|
|
||||||
|
from jedi.evaluate.iterable import get_iterator_types
|
||||||
|
from jedi.evaluate.precedence import _is_string
|
||||||
|
for val in get_iterator_types(evaluator.eval_statement(statement)):
|
||||||
|
if _is_string(val):
|
||||||
|
yield val.obj
|
||||||
|
|
||||||
|
|
||||||
def _paths_from_insert(module_path, exe):
|
def _paths_from_insert(module_path, exe):
|
||||||
@@ -100,7 +106,7 @@ def _paths_from_call_expression(module_path, call):
|
|||||||
return path and [path] or []
|
return path and [path] or []
|
||||||
|
|
||||||
|
|
||||||
def _check_module(module):
|
def _check_module(evaluator, module):
|
||||||
try:
|
try:
|
||||||
possible_stmts = module.used_names['path']
|
possible_stmts = module.used_names['path']
|
||||||
except KeyError:
|
except KeyError:
|
||||||
@@ -114,7 +120,7 @@ def _check_module(module):
|
|||||||
_paths_from_call_expression(module.path, expressions[0]) or [])
|
_paths_from_call_expression(module.path, expressions[0]) or [])
|
||||||
elif hasattr(stmt, 'assignment_details') \
|
elif hasattr(stmt, 'assignment_details') \
|
||||||
and len(stmt.assignment_details) == 1:
|
and len(stmt.assignment_details) == 1:
|
||||||
sys_path.extend(_paths_from_assignment(stmt) or [])
|
sys_path.extend(_paths_from_assignment(evaluator, stmt))
|
||||||
return sys_path
|
return sys_path
|
||||||
|
|
||||||
|
|
||||||
@@ -129,7 +135,7 @@ def sys_path_with_modifications(evaluator, module):
|
|||||||
with common.ignored(OSError):
|
with common.ignored(OSError):
|
||||||
os.chdir(os.path.dirname(module.path))
|
os.chdir(os.path.dirname(module.path))
|
||||||
|
|
||||||
result = _check_module(module)
|
result = _check_module(evaluator, module)
|
||||||
result += _detect_django_path(module.path)
|
result += _detect_django_path(module.path)
|
||||||
# buildout scripts often contain the same sys.path modifications
|
# buildout scripts often contain the same sys.path modifications
|
||||||
# the set here is used to avoid duplicate sys.path entries
|
# the set here is used to avoid duplicate sys.path entries
|
||||||
|
|||||||
@@ -202,7 +202,7 @@ class Parser(object):
|
|||||||
if annotation:
|
if annotation:
|
||||||
param.add_annotation(annotation)
|
param.add_annotation(annotation)
|
||||||
|
|
||||||
# function params without vars are usually syntax errors.
|
# Function params without vars are usually syntax errors.
|
||||||
# expressions are valid in superclass declarations.
|
# expressions are valid in superclass declarations.
|
||||||
if param is not None and param.get_defined_names():
|
if param is not None and param.get_defined_names():
|
||||||
param.position_nr = pos
|
param.position_nr = pos
|
||||||
|
|||||||
@@ -950,14 +950,11 @@ class Statement(Simple, DocstringMixin):
|
|||||||
c = call
|
c = call
|
||||||
# Check if there's an execution in it, if so this is
|
# Check if there's an execution in it, if so this is
|
||||||
# not a set_var.
|
# not a set_var.
|
||||||
is_execution = False
|
|
||||||
while c:
|
while c:
|
||||||
# TODO use StatementElement.next_is_execution
|
if isinstance(c.next, Array):
|
||||||
if Array.is_type(c.next, Array.TUPLE):
|
break
|
||||||
is_execution = True
|
|
||||||
c = c.next
|
c = c.next
|
||||||
if is_execution:
|
else:
|
||||||
continue
|
|
||||||
self._set_vars.append(call.name)
|
self._set_vars.append(call.name)
|
||||||
|
|
||||||
self._set_vars = []
|
self._set_vars = []
|
||||||
|
|||||||
@@ -1,9 +1,11 @@
|
|||||||
import os
|
import os
|
||||||
|
from textwrap import dedent
|
||||||
|
|
||||||
from jedi._compatibility import u
|
from jedi._compatibility import u
|
||||||
from jedi.evaluate.sys_path import (_get_parent_dir_with_file,
|
from jedi.evaluate.sys_path import (_get_parent_dir_with_file,
|
||||||
_get_buildout_scripts,
|
_get_buildout_scripts,
|
||||||
_check_module)
|
_check_module)
|
||||||
|
from jedi.evaluate import Evaluator
|
||||||
from jedi.parser import Parser
|
from jedi.parser import Parser
|
||||||
|
|
||||||
from ..helpers import cwd_at
|
from ..helpers import cwd_at
|
||||||
@@ -27,30 +29,30 @@ def test_buildout_detection():
|
|||||||
|
|
||||||
|
|
||||||
def test_append_on_non_sys_path():
|
def test_append_on_non_sys_path():
|
||||||
SRC = u("""
|
SRC = dedent(u("""
|
||||||
class Dummy(object):
|
class Dummy(object):
|
||||||
path = []
|
path = []
|
||||||
|
|
||||||
d = Dummy()
|
d = Dummy()
|
||||||
d.path.append('foo')""")
|
d.path.append('foo')"""))
|
||||||
p = Parser(SRC)
|
p = Parser(SRC)
|
||||||
paths = _check_module(p.module)
|
paths = _check_module(Evaluator(), p.module)
|
||||||
assert len(paths) > 0
|
assert len(paths) > 0
|
||||||
assert 'foo' not in paths
|
assert 'foo' not in paths
|
||||||
|
|
||||||
|
|
||||||
def test_path_from_invalid_sys_path_assignment():
|
def test_path_from_invalid_sys_path_assignment():
|
||||||
SRC = u("""
|
SRC = dedent(u("""
|
||||||
import sys
|
import sys
|
||||||
sys.path = 'invalid'""")
|
sys.path = 'invalid'"""))
|
||||||
p = Parser(SRC)
|
p = Parser(SRC)
|
||||||
paths = _check_module(p.module)
|
paths = _check_module(Evaluator(), p.module)
|
||||||
assert len(paths) > 0
|
assert len(paths) > 0
|
||||||
assert 'invalid' not in paths
|
assert 'invalid' not in paths
|
||||||
|
|
||||||
|
|
||||||
def test_path_from_sys_path_assignment():
|
def test_path_from_sys_path_assignment():
|
||||||
SRC = u("""
|
SRC = dedent(u("""
|
||||||
#!/usr/bin/python
|
#!/usr/bin/python
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
@@ -64,8 +66,8 @@ path[0:0] = [1]
|
|||||||
import important_package
|
import important_package
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
sys.exit(important_package.main())""")
|
sys.exit(important_package.main())"""))
|
||||||
p = Parser(SRC)
|
p = Parser(SRC)
|
||||||
paths = _check_module(p.module)
|
paths = _check_module(Evaluator(), p.module)
|
||||||
assert 1 not in paths
|
assert 1 not in paths
|
||||||
assert '/home/test/.buildout/eggs/important_package.egg' in paths
|
assert '/home/test/.buildout/eggs/important_package.egg' in paths
|
||||||
|
|||||||
Reference in New Issue
Block a user