forked from VimPlug/jedi
Fix the first part of sys path checks.
This commit is contained in:
@@ -35,15 +35,14 @@ def _execute_code(module_path, code):
|
|||||||
exec_function(c % code, variables)
|
exec_function(c % code, variables)
|
||||||
except Exception:
|
except Exception:
|
||||||
debug.warning('sys.path manipulation detected, but failed to evaluate.')
|
debug.warning('sys.path manipulation detected, but failed to evaluate.')
|
||||||
return None
|
else:
|
||||||
try:
|
try:
|
||||||
res = variables['result']
|
res = variables['result']
|
||||||
if isinstance(res, str):
|
if isinstance(res, str):
|
||||||
return os.path.abspath(res)
|
return [os.path.abspath(res)]
|
||||||
else:
|
except KeyError:
|
||||||
return None
|
pass
|
||||||
except KeyError:
|
return []
|
||||||
return None
|
|
||||||
|
|
||||||
|
|
||||||
def _paths_from_assignment(evaluator, statement):
|
def _paths_from_assignment(evaluator, statement):
|
||||||
@@ -73,52 +72,48 @@ def _paths_from_assignment(evaluator, statement):
|
|||||||
yield val.obj
|
yield val.obj
|
||||||
|
|
||||||
|
|
||||||
def _paths_from_insert(module_path, exe):
|
def _paths_from_list_modifications(module_path, trailer1, trailer2):
|
||||||
""" extract the inserted module path from an "sys.path.insert" statement
|
|
||||||
"""
|
|
||||||
exe_type, exe.type = exe.type, pr.Array.NOARRAY
|
|
||||||
try:
|
|
||||||
exe_pop = exe.values.pop(0)
|
|
||||||
res = _execute_code(module_path, exe.get_code())
|
|
||||||
finally:
|
|
||||||
exe.type = exe_type
|
|
||||||
exe.values.insert(0, exe_pop)
|
|
||||||
return res
|
|
||||||
|
|
||||||
|
|
||||||
def _paths_from_call_expression(module_path, call):
|
|
||||||
""" extract the path from either "sys.path.append" or "sys.path.insert" """
|
""" extract the path from either "sys.path.append" or "sys.path.insert" """
|
||||||
names = call.names()
|
# Guarantee that both are trailers, the first one a name and the second one
|
||||||
if names[:3] != ['sys', 'path', 'append'] and names[:3] != ['sys', 'path', 'insert']:
|
# a function execution with at least one param.
|
||||||
return []
|
if not (pr.is_node(trailer1, 'trailer') and trailer1.children[0] == '.'
|
||||||
if not call.next.next.next_is_execution():
|
and pr.is_node(trailer2, 'trailer') and trailer2.children[0] == '('
|
||||||
|
and len(trailer2.children) == 3):
|
||||||
return []
|
return []
|
||||||
|
|
||||||
cmd = names[2]
|
name = trailer1.children[1].value
|
||||||
exe = call.next.next.next
|
if name not in ['insert', 'append']:
|
||||||
path = None
|
return []
|
||||||
if cmd == 'insert' and len(exe) == 2:
|
|
||||||
path = _paths_from_insert(module_path, exe)
|
arg = trailer2.children[1]
|
||||||
elif cmd == 'append' and len(exe) == 1:
|
if name == 'insert' and len(arg.children) in (3, 4): # Possible trailing comma.
|
||||||
path = _execute_code(module_path, exe.get_code())
|
arg = arg.children[2]
|
||||||
return path and [path] or []
|
return _execute_code(module_path, arg.get_code())
|
||||||
|
|
||||||
|
|
||||||
def _check_module(evaluator, module):
|
def _check_module(evaluator, module):
|
||||||
try:
|
def get_sys_path_powers(names):
|
||||||
possible_stmts = module.used_names['path']
|
for power in [p.parent.parent for p in names]:
|
||||||
except KeyError:
|
if pr.is_node(power, 'power'):
|
||||||
return get_sys_path()
|
c = power.children
|
||||||
|
if isinstance(c[0], pr.Name) and c[0].value == 'sys' \
|
||||||
|
and pr.is_node(c[1], 'trailer'):
|
||||||
|
n = c[1].children[1]
|
||||||
|
if isinstance(n, pr.Name) and n.value == 'path':
|
||||||
|
yield power
|
||||||
|
|
||||||
sys_path = list(get_sys_path()) # copy
|
sys_path = list(get_sys_path()) # copy
|
||||||
statements = (p for p in possible_stmts if isinstance(p, pr.ExprStmt))
|
try:
|
||||||
for stmt in statements:
|
possible_names = module.used_names['path']
|
||||||
expressions = stmt.expression_list()
|
except KeyError:
|
||||||
if len(expressions) == 1 and isinstance(expressions[0], pr.Call):
|
pass
|
||||||
sys_path.extend(
|
else:
|
||||||
_paths_from_call_expression(module.path, expressions[0]) or [])
|
for power in get_sys_path_powers(possible_names):
|
||||||
elif hasattr(stmt, 'assignment_details') \
|
if len(power.children) >= 4:
|
||||||
and len(stmt.assignment_details) == 1:
|
sys_path.extend(_paths_from_list_modifications(module.path, *power.children[2:4]))
|
||||||
sys_path.extend(_paths_from_assignment(evaluator, stmt))
|
elif hasattr(power, 'assignment_details') \
|
||||||
|
and len(stmt.assignment_details) == 1:
|
||||||
|
sys_path.extend(_paths_from_assignment(evaluator, stmt))
|
||||||
return sys_path
|
return sys_path
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user