This page is about the wrapper classes for libsigc++ signals, pysigc.
Python sigc signal proxies
(For this paragraph i'll assume that you're familiar with libsigc++; if you're not please consult the tutorial and/or source code.)
First off, to connect to a sigc signal you need to specify a sigc functor, for which to construct you need to specify the address of a function. Obviously, we can not take the address of a Python function, which renders it impossible to, even with a little help, directly connect a Python closure to a sigc signal.
What we can do, however, is to (and we do..) have a templated proxy class which has a member function that acts as the signal's callback, and there invokes the Python closure. In (very) short terms (but it should nevertheless be clear), to keep a Python closure connected to a sigc signal, we need to keep the proxy alive; without it, the Python closure doesn't get called.
Wrong
(init here is the constructor of an mpx.Plugin derived class, which gets passed its own id (a long long integer), the Player object, and MCS, the configuration storage backend)
def __init__(self, id, player, mcs): self.player = player self.lib = player.get_library() self.lib.signal_new_album().connect(self.on_new_album) def on_new_album(self, mpx_track, album_id, album_artist_id): # do something with the new album
This is wrong(TM) because, while we get the sigc pysigc signal proxy by calling self.lib.signal_new_album() and then connect to it, this instance is temporary, and dies the moment the interpreter moves to the next line.
The proxy dies, and we never get called back.
Right
def __init__(self, id, player, mcs): self.player = player self.lib = player.get_library() self.lib_signal_new_album = self.lib.signal_new_album() self.lib_signal_new_album.connect(self.on_new_album) def on_new_album(self, mpx_track, album_id, album_artist_id): # do something with the new album
This time, we keep the sigc pysigc signal proxy around, which on the C++ side of things keeps the sigc signal and our Python closure connected, which is all we ever wanted!
