[Twisted-Python] Possible patch to twisted.python.hook
The following patch makes post hook get the result of the hooked function. THis lets you do cool stuff like pre/post-conditions (like synchronization, they can be optional), but it is most likely rather slower than the current hook, which means synchronized classes will be slower. I have a working implementation of pre/post conditions based on this, if anyone's interested. *** hook.py Sun Nov 18 20:02:59 2001 --- hook.py-new Sun Nov 11 11:31:17 2001 *************** *** 48,55 **** --- 48,58 ---- none of the hooks raise an exception). Hooks will be executed in the order in which they are added. + Pre-functions should have the signature (self, args, kwargs). + Post-functions should have the signature (success, result, self, args, kwargs). """ + # System Imports import string *************** *** 99,114 **** hooked_func = """ import %(module)s def %(name)s(*args, **kw): klazz = %(module)s.%(klass)s for preMethod in klazz.%(preName)s: apply(preMethod, args, kw) try: ! return apply(klazz.%(originalName)s, args, kw) ! finally: ! for postMethod in klazz.%(postName)s: ! apply(postMethod, args, kw) """ _PRE = '__hook_pre_%s_%s_%s__' --- 102,128 ---- hooked_func = """ import %(module)s + import sys def %(name)s(*args, **kw): klazz = %(module)s.%(klass)s for preMethod in klazz.%(preName)s: apply(preMethod, args, kw) + try: ! result = apply(klazz.%(originalName)s, args, kw) ! success = 1 ! except: ! success = 0 ! result = sys.last_value ! ! for postMethod in klazz.%(postName)s: ! apply(postMethod, (success, result) + args, kw) ! ! if success: ! return result ! else: ! raise result """ _PRE = '__hook_pre_%s_%s_%s__'
participants (1)
-
Itamar Shtull-Trauring