[Python-Dev] GC and ExtensionClass - a summary of the problem and a workaround

Fredrik Lundh fredrik@effbot.org
Thu, 17 May 2001 09:00:20 +0200


Skip wrote:
> When defining ExtensionClass types, you need to create and initialize a
> PyExtensionClass struct.  It looks something like so:
> 
>     PyExtensionClass PyGtkTreeSortable_Type = {
>        PyObject_HEAD_INIT(NULL)
>        0, /* ob_size */
>        "GtkTreeSortable", /* tp_name */
>        sizeof(PyPureMixinObject), /* tp_basicsize */
>        ...
>     };
> 
> Note that the parameter to the PyObject_HEAD_INIT macro is NULL.  It would
> normally be the address of a type object (e.g. &PyType_Type).  However, Jim
> Fulton pointed out that on Windows you can't get the address of &PyType_Type
> object at compile time. Accordingly, ExtensionClass provides a
> PyExtensionClass_Export macro whose responsibility is, in part, to set the
> ob_type field appropriately at runtime

footnote: this is usually done in the module init function, *before*
the call to Py_InitModule.  see:

    http://www.python.org/doc/FAQ.html#3.24

if the garbage collector can run after Python calls a module's init-
function, but before that module calls back into Python, anything
can happen...

Cheers /F