I have some C++ code that delivers events to Python objects. Observers are kept as weak_ptrs, so they don't have to deregister.
This works in C++, but bridging weak pointers and Python weak references is troublesome (I also want Python event handlers not being kept alive by subscriptions, same as in C++ code). In order to have a live observer, something needs to have a shared pointer to it while the object is alive, so it boils down to having an observer in Python land control the lifetime of a C++ observer object.
The approaches I've come up with so far involve a fair amount of boilerplate and intermediate objects (e.g. creating another PyTypeObject for a type that keeps a C++ observer and a weak reference to the Python observer and setting it as a member of Python observer, so it dies with it).
The question is, is there any obvious way to do it?
I would write a python wrapper over the C++ module and dispatch to python observers in the python wrapper. Would that be enough?
When you mention that something needs to have a shared pointer, would it be enough if that shared pointer is on the stack until given observer returns?
Track the lifetime of a CPython object from C extension