|
| 1 | +\section{\module{weakref} --- |
| 2 | + Weak references} |
| 3 | + |
| 4 | +\declaremodule{extension}{weakref} |
| 5 | +\moduleauthor{Fred L. Drake, Jr.}{ [email protected]} |
| 6 | +\sectionauthor{Fred L. Drake, Jr.}{ [email protected]} |
| 7 | + |
| 8 | +\versionadded{2.1} |
| 9 | + |
| 10 | + |
| 11 | +The \module{weakref} module allows the Python programmer to create |
| 12 | +\dfn{weak references} to objects. |
| 13 | + |
| 14 | +XXX --- need to say more here! |
| 15 | + |
| 16 | +Not all objects can be weakly referenced; those objects which do |
| 17 | +include class instances and dictionaries. Extension types can easily |
| 18 | +be made to support weak references; see section \ref{weakref-extension}, |
| 19 | +``Weak References in Extension Types,'' for more information. |
| 20 | + |
| 21 | + |
| 22 | +\strong{Warning:} |
| 23 | +The weak dictionaries provided in the current implementation and |
| 24 | +described below are subject to change. They are included to solicit |
| 25 | +feedback and usage experience, and may be changed or removed in the |
| 26 | +final version. |
| 27 | + |
| 28 | +\strong{Warning:} |
| 29 | +The desired semantics of weak-reference proxy objects are not |
| 30 | +completely clear; it is very difficult to create proxies which behave |
| 31 | +exactly like the type of the referent. The details of these objects |
| 32 | +are likely to change to some degree before the final release as |
| 33 | +experience reports become available. |
| 34 | + |
| 35 | +Please send specific feeback on this module to Fred Drake at |
| 36 | + |
| 37 | + |
| 38 | + |
| 39 | +\begin{funcdesc}{ref}{object\optional{, callback}} |
| 40 | + Return a weak reference to \var{object}. If \var{callback} is |
| 41 | + provided, it will be called when the object is about to be |
| 42 | + finalized; the weak reference object will be passed as the only |
| 43 | + parameter to the callback; the referent will no longer be available. |
| 44 | + The original object can be retrieved by calling the reference |
| 45 | + object, if the referent is still alive. |
| 46 | + |
| 47 | + It is allowable for many weak references to be constructed for the |
| 48 | + same object. Callbacks registered for each weak reference will be |
| 49 | + called from the most recently registered callback to the oldest |
| 50 | + registered callback. |
| 51 | + |
| 52 | + Exceptions raised by the callback will be noted on the standard |
| 53 | + error output, but cannot be propogated; they are handled in exactly |
| 54 | + the same way as exceptions raised from an object's |
| 55 | + \method{__del__()} method. |
| 56 | +\end{funcdesc} |
| 57 | + |
| 58 | +\begin{funcdesc}{mapping}{\optional{dict}} |
| 59 | + Return a weak dictionary. If \var{dict} is given and not |
| 60 | + \code{None}, the new dictionary will contain the items contained in |
| 61 | + \var{dict}. The values from \var{dict} must be weakly referencable; |
| 62 | + if any values which would be inserted into the new mapping are not |
| 63 | + weakly referencable, \exception{TypeError} will be raised and the |
| 64 | + new mapping will be empty. |
| 65 | +\end{funcdesc} |
| 66 | + |
| 67 | +\begin{funcdesc}{proxy}{object\optional{, callback}} |
| 68 | + Return a proxy to \var{object} which uses a weak reference. This |
| 69 | + supports use of the proxy in most contexts instead of requiring the |
| 70 | + explicit dereferencing used with weak reference objects. The |
| 71 | + returned object will have a type of either \code{ProxyType} or |
| 72 | + \code{CallableProxyType}, depending on whether \var{object} is |
| 73 | + callable. Proxy objects are not hashable regardless of the |
| 74 | + referent; this avoids a number of problems related to their |
| 75 | + fundamentally mutable nature, and prevent their use as dictionary |
| 76 | + keys. \var{callable} is the same as the parameter of the same name |
| 77 | + to the \function{ref()} function. |
| 78 | +\end{funcdesc} |
| 79 | + |
| 80 | +\begin{funcdesc}{getweakrefcount}{object} |
| 81 | + Return the number of weak references and proxies which refer to |
| 82 | + \var{object}. |
| 83 | +\end{funcdesc} |
| 84 | + |
| 85 | +\begin{funcdesc}{getweakrefs}{object} |
| 86 | + Return a list of all weak reference and proxy objects which refer to |
| 87 | + \var{object}. |
| 88 | +\end{funcdesc} |
| 89 | + |
| 90 | +\begin{classdesc}{WeakDictionary}{\optional{dict}} |
| 91 | + The class of the mapping objects returned by \function{mapping()}. |
| 92 | + This can be used for subclassing the implementation if needed. |
| 93 | +\end{classdesc} |
| 94 | + |
| 95 | +\begin{datadesc}{ReferenceType} |
| 96 | + The type object for weak references objects. |
| 97 | +\end{datadesc} |
| 98 | + |
| 99 | +\begin{datadesc}{ProxyType} |
| 100 | + The type object for proxies of objects which are not callable. |
| 101 | +\end{datadesc} |
| 102 | + |
| 103 | +\begin{datadesc}{CallableProxyType} |
| 104 | + The type object for proxies of callable objects. |
| 105 | +\end{datadesc} |
| 106 | + |
| 107 | +\begin{datadesc}{ProxyTypes} |
| 108 | + Sequence containing all the type objects for proxies. This can make |
| 109 | + it simpler to test if an object is a proxy without being dependent |
| 110 | + on naming both proxy types. |
| 111 | +\end{datadesc} |
| 112 | + |
| 113 | + |
| 114 | +\begin{seealso} |
| 115 | + \seepep{0205}{Weak References}{The proposal and rationale for this |
| 116 | + feature, including links to earlier implementations |
| 117 | + and information about similar features in other |
| 118 | + languages.} |
| 119 | +\end{seealso} |
| 120 | + |
| 121 | + |
| 122 | +\subsection{Weak Reference Objects |
| 123 | + \label{weakref-objects}} |
| 124 | + |
| 125 | +Weak reference objects have no attributes or methods, but do allow the |
| 126 | +referent to be obtained, if it still exists, by calling it: |
| 127 | + |
| 128 | +\begin{verbatim} |
| 129 | +>>> import weakref |
| 130 | +>>> class Object: |
| 131 | +... pass |
| 132 | +... |
| 133 | +>>> o = Object() |
| 134 | +>>> r = weakref.ref(o) |
| 135 | +>>> o2 = r() |
| 136 | +>>> o is o2 |
| 137 | +1 |
| 138 | +\end{verbatim} |
| 139 | + |
| 140 | +If the referent no longer exists, calling the reference object returns |
| 141 | +\code{None}: |
| 142 | + |
| 143 | +\begin{verbatim} |
| 144 | +>>> del o, o2 |
| 145 | +>>> print r() |
| 146 | +None |
| 147 | +\end{verbatim} |
| 148 | + |
| 149 | +Testing that a weak reference object is still live should be done |
| 150 | +using the expression \code{\var{ref}.get() is not None}. Normally, |
| 151 | +application code that needs to use a reference object should follow |
| 152 | +this pattern: |
| 153 | + |
| 154 | +\begin{verbatim} |
| 155 | +o = ref.get() |
| 156 | +if o is None: |
| 157 | + # referent has been garbage collected |
| 158 | + print "Object has been allocated; can't frobnicate." |
| 159 | +else: |
| 160 | + print "Object is still live!" |
| 161 | + o.do_something_useful() |
| 162 | +\end{verbatim} |
| 163 | + |
| 164 | +Using a separate test for ``liveness'' creates race conditions in |
| 165 | +threaded applications; another thread can cause a weak reference to |
| 166 | +become invalidated before the \method{get()} method is called; the |
| 167 | +idiom shown above is safe in threaded applications as well as |
| 168 | +single-threaded applications. |
| 169 | + |
| 170 | + |
| 171 | +\subsection{Weak References in Extension Types |
| 172 | + \label{weakref-extension}} |
| 173 | + |
| 174 | +One of the goals of the implementation is to allow any type to |
| 175 | +participate in the weak reference mechanism without incurring the |
| 176 | +overhead on those objects which do not benefit by weak referencing |
| 177 | +(such as numbers). |
| 178 | + |
| 179 | +For an object to be weakly referencable, the extension must include a |
| 180 | +\ctype{PyObject *} field in the instance structure for the use of the |
| 181 | +weak reference mechanism; it will be initialized by Python's functions |
| 182 | +for object creation. It must also set the \code{tp_weaklistoffset} |
| 183 | +field of the corresponding type object to the offset of the field. |
| 184 | +For example, the instance type is defined with the following structure: |
| 185 | + |
| 186 | +\begin{verbatim} |
| 187 | +typedef struct { |
| 188 | + PyObject_HEAD |
| 189 | + PyClassObject *in_class; /* The class object */ |
| 190 | + PyObject *in_dict; /* A dictionary */ |
| 191 | + PyObject *in_weakreflist; /* List of weak references */ |
| 192 | +} PyInstanceObject; |
| 193 | +\end{verbatim} |
| 194 | + |
| 195 | +The statically-declared type object for instances is defined this way: |
| 196 | + |
| 197 | +\begin{verbatim} |
| 198 | +PyTypeObject PyInstance_Type = { |
| 199 | + PyObject_HEAD_INIT(&PyType_Type) |
| 200 | + 0, |
| 201 | + "instance", |
| 202 | +
|
| 203 | + /* lots of stuff omitted for brevity */ |
| 204 | +
|
| 205 | + offsetof(PyInstanceObject, in_weakreflist) /* tp_weaklistoffset */ |
| 206 | +}; |
| 207 | +\end{verbatim} |
| 208 | + |
| 209 | +The only further addition is that the destructor needs to call the |
| 210 | +weak reference manager to clear any weak references and return if the |
| 211 | +object has been resurrected. This needs to occur before any other |
| 212 | +parts of the destruction have occurred: |
| 213 | + |
| 214 | +\begin{verbatim} |
| 215 | +static void |
| 216 | +instance_dealloc(PyInstanceObject *inst) |
| 217 | +{ |
| 218 | + /* allocate tempories if needed, but do not begin |
| 219 | + destruction here |
| 220 | + */ |
| 221 | +
|
| 222 | + if (!PyObject_ClearWeakRefs((PyObject *) inst)) |
| 223 | + return; |
| 224 | +
|
| 225 | + /* proceed with object destuction normally */ |
| 226 | +} |
| 227 | +\end{verbatim} |
0 commit comments