Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 22c001b

Browse files
committed
Described responsibilty of weakly referenced extension types to initialize
the weakreflist to NULL in the constructor and to fill the tp_flags slot with Py_TPFLAGS_HAVE_WEAKREFS. Closes SF bug 586583.
1 parent 861bb02 commit 22c001b

1 file changed

Lines changed: 23 additions & 2 deletions

File tree

Doc/lib/libweakref.tex

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,9 @@ \subsection{Weak References in Extension Types
217217
weak reference mechanism; it must be initialized to \NULL{} by the
218218
object's constructor. It must also set the \member{tp_weaklistoffset}
219219
field 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}
223225
typedef 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+
245266
The only further addition is that the destructor needs to call the
246267
weak reference manager to clear any weak references. This should be
247268
done before any other parts of the destruction have occurred, but is

0 commit comments

Comments
 (0)