Debian Codegraph @

rpython/tool/flattenrec.py

raw symbols SCIP
1 """
2 A general way to flatten deeply recursive algorithms by delaying some
3 parts until later.
4 """
5
6 try:
7 from thread import _local as TlsClass
8 except ImportError:
9 class TlsClass(object):
10 pass
11
12
13 class FlattenRecursion(TlsClass):
14
15 def __init__(self):
16 self.later = None
17
18 def __call__(self, func, *args, **kwds):
19 """Call func(*args, **kwds), either now, or, if we're recursing,
20 then the call will be done later by the first level.
21 """
22 if self.later is not None:
23 self.later.append((func, args, kwds))
24 else:
25 self.later = lst = []
26 try:
27 func(*args, **kwds)
28 for func, args, kwds in lst:
29 func(*args, **kwds)
30 finally:
31 self.later = None