I’ve found the documentation here on how to change a static extension type into a heap type. However, I’m having trouble working out the mechanics of actually how to do the conversion based on those docs. It would be really nice to see a version of the extension types tutorial that uses heap types, but that’s not covered there. Is anyone aware of another resource I could look at to compare the implementation of a static type and heap type in a a C extension module?
Thanks for bringing this up. These docs definitely need to be updated to reflect current recommended practise (using heap types, module state, and multi-phase init). There are multiple issues[1][2] regarding such doc updates, but unfortunately nothing has been done yet.
I’m trying to follow along with the guide linked by @ngoldbaum, as well as the xxlimited example reference by @erlendaasland while trying to port the _zope_interface_c_optimizations extension module to multi-phase initialization and heap types.
One thing I don’t see covered in either the guide or the example module is how to deal with weakrefs in the New World Order. The existing _z_i_c module’s Spec holds an explicit weakreflist and clears it in Spec_dealloc. The static TypeObj sets the tp_weaklistoffset field in its type definition as you would expect:
The docs for tp_members discourage the use of tp_weaklistoffset in favor of Py_TPFLAGS_MANAGED_WEAKREF, but I cannot find any description of the difference in the semantics. Given that my updated module can produce segfaults where the old version did not, and that the other refcount stuff is identical between them, I suspect that I’m not handling the weakrefs case correcty.
Work to date (started Monday at PyCon US sprints) and more musings are at this draft PR.