First array addition working.

This commit is contained in:
Dave Halter
2014-12-08 18:25:38 +01:00
parent 4c3584ed3c
commit 1c44336d60

View File

@@ -441,13 +441,15 @@ def _check_array_additions(evaluator, compare_array, module, is_list):
if not settings.dynamic_array_additions or isinstance(module, compiled.CompiledObject): if not settings.dynamic_array_additions or isinstance(module, compiled.CompiledObject):
return [] return []
def check_calls(calls, add_name): def check_power(power):
""" """
Calls are processed here. The part before the call is searched and The power node is compared with the original Array.
compared with the original Array.
""" """
# TODO remove unused.
result = [] result = []
for c in calls: x = evaluator.eval_element(power)
#print(power, compare_array in x, x, compare_array)
if 0:
call_path = list(c.generate_call_path()) call_path = list(c.generate_call_path())
call_path_simple = [unicode(n) if isinstance(n, pr.Name) else n call_path_simple = [unicode(n) if isinstance(n, pr.Name) else n
for n in call_path] for n in call_path]
@@ -465,30 +467,50 @@ def _check_array_additions(evaluator, compare_array, module, is_list):
if not compare_array in found: if not compare_array in found:
continue continue
def get_additions(arglist, add_name):
params = list(param.Arguments(evaluator, arglist).unpack())
result = []
if add_name in ['insert']:
params = params[1:]
if add_name in ['append', 'add', 'insert']:
for key, nodes in params:
result += unite(evaluator.eval_element(node) for node in nodes)
elif add_name in ['extend', 'update']:
for key, nodes in params:
iterators = unite(evaluator.eval_element(node) for node in nodes)
result += get_iterator_types(iterators)
return result
# TODO REMOVE
"""
params = call_path[separate_index + 1] params = call_path[separate_index + 1]
if not params.values: if not params.values:
continue # no params: just ignore it #continue # no params: just ignore it
pass
if add_name in ['append', 'add']: if add_name in ['append', 'add']:
for param in params: for p in params:
result += evaluator.eval_statement(param) result += evaluator.eval_statement(p)
elif add_name in ['insert']: elif add_name in ['insert']:
try: try:
second_param = params[1] second_param = params[1]
except IndexError: except IndexError:
continue #continue
pass
else: else:
result += evaluator.eval_statement(second_param) result += evaluator.eval_statement(second_param)
elif add_name in ['extend', 'update']: elif add_name in ['extend', 'update']:
for param in params: for p in params:
iterators = evaluator.eval_statement(param) iterators = evaluator.eval_statement(p)
result += get_iterator_types(iterators) result += get_iterator_types(iterators)
return result return result
"""
from jedi.evaluate import representation as er from jedi.evaluate import representation as er, param
def get_execution_parent(element, *stop_classes): def get_execution_parent(element, *stop_classes):
""" Used to get an Instance/FunctionExecution parent """ """ Used to get an Instance/FunctionExecution parent """
if isinstance(element, Array): if isinstance(element, Array):
# TODO remove!
stmt = element._array.parent stmt = element._array.parent
else: else:
# is an Instance with an ArrayInstance inside # is an Instance with an ArrayInstance inside
@@ -497,25 +519,25 @@ def _check_array_additions(evaluator, compare_array, module, is_list):
stop_classes = list(stop_classes) + [er.Function] stop_classes = list(stop_classes) + [er.Function]
return stmt.get_parent_until(stop_classes) return stmt.get_parent_until(stop_classes)
temp_param_add = settings.dynamic_params_for_other_modules temp_param_add, settings.dynamic_params_for_other_modules = \
settings.dynamic_params_for_other_modules = False settings.dynamic_params_for_other_modules, False
search_names = ['append', 'extend', 'insert'] if is_list else \ search_names = ['append', 'extend', 'insert'] if is_list else ['add', 'update']
['add', 'update'] #comp_arr_parent = get_execution_parent(compare_array, er.FunctionExecution)
comp_arr_parent = get_execution_parent(compare_array, er.FunctionExecution)
possible_stmts = []
res = [] res = []
for n in search_names: for add_name in search_names:
try: try:
possible_stmts += module.used_names[n] possible_names = module.used_names[add_name]
except KeyError: except KeyError:
continue continue
for stmt in possible_stmts: else:
for name in possible_names:
# Check if the original scope is an execution. If it is, one # Check if the original scope is an execution. If it is, one
# can search for the same statement, that is in the module # can search for the same statement, that is in the module
# dict. Executions are somewhat special in jedi, since they # dict. Executions are somewhat special in jedi, since they
# literally copy the contents of a function. # literally copy the contents of a function.
"""
if isinstance(comp_arr_parent, er.FunctionExecution): if isinstance(comp_arr_parent, er.FunctionExecution):
stmt = comp_arr_parent. \ stmt = comp_arr_parent. \
get_statement_for_position(stmt.start_pos) get_statement_for_position(stmt.start_pos)
@@ -525,13 +547,30 @@ def _check_array_additions(evaluator, compare_array, module, is_list):
# but have this wrapper around them. # but have this wrapper around them.
if isinstance(comp_arr_parent, er.InstanceElement): if isinstance(comp_arr_parent, er.InstanceElement):
stmt = er.get_instance_el(comp_arr_parent.instance, stmt) stmt = er.get_instance_el(comp_arr_parent.instance, stmt)
"""
if evaluator.recursion_detector.push_stmt(stmt): trailer = name.parent
# check recursion power = trailer.parent
trailer_pos = power.children.index(trailer)
try:
execution_trailer = power.children[trailer_pos + 1]
except IndexError:
continue continue
else:
if execution_trailer.type != 'trailer' \
or execution_trailer.children[0] != '(' \
or execution_trailer.children[1] == ')':
continue
power = helpers.call_of_name(name, cut_own_trailer=True)
#if evaluator.recursion_detector.push_stmt(stmt):
# check recursion
# continue
if compare_array in evaluator.eval_element(power):
# The arrays match.
res += get_additions(execution_trailer.children[1], add_name)
res += check_calls(helpers.scan_statement_for_calls(stmt, n), n) #res += check_power(call)
evaluator.recursion_detector.pop_stmt() #evaluator.recursion_detector.pop_stmt()
# reset settings # reset settings
settings.dynamic_params_for_other_modules = temp_param_add settings.dynamic_params_for_other_modules = temp_param_add
return res return res