In other words, is it safe for C code, given a reference to Python object to assume that, provided it called Py_INCREF
on it, the pointer will not become invalid?
If you are wondering how this may happen: for example, Java GC is allowed to relocate objects in memory to prevent memory fragmentation. This will, however, preclude you from using "raw" pointers to Java objects, since you may get a garbage reference.
Somewhat related: how can C code determine that Python object was garbage-collected, if it doesn't want to increase reference count (for example, if implementing WeakKeyDictionary
)?