Python - how to check if weak link is available

I pass some weak references from Python to the C ++ class, but C ++ destructors are actively trying to access ref when the real object is already dead, it is obvious that it crashes ...

Is there any Python C / API approach to find out if there is a Python link yet or some other known workaround?

thank

+5
source share
2 answers

If you call PyWeakref_GetObject on a weak link, it should return either Py_None or NULL, I forget that. But you have to check if it returns one of them, and this will tell you that the reference object is no longer alive.

+2
source

Python C API:

PyObject * PyWeakref_GetObject (PyObject * ref)
        : .
    , ref.     , None. 2.2.

+3

All Articles