[Python-Dev] C new-style classes and GC

Guido van Rossum guido@python.org
Sun, 18 May 2003 15:02:55 -0400


> PEP 253 may be partly out of date here -- or not.  In the section on
> creating a subclassable type, it says:
> 
> """
>    The base type must do the following:
> 
>       - Add the flag value Py_TPFLAGS_BASETYPE to tp_flags.
> 
>       - Declare and use tp_new(), tp_alloc() and optional tp_init()
>         slots.
> 
>       - Declare and use tp_dealloc() and tp_free().
> 
>       - Export its object structure declaration.
> 
>       - Export a subtyping-aware type-checking macro.
> """
> 
> This doesn't leave a choice about defining tp_alloc() or tp_free() -- it
> says both are required.  For a subclassable type, I believe both must
> actually be implemented too.
> 
> For a non-subclassable type, I expect they're optional.  But if you don't
> define tp_free in that case, then I believe you must also not do the
> 
>     obj->ob_type->tp_free(obj)
> 
> business in the tp_dealloc slot (else it will segfault).

PyType_Ready() inherits tp_free from the base class, so it's optional.

--Guido van Rossum (home page: http://www.python.org/~guido/)