Skip to content

Instantly share code, notes, and snippets.

@vindolin

vindolin/connect_weakly

Last active Aug 29, 2015
Embed
What would you like to do?
lass __WeakWrapper(object):
def __init__(self, sender, callback, *userdata):
self.weak_obj = weakref.ref(callback.im_self)
self.weak_fun = weakref.ref(callback.im_func)
self.userdata = userdata
self.sender = sender
self.handle = None
def __call__(self, source):
obj = self.weak_obj()
func = self.weak_fun()
if obj is not None and func is not None:
return func(obj, source, self.userdata)
elif self.handle is not None:
self.sender.disconnect(self.handle)
self.handle = None
self.sender = None
def connect_weakly(sender, signal, callback, *userdata):
wrapper = __WeakWrapper(sender, callback, userdata)
wrapper.handle = sender.connect(signal, wrapper)
return wrapper
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment