The Python manual (I'm looking at 2.2.1, but I think
it's true since 2.1) omits one detail for creating weak
references on extension types:
It fails to indicate that the tp_flags field of the
PyTypeObject has to
have Py_TPFLAGS_HAVE_WEAKREFS added into it as well.
Without this, it doesn't work.
So the second code block would read as follows:
PyTypeObject PyInstance_Type = {
PyObject_HEAD_INIT(&PyType_Type)
0,
"module.instance",
/* Lots of stuff omitted for brevity... */
Py_TPFLAGS_HAVE_WEAKREFS, /* tp_flags */
NULL, /*doc*/
0L,0L,0L,
offsetof(PyInstanceObject, in_weakreflist) /*
tp_weaklistoffset */
/* Possibly more stuff here... */
};
----------------------
You may also want to emphasize that the in_weakreflist
field that is added should be initialized in the object
constructor. Since you show the code in
instance_dealloc(), it might make sense to show the
allocation function too, with the code that sets the
in_weakreflist field to NULL.
Yea, I know this should be obvious and it is mentioned
in the first
paragraph, but somehow I got confused into thinking it
would be initialize for me because there wasn't a
seperate code example.
Here is another code block that could be shown before
the instance_dealloc block, to make this clear for
dummies like me:
static PyObject *
instance_new() {
/* Code removed for brevity */
self = (PyInstance_Object
*)PyObject_NEW(PyInstance_Object,
&PyInstance_Type);
if (self == NULL)
return NULL;
/* More code removed for brevity */
self->in_weakreflist = NULL;
return (PyObject *) self;
}
Hope that helps...
- Stephan
------------------------------------------------------------------------
Wing IDE for Python
Archaeopteryx Software, Inc
www.wingide.com Take Flight!
|