[Python-Dev] __class_init__

Thomas Heller thomas.heller@ion-tof.com
Fri, 16 Nov 2001 19:03:37 +0100


When implementing a metatype in C (a subtype of PyType_Type),
aren't the tp_basicsize and tp_itemsize fields inherited?

This question is not covered in PEP 253...

I've attached the code below.

Thanks,

Thomas
-------------------------------------------------
#include <Python.h>

static PyMethodDef methods[] = {
    { NULL, NULL },  /* Sentinel */
};

PyTypeObject MyMeta_Type = {
    PyObject_HEAD_INIT(NULL)
    0,     /* ob_size */
    "mytype",    /* tp_name */
    0, /*sizeof(PyTypeObject),  /* tp_basicsize */
    0,     /* tp_itemsize */
    0,     /* tp_dealloc */
    0,     /* tp_print */
    0,     /* tp_getattr */
    0,     /* tp_setattr */
    0,     /* tp_compare */
    0,     /* tp_repr */
    0,     /* tp_as_number */
    0,     /* tp_as_sequence */
    0,     /* tp_as_mapping */
    0,     /* tp_hash */
    0,     /* tp_call */
    0,     /* tp_str */
    0,     /* tp_getattro */
    0,     /* tp_setattro */
    0,     /* tp_as_buffer */
    Py_TPFLAGS_DEFAULT,   /* tp_flags */
    "a meta type",   /* tp_doc */
    0,     /* tp_traverse */
    0,     /* tp_clear */
    0,     /* tp_richcompare */
    0,     /* tp_weaklistoffset */
    0,     /* tp_iter */
    0,     /* tp_iternext */
    0,     /* tp_methods */
    0,     /* tp_members */
    0,     /* tp_getset */
    0,     /* tp_base */
    0,     /* tp_dict */
    0,     /* tp_descr_get */
    0,     /* tp_descr_set */
    0,     /* tp_dictoffset */
    0,     /* tp_init */
    0,     /* tp_alloc */
    0,     /* tp_new */
    0,     /* tp_free */
};

DL_EXPORT(void)
initmetatype(void)
{
    PyObject *m, *d;

    MyMeta_Type.ob_type = &PyType_Type;
    MyMeta_Type.tp_base = &PyType_Type;
    MyMeta_Type.tp_basicsize = PyType_Type.tp_basicsize;
    MyMeta_Type.tp_itemsize = PyType_Type.tp_itemsize;

    if (PyType_Ready(&MyMeta_Type) < 0)
        return;

    m = Py_InitModule3("metatype", methods, "provides a metatype");
    d = PyModule_GetDict(m);

    PyDict_SetItemString(d, "mytype", (PyObject *)&MyMeta_Type);
}