1
0
mirror of https://github.com/django/django.git synced 2025-07-05 18:29:11 +00:00

magic-removal: Upgraded django.dispatch to pydispatcher 1.0.3 (which hasn't been released yet; got it from latest CVS)

git-svn-id: http://code.djangoproject.com/svn/django/branches/magic-removal@2636 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Adrian Holovaty 2006-04-09 22:59:25 +00:00
parent c229739f2f
commit 25b712819d
7 changed files with 817 additions and 741 deletions

View File

@ -25,12 +25,19 @@ Internal attributes:
deletion, (considerably speeds up the cleanup process
vs. the original code.)
"""
from __future__ import generators
import types, weakref
from django.dispatch import saferef, robustapply, errors
__author__ = "Patrick K. O'Brien <pobrien@orbtech.com>"
__cvsid__ = "$Id: dispatcher.py,v 1.8 2004/11/26 06:37:33 mcfletch Exp $"
__version__ = "$Revision: 1.8 $"[11:-2]
__cvsid__ = "$Id: dispatcher.py,v 1.9 2005/09/17 04:55:57 mcfletch Exp $"
__version__ = "$Revision: 1.9 $"[11:-2]
try:
True
except NameError:
True = 1==1
False = 1==0
class _Parameter:
"""Used to represent default parameter values."""
@ -366,6 +373,9 @@ def sendExact( signal=Any, sender=Anonymous, *arguments, **named ):
def _removeReceiver(receiver):
"""Remove receiver from connections."""
if not sendersBack:
# During module cleanup the mapping will be replaced with None
return False
backKey = id(receiver)
for senderkey in sendersBack.get(backKey,()):
try:
@ -417,8 +427,10 @@ def _removeSender(senderkey):
pass
# Senderkey will only be in senders dictionary if sender
# could be weakly referenced.
try: del senders[senderkey]
except: pass
try:
del senders[senderkey]
except:
pass
def _removeBackrefs( senderkey):

57
django/dispatch/robust.py Normal file
View File

@ -0,0 +1,57 @@
"""Module implementing error-catching version of send (sendRobust)"""
from django.dispatch.dispatcher import Any, Anonymous, liveReceivers, getAllReceivers
from django.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

View File

@ -109,12 +109,19 @@ class BoundMethodWeakref(object):
try:
if callable( function ):
function( self )
except Exception:
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
@ -127,8 +134,8 @@ class BoundMethodWeakref(object):
"""Give a friendly representation of the object"""
return """%s( %s.%s )"""%(
self.__class__.__name__,
self.weakSelf(),
self.weakFunc().__name__,
self.selfName,
self.funcName,
)
__repr__ = __str__
def __nonzero__( self ):