recursive decorator

Michele Simionato michele.simionato at gmail.com
Sat Sep 5 04:58:57 EDT 2009


> def enable_recur(f):
>     print f.func_code.co_names
>     if 'recur' not in f.func_code.co_names:
>         return f # do nothing on non-recursive functions
>     c = Code.from_code(f.func_code)
>     c.code[1:1] = [(LOAD_GLOBAL, f.__name__), (STORE_FAST, 'recur')]
>     for i, (opcode, value) in enumerate(c.code[2:]):
>         if opcode == LOAD_GLOBAL and value == 'recur':
>             c.code[i+2] = (LOAD_FAST, 'recur')
>     f.func_code = c.to_code()
>     return f
>
There is a useless line 'print f.func_code.co_names' here, a left over
of my tests, sorry.



More information about the Python-list mailing list