I have a module that I use as a decorator - to modify a function that I use recursively.
The module optimises a tail call to avoid recursion limit - see credits below.
If I copy paste the code on the console, first the module and then the decorated function, it works ok.
But if I import the module, I got error that module fails to import sys - despite I import it in the first line in my module.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "TailRecurseException.py", line 18, in func
f = sys._getframe()
NameError: global name 'sys' is not defined
>>>
I thought I am importing the module in a wrong way:
I tried with
from TailRecurseException import tail_call_optimized
and
from TailRecurseException import *
@tail_call_optimized
def recursive_activations( ...)
but same results.
What can I do wrong in the module to decorate another function, since it fails to import sys?
This is my submodule:
import sys
class TailRecurseException:
def __init__(self, args, kwargs):
self.args = args
self.kwargs = kwargs
def tail_call_optimized(g):
"""
This function decorates a function with tail call
optimization. It does this by throwing an exception
if it is it's own grandparent, and catching such
exceptions to fake the tail call optimization.
This function fails if the decorated
function recurses in a non-tail context.
"""
def func(*args, **kwargs):
f = sys._getframe()
if f.f_back and f.f_back.f_back \
and f.f_back.f_back.f_code == f.f_code:
raise TailRecurseException(args, kwargs)
else:
while 1:
try:
return g(*args, **kwargs)
except TailRecurseException, e:
args = e.args
kwargs = e.kwargs
func.__doc__ = g.__doc__
return func
Credits: https://mail.python.org/pipermail//python-list/2006-February/407243.html