diff options
| author | michele.simionato <devnull@localhost> | 2009-04-21 09:53:51 +0000 |
|---|---|---|
| committer | michele.simionato <devnull@localhost> | 2009-04-21 09:53:51 +0000 |
| commit | 4fab653aa6f1a2843daa9fa119673583c317ba7b (patch) | |
| tree | 4b367c9673b0b2c96bf95690ae48f092af40bd31 | |
| parent | ed690fd6af1357d514c298bc4a3f84ef6f153486 (diff) | |
| download | python-decorator-git-4fab653aa6f1a2843daa9fa119673583c317ba7b.tar.gz | |
Improved the TailRecursive decorator by using George Sakkis's version
| -rw-r--r-- | documentation.py | 35 |
1 files changed, 17 insertions, 18 deletions
diff --git a/documentation.py b/documentation.py index 97552da..e5cc32c 100644 --- a/documentation.py +++ b/documentation.py @@ -983,37 +983,36 @@ class Action(object): @Restricted(Admin) def delete(self): pass - + class TailRecursive(object): """ tail_recursive decorator based on Kay Schluehr's recipe http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496691 + with improvements by me and George Sakkis. """ - CONTINUE = object() # sentinel def __init__(self, func): self.func = func self.firstcall = True + self.CONTINUE = object() # sentinel def __call__(self, *args, **kwd): - try: - if self.firstcall: # start looping - self.firstcall = False - while True: - result = self.func(*args, **kwd) - if result is self.CONTINUE: # update arguments + CONTINUE = self.CONTINUE + if self.firstcall: + func = self.func + self.firstcall = False + try: + while True: + result = func(*args, **kwd) + if result is CONTINUE: # update arguments args, kwd = self.argskwd else: # last call - break - else: # return the arguments of the tail call - self.argskwd = args, kwd - return self.CONTINUE - except: # reset and re-raise - self.firstcall = True - raise - else: # reset and exit - self.firstcall = True - return result + return result + finally: + self.firstcall = True + else: # return the arguments of the tail call + self.argskwd = args, kwd + return CONTINUE def tail_recursive(func): return decorator_apply(TailRecursive, func) |
