Tried to get the recursion issues with if stmts working.

This commit is contained in:
Dave Halter
2015-11-16 11:30:30 +01:00
parent 4361ce0778
commit 03efbca586
4 changed files with 42 additions and 10 deletions

View File

@@ -189,19 +189,19 @@ class Evaluator(object):
elif isinstance(element, iterable.MergedNodes): elif isinstance(element, iterable.MergedNodes):
return set(iterable.unite(self.eval_element(e) for e in element)) return set(iterable.unite(self.eval_element(e) for e in element))
parent = element.get_parent_until((tree.IfStmt, tree.ForStmt, tree.IsScope)) if_stmt = element.get_parent_until((tree.IfStmt, tree.ForStmt, tree.IsScope))
predefined_if_name_dict = self.predefined_if_name_dict_dict.get(parent) predefined_if_name_dict = self.predefined_if_name_dict_dict.get(if_stmt)
if not predefined_if_name_dict and isinstance(parent, tree.IfStmt): if not predefined_if_name_dict and isinstance(if_stmt, tree.IfStmt):
if_stmt = parent.children[1] if_stmt_test = if_stmt.children[1]
name_dicts = [{}] name_dicts = [{}]
# If we already did a check, we don't want to do it again -> If # If we already did a check, we don't want to do it again -> If
# predefined_if_name_dict_dict is filled, we stop. # predefined_if_name_dict_dict is filled, we stop.
# We don't want to check the if stmt itself, it's just about # We don't want to check the if stmt itself, it's just about
# the content. # the content.
if element.start_pos > if_stmt.end_pos: if element.start_pos > if_stmt_test.end_pos:
# Now we need to check if the names in the if_stmt match the # Now we need to check if the names in the if_stmt match the
# names in the suite. # names in the suite.
if_names = helpers.get_names_of_node(if_stmt) if_names = helpers.get_names_of_node(if_stmt_test)
element_names = helpers.get_names_of_node(element) element_names = helpers.get_names_of_node(element)
str_element_names = [str(e) for e in element_names] str_element_names = [str(e) for e in element_names]
if any(str(i) in str_element_names for i in if_names): if any(str(i) in str_element_names for i in if_names):
@@ -234,11 +234,11 @@ class Evaluator(object):
if len(name_dicts) > 1: if len(name_dicts) > 1:
result = set() result = set()
for name_dict in name_dicts: for name_dict in name_dicts:
self.predefined_if_name_dict_dict[parent] = name_dict self.predefined_if_name_dict_dict[if_stmt] = name_dict
try: try:
result |= self._eval_element_not_cached(element) result |= self._eval_element_not_cached(element)
finally: finally:
del self.predefined_if_name_dict_dict[parent] del self.predefined_if_name_dict_dict[if_stmt]
return result return result
else: else:
return self._eval_element_cached(element) return self._eval_element_cached(element)

View File

@@ -985,9 +985,20 @@ class IfStmt(Flow):
yield self.children[i + 1] yield self.children[i + 1]
def node_in_which_check_node(self, node): def node_in_which_check_node(self, node):
"""
Returns the check node (see function above) that a node is contained
in. However if it the node is in the check node itself and not in the
suite return None.
"""
start_pos = node.start_pos
for check_node in reversed(list(self.check_nodes())): for check_node in reversed(list(self.check_nodes())):
if check_node.start_pos < node.start_pos: if check_node.start_pos < start_pos:
return check_node if start_pos < check_node.end_pos:
return None
# In this case the node is within the check_node itself,
# not in the suite
else:
return check_node
def node_after_else(self, node): def node_after_else(self, node):
""" """

View File

@@ -218,3 +218,23 @@ else:
a = '' a = ''
#? int() #? int()
a a
# -----------------
# Recursion issues
# -----------------
def possible_recursion_error(filename):
if filename == 'a':
return filename
# It seems like without the brackets there wouldn't be a RecursionError.
elif type(filename) == str:
return filename
if NOT_DEFINED:
s = str()
else:
s = str()
#? str()
possible_recursion_error(s)

View File

@@ -6,6 +6,7 @@ import pytest
from . import helpers from . import helpers
from . import run from . import run
from . import refactor from . import refactor
import jedi import jedi
from jedi.evaluate.analysis import Warning from jedi.evaluate.analysis import Warning