parent
c29d6f7676
commit
297c009cf2
|
@ -106,34 +106,31 @@ class Signal(object):
|
||||||
assert argspec[2] is not None, \
|
assert argspec[2] is not None, \
|
||||||
"Signal receivers must accept keyword arguments (**kwargs)."
|
"Signal receivers must accept keyword arguments (**kwargs)."
|
||||||
|
|
||||||
receiver_id = _make_id(receiver)
|
|
||||||
if dispatch_uid:
|
if dispatch_uid:
|
||||||
lookup_key = (dispatch_uid, _make_id(sender))
|
lookup_key = (dispatch_uid, _make_id(sender))
|
||||||
else:
|
else:
|
||||||
lookup_key = (receiver_id, _make_id(sender))
|
lookup_key = (_make_id(receiver), _make_id(sender))
|
||||||
|
|
||||||
if weak:
|
if weak:
|
||||||
ref = weakref.ref
|
ref = weakref.ref
|
||||||
original_receiver = receiver
|
receiver_object = receiver
|
||||||
# Check for bound methods
|
# Check for bound methods
|
||||||
if hasattr(receiver, '__self__') and hasattr(receiver, '__func__'):
|
if hasattr(receiver, '__self__') and hasattr(receiver, '__func__'):
|
||||||
ref = WeakMethod
|
ref = WeakMethod
|
||||||
original_receiver = original_receiver.__self__
|
receiver_object = receiver.__self__
|
||||||
if sys.version_info >= (3, 4):
|
if sys.version_info >= (3, 4):
|
||||||
receiver = ref(receiver)
|
receiver = ref(receiver)
|
||||||
weakref.finalize(original_receiver, self._remove_receiver, receiver_id=receiver_id)
|
weakref.finalize(receiver_object, self._remove_receiver)
|
||||||
else:
|
else:
|
||||||
receiver = ref(receiver, self._remove_receiver)
|
receiver = ref(receiver, self._remove_receiver)
|
||||||
# Use the id of the weakref, since that's what passed to the weakref callback!
|
|
||||||
receiver_id = _make_id(receiver)
|
|
||||||
|
|
||||||
with self.lock:
|
with self.lock:
|
||||||
self._clear_dead_receivers()
|
self._clear_dead_receivers()
|
||||||
for r_key, _, _ in self.receivers:
|
for r_key, _ in self.receivers:
|
||||||
if r_key == lookup_key:
|
if r_key == lookup_key:
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
self.receivers.append((lookup_key, receiver, receiver_id))
|
self.receivers.append((lookup_key, receiver))
|
||||||
self.sender_receivers_cache.clear()
|
self.sender_receivers_cache.clear()
|
||||||
|
|
||||||
def disconnect(self, receiver=None, sender=None, weak=True, dispatch_uid=None):
|
def disconnect(self, receiver=None, sender=None, weak=True, dispatch_uid=None):
|
||||||
|
@ -166,7 +163,7 @@ class Signal(object):
|
||||||
with self.lock:
|
with self.lock:
|
||||||
self._clear_dead_receivers()
|
self._clear_dead_receivers()
|
||||||
for index in xrange(len(self.receivers)):
|
for index in xrange(len(self.receivers)):
|
||||||
(r_key, _, _) = self.receivers[index]
|
(r_key, _) = self.receivers[index]
|
||||||
if r_key == lookup_key:
|
if r_key == lookup_key:
|
||||||
del self.receivers[index]
|
del self.receivers[index]
|
||||||
break
|
break
|
||||||
|
@ -270,7 +267,7 @@ class Signal(object):
|
||||||
self._clear_dead_receivers()
|
self._clear_dead_receivers()
|
||||||
senderkey = _make_id(sender)
|
senderkey = _make_id(sender)
|
||||||
receivers = []
|
receivers = []
|
||||||
for (receiverkey, r_senderkey), receiver, _ in self.receivers:
|
for (receiverkey, r_senderkey), receiver in self.receivers:
|
||||||
if r_senderkey == NONE_ID or r_senderkey == senderkey:
|
if r_senderkey == NONE_ID or r_senderkey == senderkey:
|
||||||
receivers.append(receiver)
|
receivers.append(receiver)
|
||||||
if self.use_caching:
|
if self.use_caching:
|
||||||
|
@ -290,7 +287,7 @@ class Signal(object):
|
||||||
non_weak_receivers.append(receiver)
|
non_weak_receivers.append(receiver)
|
||||||
return non_weak_receivers
|
return non_weak_receivers
|
||||||
|
|
||||||
def _remove_receiver(self, receiver=None, receiver_id=None, _make_id=_make_id):
|
def _remove_receiver(self, receiver=None):
|
||||||
# Mark that the self.receivers list has dead weakrefs. If so, we will
|
# Mark that the self.receivers list has dead weakrefs. If so, we will
|
||||||
# clean those up in connect, disconnect and _live_receivers while
|
# clean those up in connect, disconnect and _live_receivers while
|
||||||
# holding self.lock. Note that doing the cleanup here isn't a good
|
# holding self.lock. Note that doing the cleanup here isn't a good
|
||||||
|
|
Loading…
Reference in New Issue