@@ -217,7 +217,9 @@ \subsection{Weak References in Extension Types
217217weak reference mechanism; it must be initialized to \NULL {} by the
218218object's constructor. It must also set the \member {tp_weaklistoffset}
219219field of the corresponding type object to the offset of the field.
220- For example, the instance type is defined with the following structure:
220+ Also, it needs to add \constant {Py_TPFLAGS_HAVE_WEAKREFS} to the
221+ tp_flags slot. For example, the instance type is defined with the
222+ following structure:
221223
222224\begin {verbatim }
223225typedef struct {
@@ -238,10 +240,29 @@ \subsection{Weak References in Extension Types
238240
239241 /* Lots of stuff omitted for brevity... */
240242
241- offsetof(PyInstanceObject, in_weakreflist) /* tp_weaklistoffset */
243+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_WEAKREFS /* tp_flags */
244+ 0, /* tp_doc */
245+ 0, /* tp_traverse */
246+ 0, /* tp_clear */
247+ 0, /* tp_richcompare */
248+ offsetof(PyInstanceObject, in_weakreflist), /* tp_weaklistoffset */
242249};
243250\end {verbatim }
244251
252+ The type constructor is responsible for initializing the weak reference
253+ list to \NULL :
254+
255+ \begin {verbatim }
256+ static PyObject *
257+ instance_new() {
258+ /* Other initialization stuff omitted for brevity */
259+
260+ self->in_weakreflist = NULL;
261+
262+ return (PyObject *) self;
263+ }
264+ \end {verbatim }
265+
245266The only further addition is that the destructor needs to call the
246267weak reference manager to clear any weak references. This should be
247268done before any other parts of the destruction have occurred, but is
0 commit comments