1 \section{\module{weakref
} ---
4 \declaremodule{extension
}{weakref
}
5 \modulesynopsis{Support for weak references and weak dictionaries.
}
6 \moduleauthor{Fred L. Drake, Jr.
}{fdrake@acm.org
}
7 \moduleauthor{Neil Schemenauer
}{nas@arctrix.com
}
8 \moduleauthor{Martin von L\"owis
}{martin@loewis.home.cs.tu-berlin.de
}
9 \sectionauthor{Fred L. Drake, Jr.
}{fdrake@acm.org
}
14 The
\module{weakref
} module allows the Python programmer to create
15 \dfn{weak references
} to objects.
17 In the discussion which follows, the term
\dfn{referent
} means the
18 object which is referred to by a weak reference.
20 XXX --- need to say more here!
22 Not all objects can be weakly referenced; those objects which do
23 include class instances, functions written in Python (but not in C),
24 and methods (both bound and unbound). Extension types can easily
25 be made to support weak references; see section
\ref{weakref-extension
},
26 ``Weak References in Extension Types,'' for more information.
29 \begin{funcdesc
}{ref
}{object
\optional{, callback
}}
30 Return a weak reference to
\var{object
}. The original object can be
31 retrieved by calling the reference object if the referent is still
32 alive; if the referent is no longer alive, calling the reference
33 object will cause
\code{None
} to be returned. If
\var{callback
} is
34 provided, it will be called when the object is about to be
35 finalized; the weak reference object will be passed as the only
36 parameter to the callback; the referent will no longer be available.
38 It is allowable for many weak references to be constructed for the
39 same object. Callbacks registered for each weak reference will be
40 called from the most recently registered callback to the oldest
43 Exceptions raised by the callback will be noted on the standard
44 error output, but cannot be propagated; they are handled in exactly
45 the same way as exceptions raised from an object's
46 \method{__del__()
} method.
48 Weak references are hashable if the
\var{object
} is hashable. They
49 will maintain their hash value even after the
\var{object
} was
50 deleted. If
\function{hash()
} is called the first time only after
51 the
\var{object
} was deleted, the call will raise
52 \exception{TypeError
}.
54 Weak references support tests for equality, but not ordering. If
55 the referents are still alive, two references have the same
56 equality relationship as their referents (regardless of the
57 \var{callback
}). If either referent has been deleted, the
58 references are equal only if the reference objects are the same
62 \begin{funcdesc
}{proxy
}{object
\optional{, callback
}}
63 Return a proxy to
\var{object
} which uses a weak reference. This
64 supports use of the proxy in most contexts instead of requiring the
65 explicit dereferencing used with weak reference objects. The
66 returned object will have a type of either
\code{ProxyType
} or
67 \code{CallableProxyType
}, depending on whether
\var{object
} is
68 callable. Proxy objects are not hashable regardless of the
69 referent; this avoids a number of problems related to their
70 fundamentally mutable nature, and prevent their use as dictionary
71 keys.
\var{callback
} is the same as the parameter of the same name
72 to the
\function{ref()
} function.
75 \begin{funcdesc
}{getweakrefcount
}{object
}
76 Return the number of weak references and proxies which refer to
80 \begin{funcdesc
}{getweakrefs
}{object
}
81 Return a list of all weak reference and proxy objects which refer to
85 \begin{classdesc
}{WeakKeyDictionary
}{\optional{dict
}}
86 Mapping class that references keys weakly. Entries in the
87 dictionary will be discarded when there is no longer a strong
88 reference to the key. This can be used to associate additional data
89 with an object owned by other parts of an application without adding
90 attributes to those objects. This can be especially useful with
91 objects that override attribute accesses.
94 \begin{classdesc
}{WeakValueDictionary
}{\optional{dict
}}
95 Mapping class that references values weakly. Entries in the
96 dictionary will be discarded when no strong reference to the value
100 \begin{datadesc
}{ReferenceType
}
101 The type object for weak references objects.
104 \begin{datadesc
}{ProxyType
}
105 The type object for proxies of objects which are not callable.
108 \begin{datadesc
}{CallableProxyType
}
109 The type object for proxies of callable objects.
112 \begin{datadesc
}{ProxyTypes
}
113 Sequence containing all the type objects for proxies. This can make
114 it simpler to test if an object is a proxy without being dependent
115 on naming both proxy types.
118 \begin{excdesc
}{ReferenceError
}
119 Exception raised when a proxy object is used but the underlying
120 object has been collected. This is the same as the standard
121 \exception{ReferenceError
} exception.
126 \seepep{0205}{Weak References
}{The proposal and rationale for this
127 feature, including links to earlier implementations
128 and information about similar features in other
133 \subsection{Weak Reference Objects
134 \label{weakref-objects
}}
136 Weak reference objects have no attributes or methods, but do allow the
137 referent to be obtained, if it still exists, by calling it:
145 >>> r = weakref.ref(o)
151 If the referent no longer exists, calling the reference object returns
160 Testing that a weak reference object is still live should be done
161 using the expression
\code{\var{ref
}() is not None
}. Normally,
162 application code that needs to use a reference object should follow
166 # r is a weak reference object
169 # referent has been garbage collected
170 print "Object has been allocated; can't frobnicate."
172 print "Object is still live!"
173 o.do_something_useful()
176 Using a separate test for ``liveness'' creates race conditions in
177 threaded applications; another thread can cause a weak reference to
178 become invalidated before the weak reference is called; the
179 idiom shown above is safe in threaded applications as well as
180 single-threaded applications.
183 \subsection{Example
\label{weakref-example
}}
185 This simple example shows how an application can use objects IDs to
186 retrieve objects that it has seen before. The IDs of the objects can
187 then be used in other data structures without forcing the objects to
188 remain alive, but the objects can still be retrieved by ID if they
191 % Example contributed by Tim Peters <tim_one@msn.com>.
195 _id2obj_dict = weakref.WeakValueDictionary()
199 _id2obj_dict
[oid
] = obj
203 return _id2obj_dict
[oid
]
207 \subsection{Weak References in Extension Types
208 \label{weakref-extension
}}
210 One of the goals of the implementation is to allow any type to
211 participate in the weak reference mechanism without incurring the
212 overhead on those objects which do not benefit by weak referencing
215 For an object to be weakly referencable, the extension must include a
216 \ctype{PyObject*
} field in the instance structure for the use of the
217 weak reference mechanism; it must be initialized to
\NULL{} by the
218 object's constructor. It must also set the
\member{tp_weaklistoffset
}
219 field of the corresponding type object to the offset of the field.
220 For example, the instance type is defined with the following structure:
225 PyClassObject *in_class; /* The class object */
226 PyObject *in_dict; /* A dictionary */
227 PyObject *in_weakreflist; /* List of weak references */
231 The statically-declared type object for instances is defined this way:
234 PyTypeObject PyInstance_Type =
{
235 PyObject_HEAD_INIT(&PyType_Type)
239 /* Lots of stuff omitted for brevity... */
241 offsetof(PyInstanceObject, in_weakreflist) /* tp_weaklistoffset */
245 The only further addition is that the destructor needs to call the
246 weak reference manager to clear any weak references. This should be
247 done before any other parts of the destruction have occurred, but is
248 only required if the weak reference list is non-
\NULL:
252 instance_dealloc(PyInstanceObject *inst)
254 /* Allocate temporaries if needed, but do not begin
255 destruction just yet.
258 if (inst->in_weakreflist != NULL)
259 PyObject_ClearWeakRefs((PyObject *) inst);
261 /* Proceed with object destruction normally. */