mirror of
https://github.com/python/cpython.git
synced 2025-11-18 01:57:37 +00:00
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.
This commit is contained in:
parent
861bb02448
commit
22c001bd29
1 changed files with 23 additions and 2 deletions
|
|
@ -217,7 +217,9 @@ For an object to be weakly referencable, the extension must include a
|
||||||
weak reference mechanism; it must be initialized to \NULL{} by the
|
weak reference mechanism; it must be initialized to \NULL{} by the
|
||||||
object's constructor. It must also set the \member{tp_weaklistoffset}
|
object's constructor. It must also set the \member{tp_weaklistoffset}
|
||||||
field of the corresponding type object to the offset of the field.
|
field of the corresponding type object to the offset of the field.
|
||||||
For example, the instance type is defined with the following structure:
|
Also, it needs to add \constant{Py_TPFLAGS_HAVE_WEAKREFS} to the
|
||||||
|
tp_flags slot. For example, the instance type is defined with the
|
||||||
|
following structure:
|
||||||
|
|
||||||
\begin{verbatim}
|
\begin{verbatim}
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
|
@ -238,10 +240,29 @@ PyTypeObject PyInstance_Type = {
|
||||||
|
|
||||||
/* Lots of stuff omitted for brevity... */
|
/* Lots of stuff omitted for brevity... */
|
||||||
|
|
||||||
offsetof(PyInstanceObject, in_weakreflist) /* tp_weaklistoffset */
|
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_WEAKREFS /* tp_flags */
|
||||||
|
0, /* tp_doc */
|
||||||
|
0, /* tp_traverse */
|
||||||
|
0, /* tp_clear */
|
||||||
|
0, /* tp_richcompare */
|
||||||
|
offsetof(PyInstanceObject, in_weakreflist), /* tp_weaklistoffset */
|
||||||
};
|
};
|
||||||
\end{verbatim}
|
\end{verbatim}
|
||||||
|
|
||||||
|
The type constructor is responsible for initializing the weak reference
|
||||||
|
list to \NULL:
|
||||||
|
|
||||||
|
\begin{verbatim}
|
||||||
|
static PyObject *
|
||||||
|
instance_new() {
|
||||||
|
/* Other initialization stuff omitted for brevity */
|
||||||
|
|
||||||
|
self->in_weakreflist = NULL;
|
||||||
|
|
||||||
|
return (PyObject *) self;
|
||||||
|
}
|
||||||
|
\end{verbatim}
|
||||||
|
|
||||||
The only further addition is that the destructor needs to call the
|
The only further addition is that the destructor needs to call the
|
||||||
weak reference manager to clear any weak references. This should be
|
weak reference manager to clear any weak references. This should be
|
||||||
done before any other parts of the destruction have occurred, but is
|
done before any other parts of the destruction have occurred, but is
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue