On Fri, 15 Jan 2021 09:36:11 +0100 Julien Danjou <julien@danjou.info> wrote:
On Thu, Jan 14 2021, Tim Peters wrote:
I'm not clear on exactly what it is you're after, but CPython faces the same question all the time: _given_ a pointer to an object, is there, or is there not, a GC header prepended? That's answered by this C API function:
""" int PyObject_IS_GC(PyObject *obj)
Returns non-zero if the object implements the garbage collector protocol, otherwise returns 0.
The object cannot be tracked by the garbage collector if this function returns 0. """
FYI, the implementation usually resolves it by looking at whether obj's type object has the Py_TPFLAGS_HAVE_GC flag set.
Right, but that only works if you have the PyObject address. If you all got is a pointer returned by PyObject_Malloc(), you don't know if the PyObject is at this pointer address, or after the PyGC_HEAD header that is prepended. :(
Also note that PyObject_Malloc() may also be used to allocate non-objects, for example a bytearray's payload, IIRC. Regards Antoine.