Documentation bugs in 3.1 - C-API - TypeObjects

"Martin v. Löwis" martin at
Sun Nov 15 02:53:29 CET 2009

> This cannot work, because Foo_Type is no PyObject but a PyVarObject
> (independent
> of the use of PyVarObject_HEAD_INIT or PyObject_HEAD_INIT). The code
> line would
> work so:
> ((PyObject *)&Foo_Type)->ob_type = &PyType_Type

However, this is not what you should use. Instead, use

Py_Type(&Foo_Type) = &PyType_Type

> If the type is not subtypable (doesn’t have the
> Py_TPFLAGS_BASETYPE flag bit set), it is permissible to call the
> object deallocator directly instead of via tp_free.
> "
> What ? Where do we "call" these methods ?

You should typically call tp_free inside of tp_dealloc. For example,
string_dealloc ends with


In the specific case (Py_TYPE(op) is not subtypeable), you could
alternatively also call


If there are subclasses, they might have used a different allocator
(rather than the object allocator), hence you must call the deallocator
through tp_free.


More information about the Python-list mailing list