From: Mike C. F. <mcf...@us...> - 2005-09-17 05:33:00
|
Update of /cvsroot/pydispatcher/dispatch In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv15076 Modified Files: saferef.py setup.py Added Files: robust.py Log Message: Add "robust" module with single function sendRobust, which catches errors during callbacks and returns the error instances instead of propagating the error Patch bug in SafeRef deletion where traceback module has already been deleted by interpreter shutdown Make SafeRef pre-cache method name to allow for repr after cleanup of the method Index: setup.py =================================================================== RCS file: /cvsroot/pydispatcher/dispatch/setup.py,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** setup.py 26 Nov 2004 06:37:33 -0000 1.6 --- setup.py 17 Sep 2005 05:32:53 -0000 1.7 *************** *** 86,90 **** setup ( name = "PyDispatcher", ! version = "1.0.2", description= "Multi-producer-multi-consumer signal dispatching mechanism", author = "Patrick K. O'Brien", --- 86,90 ---- setup ( name = "PyDispatcher", ! version = "1.0.3", description= "Multi-producer-multi-consumer signal dispatching mechanism", author = "Patrick K. O'Brien", --- NEW FILE: robust.py --- """Module implementing error-catching version of send (sendRobust)""" from dispatch.dispatcher import Any, Anonymous, liveReceivers, getAllReceivers from dispatch.robustapply import robustApply def sendRobust( signal=Any, sender=Anonymous, *arguments, **named ): """Send signal from sender to all connected receivers catching errors signal -- (hashable) signal value, see connect for details sender -- the sender of the signal if Any, only receivers registered for Any will receive the message. if Anonymous, only receivers registered to receive messages from Anonymous or Any will receive the message Otherwise can be any python object (normally one registered with a connect if you actually want something to occur). arguments -- positional arguments which will be passed to *all* receivers. Note that this may raise TypeErrors if the receivers do not allow the particular arguments. Note also that arguments are applied before named arguments, so they should be used with care. named -- named arguments which will be filtered according to the parameters of the receivers to only provide those acceptable to the receiver. Return a list of tuple pairs [(receiver, response), ... ] if any receiver raises an error (specifically any subclass of Exception), the error instance is returned as the result for that receiver. """ # Call each receiver with whatever arguments it can accept. # Return a list of tuple pairs [(receiver, response), ... ]. responses = [] for receiver in liveReceivers(getAllReceivers(sender, signal)): try: response = robustApply( receiver, signal=signal, sender=sender, *arguments, **named ) except Exception, err: responses.append((receiver, err)) else: responses.append((receiver, response)) return responses Index: saferef.py =================================================================== RCS file: /cvsroot/pydispatcher/dispatch/saferef.py,v retrieving revision 1.1.1.1 retrieving revision 1.2 diff -C2 -d -r1.1.1.1 -r1.2 *** saferef.py 1 Jul 2003 03:52:28 -0000 1.1.1.1 --- saferef.py 17 Sep 2005 05:32:53 -0000 1.2 *************** *** 59,63 **** to the same (object, function) pair produce the same BoundMethodWeakref instance. ! """ _allInstances = weakref.WeakValueDictionary() --- 59,63 ---- to the same (object, function) pair produce the same BoundMethodWeakref instance. ! """ _allInstances = weakref.WeakValueDictionary() *************** *** 110,119 **** if callable( function ): function( self ) ! except Exception: ! traceback.print_exc() self.deletionMethods = [onDelete] self.key = self.calculateKey( target ) self.weakSelf = weakref.ref(target.im_self, remove) self.weakFunc = weakref.ref(target.im_func, remove) def calculateKey( cls, target ): """Calculate the reference key for this reference --- 110,126 ---- if callable( function ): function( self ) ! except Exception, e: ! try: ! traceback.print_exc() ! except AttributeError, err: ! print '''Exception during saferef %s cleanup function %s: %s'''%( ! self, function, e ! ) self.deletionMethods = [onDelete] self.key = self.calculateKey( target ) self.weakSelf = weakref.ref(target.im_self, remove) self.weakFunc = weakref.ref(target.im_func, remove) + self.selfName = str(target.im_self) + self.funcName = str(target.im_func.__name__) def calculateKey( cls, target ): """Calculate the reference key for this reference *************** *** 128,133 **** return """%s( %s.%s )"""%( self.__class__.__name__, ! self.weakSelf(), ! self.weakFunc().__name__, ) __repr__ = __str__ --- 135,140 ---- return """%s( %s.%s )"""%( self.__class__.__name__, ! self.selfName, ! self.funcName, ) __repr__ = __str__ |