sizeof PyTypeObject difference

Martin v. Loewis martin at v.loewis.de
Mon Sep 30 08:01:19 CEST 2002


"Tom Widrick" <lordmacro at hotmail.com> writes:

> 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.

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.

> This causes a core dump.

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.

> 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.

> And what is the "proper" way to do this?

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.

Regards,
Martin



More information about the Python-list mailing list