Ticket #16547: signal.patch

File signal.patch, 2.8 KB (added by EvilX, 4 years ago)
  • dispatch/dispatcher.py

     
    3333        self.providing_args = set(providing_args)
    3434        self.lock = threading.Lock()
    3535
    36     def connect(self, receiver, sender=None, weak=True, dispatch_uid=None):
     36    def connect(self, receiver, sender=None, weak=True, dispatch_uid=None, weight=0):
    3737        """
    3838        Connect receiver to sender for signal.
    3939   
     
    6767                An identifier used to uniquely identify a particular instance of
    6868                a receiver. This will usually be a string, though it may be
    6969                anything hashable.
     70
     71            weight
     72                Signal priority. Greater run first.
    7073        """
    7174        from django.conf import settings
    7275       
     
    101104
    102105        self.lock.acquire()
    103106        try:
    104             for r_key, _ in self.receivers:
     107            for r_key, _, w in self.receivers:
    105108                if r_key == lookup_key:
    106109                    break
    107110            else:
    108                 self.receivers.append((lookup_key, receiver))
     111                self.receivers.append((lookup_key, receiver, weight))
    109112        finally:
    110113            self.lock.release()
    111114
     
    139142        self.lock.acquire()
    140143        try:
    141144            for index in xrange(len(self.receivers)):
    142                 (r_key, _) = self.receivers[index]
     145                (r_key, _, weight) = self.receivers[index]
    143146                if r_key == lookup_key:
    144147                    del self.receivers[index]
    145148                    break
     
    221224        none_senderkey = _make_id(None)
    222225        receivers = []
    223226
    224         for (receiverkey, r_senderkey), receiver in self.receivers:
     227        for (receiverkey, r_senderkey), receiver, weight in sorted(self.receivers, key=lambda x: x[2], reverse=True):
    225228            if r_senderkey == none_senderkey or r_senderkey == senderkey:
    226229                if isinstance(receiver, WEAKREF_TYPES):
    227230                    # Dereference the weak reference.
     
    240243        self.lock.acquire()
    241244        try:
    242245            to_remove = []
    243             for key, connected_receiver in self.receivers:
     246            for key, connected_receiver, weight in self.receivers:
    244247                if connected_receiver == receiver:
    245248                    to_remove.append(key)
    246249            for key in to_remove:
    247250                last_idx = len(self.receivers) - 1
    248251                # enumerate in reverse order so that indexes are valid even
    249252                # after we delete some items
    250                 for idx, (r_key, _) in enumerate(reversed(self.receivers)):
     253                for idx, (r_key, _, weight) in enumerate(reversed(self.receivers)):
    251254                    if r_key == key:
    252255                        del self.receivers[last_idx-idx]
    253256        finally:
Back to Top