[Python-Dev] Define metatype and a type that uses it
Erik Groeneveld
erik at cq2.nl
Mon Jul 13 12:08:29 CEST 2009
Amaury,
Thank you very much for your detailed explanation. It helps to
understand it better, and it mostly works now. There is one thing
however:
On Wed, Jul 8, 2009 at 17:35, Amaury Forgeot d'Arc<amauryfa at gmail.com> wrote:
> - Don't define a JObjectMeta struct, use JObjectType directly instead.
> An instance of the metatype is the type itself!
> - Don't set JObjectMetaType.tp_basicsize, let it inherit from the base
> type (the correct value would be sizeof(PyHeapTypeObject), this is
> important in order to create derived classes in python)
I'd like to add a C pointer field to the metatype instance (the type).
So, contrary to your advice, I have defined:
typedef struct {
PyHeapTypeObject x;
void* p;
} JObjectMeta;
This seems the way to do it for objects, but for types, it doesn't
seem right, as the p member turns out to be overwritten unexpectedly
at runtime.
Reading Python's object.h file it turns out that PyHeapTypeObject
'extends' PyTypeObject, which in turn has a PyObject_VAR_HEAD init
macro. So mustn't:
PyTypeObject JObjectMetaType = {
PyObject_HEAD_INIT(NULL)
};
actually be:
PyTypeObject JObjectMetaType = {
PyVarObject_HEAD_INIT(NULL, 1)
};
with:
JObjectMetaType.tp_basic_size = sizeof(JObjectMeta);
JObjectMetaType.tp_itemsize = sizeof(void*);
I tried this, but it doesn't keep my app from dumping core on an
overwritten 'p'.
My question basically is: how can I define a pointer for each type
created with this metatype, such as is intended by the JObjectMeta
struct?
Best regards
Erik Groeneveld
More information about the Python-Dev
mailing list