Hi,

2009/7/13 Erik Groeneveld <e...@cq2.nl>:
> 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<amaur...@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?

The best is probably to store it in the class dictionary:
    PyObject_SetAttrString(self, "_javatype_", PyLong_FromVoidPtr(p));

-- 
Amaury Forgeot d'Arc
_______________________________________________
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to