recursions are now being catched

This commit is contained in:
David Halter
2012-07-09 12:05:07 +02:00
parent e78ba60a47
commit 37f2b8ff56
4 changed files with 70 additions and 5 deletions

View File

@@ -30,6 +30,7 @@ import parsing
import modules
import debug
import builtin
import helpers
memoize_caches = []
@@ -70,6 +71,8 @@ def clear_caches():
for m in memoize_caches:
m.clear()
#follow_statement.reset()
def memoize_default(default=None):
"""
@@ -209,7 +212,8 @@ class Instance(Executable):
def __getattr__(self, name):
if name not in ['line_nr', 'indent', 'name', 'get_imports']:
raise AttributeError("Don't touch this (%s)!" % name)
raise AttributeError("Instance %s: Don't touch this (%s)!"
% (self, name))
return getattr(self.base, name)
def __repr__(self):
@@ -976,7 +980,9 @@ def assign_tuples(tup, results, seek_name):
if hasattr(r, "get_exact_index_types"):
types += r.get_exact_index_types(index)
else:
debug.warning("assign tuples: invalid tuple lookup")
debug.warning("invalid tuple lookup %s of result %s in %s"
% (tup, results, seek_name))
return types
result = []
@@ -1002,6 +1008,7 @@ def assign_tuples(tup, results, seek_name):
return result
@helpers.RecursionDecorator
@memoize_default(default=[])
def follow_statement(stmt, scope=None, seek_name=None):
"""

45
helpers.py Normal file
View File

@@ -0,0 +1,45 @@
import parsing
class RecursionDecorator(object):
""" A decorator to detect recursions in statements """
def __init__(self, func):
self.func = func
self.reset()
self.current = None
def __call__(self, stmt, *args, **kwargs):
# don't check param instances, they are not causing recursions
if isinstance(stmt, parsing.Param):
return self.func(stmt, *args, **kwargs)
r = RecursionNode(stmt, self.current)
if self.check_recursion(r):
return []
parent, self.current = self.current, r
result = self.func(stmt, *args, **kwargs)
self.current = parent
return result
def check_recursion(self, new):
test = self.current
while True:
if new == test:
return True
if not test:
return False
test = test.parent
def reset(self):
self.top = None
self.current = None
class RecursionNode(object):
def __init__(self, stmt, parent):
self.script = stmt.get_parent_until()
self.position = (stmt.line_nr, stmt.indent)
self.parent = parent
def __eq__(self, other):
if not other:
return None
return self.script == other.script and self.position == other.position

View File

@@ -54,7 +54,7 @@ variable_rename(list())().
variable_rename(1)().
# -----------------
# recursion (should ignore)
# recursions (should ignore)
# -----------------
def recursion(a, b):
if a:
@@ -62,9 +62,22 @@ def recursion(a, b):
else:
return recursion(a+".", b+1)
##? int() float()
#? int() float()
recursion("a", 1.0)
def other(a):
return recursion2(a)
def recursion2(a):
if a:
return other(a)
else:
return recursion2("")
return a
#? int() str() str()
recursion2(1)
# -----------------
# ordering
# -----------------

View File

@@ -17,5 +17,5 @@ scopes, path, dot, like = \
#? set()
el = scopes.
##? str() <--- recursion
#? str() <--- recursion
el = evaluate.get_names_for_scope()[0].