[Pyrex] Weakrefs

Chris Perkins chrisperkins99 at gmail.com
Thu Nov 30 18:34:19 UTC 2006


I'm trying to understand how (or whether) Pyrex supports weakly-referencable
objects.

According to the Pyrex docs, all that is required is to add "cdef object
__weakref__" to the class.  That seems to work only superficially for me.
I am able to create weak refs to instances, but I get mysterious crashes all
over the place if I actually use them.

According to this:
http://effbot.org/lib/weakref.html#weak-references-in-extension-types
there are 5 things that need to be done to make an extension type
weak-referenceable:

1) include a PyObject* field in the instance structure.
2) initialize it to NULL in the constructor
3) set the tp_weaklistoffset field of the type object
4) add Py_TPFLAGS_HAVE_WEAKREFS to the tp_flags slot
5) call PyObject_ClearWeakRefs in the dealloc function

As far as I can tell by looking at the generated C code, Pyrex only does 1)
and 3).  As for 2), it sets __weakref__ to Py_None instead of NULL (maybe
that's OK, I don't know).  It doesn't seem to do 4) or 5) at all.

Is this intentional?  I don't know much about writing extension types, so
it's entirely possible that the generated code is fine.  I would just like to
be sure before I spend more time hunting down the cause of the crash.

Thanks,

Chris Perkins



More information about the Pyrex mailing list