summaryrefslogtreecommitdiffstats
path: root/Doc/ext
diff options
context:
space:
mode:
authorFred Drake <fdrake@acm.org>2006-07-29 20:04:42 (GMT)
committerFred Drake <fdrake@acm.org>2006-07-29 20:04:42 (GMT)
commit45540b0922f1858c4214287941b9c3b2888767e0 (patch)
tree3b37bef9127a0f86f96615903a2c15969ed850ba /Doc/ext
parent9964fdb466d7338c37e30b302d42bb0df697d234 (diff)
downloadcpython-45540b0922f1858c4214287941b9c3b2888767e0.zip
cpython-45540b0922f1858c4214287941b9c3b2888767e0.tar.gz
cpython-45540b0922f1858c4214287941b9c3b2888767e0.tar.bz2
SF bug #1193966: Weakref types documentation misplaced
The information about supporting weakrefs with types defined in C extensions is moved to the Extending & Embedding manual. Py_TPFLAGS_HAVE_WEAKREFS is no longer mentioned since it is part of Py_TPFLAGS_DEFAULT.
Diffstat (limited to 'Doc/ext')
-rw-r--r--Doc/ext/newtypes.tex79
1 files changed, 79 insertions, 0 deletions
diff --git a/Doc/ext/newtypes.tex b/Doc/ext/newtypes.tex
index 3672533..a485a15 100644
--- a/Doc/ext/newtypes.tex
+++ b/Doc/ext/newtypes.tex
@@ -1564,6 +1564,85 @@ without setting an exception or it may set \exception{StopIteration};
avoiding the exception can yield slightly better performance. If an
actual error occurs, it should set an exception and return \NULL.
+
+\subsection{Weak Reference Support\label{weakref-support}}
+
+One of the goals of Python's weak-reference implementation is to allow
+any type to participate in the weak reference mechanism without
+incurring the overhead on those objects which do not benefit by weak
+referencing (such as numbers).
+
+For an object to be weakly referencable, the extension must include a
+\ctype{PyObject*} field in the instance structure for the use of the
+weak reference mechanism; it must be initialized to \NULL{} by the
+object's constructor. It must also set the \member{tp_weaklistoffset}
+field of the corresponding type object to the offset of the field.
+For example, the instance type is defined with the following
+structure:
+
+\begin{verbatim}
+typedef struct {
+ PyObject_HEAD
+ PyClassObject *in_class; /* The class object */
+ PyObject *in_dict; /* A dictionary */
+ PyObject *in_weakreflist; /* List of weak references */
+} PyInstanceObject;
+\end{verbatim}
+
+The statically-declared type object for instances is defined this way:
+
+\begin{verbatim}
+PyTypeObject PyInstance_Type = {
+ PyObject_HEAD_INIT(&PyType_Type)
+ 0,
+ "module.instance",
+
+ /* Lots of stuff omitted for brevity... */
+
+ Py_TPFLAGS_DEFAULT, /* tp_flags */
+ 0, /* tp_doc */
+ 0, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ offsetof(PyInstanceObject, in_weakreflist), /* tp_weaklistoffset */
+};
+\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
+weak reference manager to clear any weak references. This should be
+done before any other parts of the destruction have occurred, but is
+only required if the weak reference list is non-\NULL:
+
+\begin{verbatim}
+static void
+instance_dealloc(PyInstanceObject *inst)
+{
+ /* Allocate temporaries if needed, but do not begin
+ destruction just yet.
+ */
+
+ if (inst->in_weakreflist != NULL)
+ PyObject_ClearWeakRefs((PyObject *) inst);
+
+ /* Proceed with object destruction normally. */
+}
+\end{verbatim}
+
+
\subsection{More Suggestions}
Remember that you can omit most of these functions, in which case you