Here, FuglyHackThatWillWorkForYourExampleButICantPromiseAnythingElse:
orig_spam = spam.func_closure[0].cell_contents
Change For functions / methods decorated more than once and more complex decorators, you can try using the following code. He relies on the fact that the decorated __name__d functions are different from the original.
def search_for_orig(decorated, orig_name): for obj in (c.cell_contents for c in decorated.__closure__): if hasattr(obj, "__name__") and obj.__name__ == orig_name: return obj if hasattr(obj, "__closure__") and obj.__closure__: found = search_for_orig(obj, orig_name) if found: return found return None >>> search_for_orig(spam, "spam") <function spam at 0x027ACD70>
This is not stupid proof. This will fail if the name of the function returned from the decorator is the same as the decorated one. The hasattr () check order is also heuristic; there are trim chains that return incorrect results anyway.
Wojciech Bederski Jul 22 '09 at 15:45 2009-07-22 15:45
source share