sizeof PyTypeObject difference

Tom Widrick lordmacro at hotmail.com
Mon Sep 30 10:09:54 CEST 2002


> > I'm trying to make a metatype in C, but there seems to be some
difference
> > between the size of the type object.
> >
> > typedef struct {
> >  PyTypeObject type;
> > } MyMetaObject;
> >
> >
> > and in the tp_basicsize field I place sizeof(MyMetaObject) the value is
only
> > 196.
>
> This is a type declararation. In C, you cannot put values into a type
> declaration. So I assume you are creating a value (an object) of
> struct MyMetaObject, and you put 196 into this value.

Well, I didn't specify but I figured it obvious the sizeof(MyMetaObject)
goes into the PyTypeObject of the metatype, i.e.

PyTypeObject MyMetaType_Type = {
 PyObject_HEAD_INIT(NULL)
 0,          /*ob_size*/
 "MyMetaType",       /*tp_name*/
 sizeof(MyMetaObject),          /*tp_basicsize*/

> If so, you misunderstand the meaning of tp_basicsize: It does not
> describe the size of the type object, but the size of instances of the
> type.

Well, aren't instances of a metatype types?  I know this statement was
probably made from invalid assumptions, but just as a point.
Since types are variable sized, as you say, how does this tie in with
tp_basicsize and metatypes?

> That is hard to believe. Declaring the typedef alone can't cause a
> core dump. Defining an object of that type, and putting the value of
> 196 into it can't cause a core dump, either. You must be doing other
> things, like using the resulting object in some way, to cause a core
> dump.

Trying to use the metatype as a metaclass for a python class.

> > What is the cause of this difference?
>
> The type type is really a variable-sized object. You are seeing the
> size of struct etype, which is hidden in typeobject.c.
>

> You can't currently declare a type with additional C fields, since
> type objects are variable-sized. One approach is to put additional
> things into the type's __dict__. Another approach is to use Christian
> Tismer's patches to overcome this limitation; I recommend that you
> contact Christian.

Thanks for the info on that one. Very helpful. Wish that was stuck in PEP
253.

As an aside, are there any other quarks about making metatypes in C? the
xxsubtype.c example in the distrib is helpful, along with the PEPs, but I'm
yet to find a good example of a metatype implemented in C (besides the 2
builtin metatypes, but I don't think those serve as  great minimalistic
examples).

Tom









More information about the Python-list mailing list