Track CPython object lifetime with C extension

I have C ++ code that passes events to Python objects. Observers are stored as weak_ptr s, so they don’t need to unregister.

This works in C ++, but the weak pointer bridges and weak Python links are unpleasant (I also want Python event handlers not to be supported by subscriptions the same as in C ++ code). In order to have a living observer, something must have a common pointer to it while the object is alive, so it comes down to the fact that the observer on the Python earth controls the lifetime of the C ++ observer object.

The approaches I came to so far include a significant number of template and intermediate objects (e.g., creating another PyTypeObject for a type that stores a C ++ observer and a weak reference to the Python observer and sets it as a member of the Python observer, so it dies with him).

The question is, is there an obvious way to do this?

+4
source share
1 answer

I would write a python shell over a C ++ module and send python observers to a python shell. Would that be enough?

When you mention that something must have a common pointer, would it be enough if that common pointer were on the stack until the observer returned?

0
source

Source: https://habr.com/ru/post/1399913/


All Articles