Fred Drake | ebcf6a8 | 2001-02-01 05:20:20 +0000 | [diff] [blame] | 1 | \section{\module{weakref} --- |
| 2 | Weak references} |
| 3 | |
| 4 | \declaremodule{extension}{weakref} |
Fred Drake | eedf985 | 2001-04-11 19:17:11 +0000 | [diff] [blame] | 5 | \modulesynopsis{Support for weak references and weak dictionaries.} |
Fred Drake | ebcf6a8 | 2001-02-01 05:20:20 +0000 | [diff] [blame] | 6 | \moduleauthor{Fred L. Drake, Jr.}{fdrake@acm.org} |
Martin v. Löwis | 5e16333 | 2001-02-27 18:36:56 +0000 | [diff] [blame] | 7 | \moduleauthor{Neil Schemenauer}{nas@arctrix.com} |
Fred Drake | 0c20904 | 2001-06-29 16:25:07 +0000 | [diff] [blame] | 8 | \moduleauthor{Martin von L\"owis}{martin@loewis.home.cs.tu-berlin.de} |
Fred Drake | ebcf6a8 | 2001-02-01 05:20:20 +0000 | [diff] [blame] | 9 | \sectionauthor{Fred L. Drake, Jr.}{fdrake@acm.org} |
| 10 | |
| 11 | \versionadded{2.1} |
| 12 | |
| 13 | |
| 14 | The \module{weakref} module allows the Python programmer to create |
| 15 | \dfn{weak references} to objects. |
| 16 | |
Tim Peters | 5a5b243 | 2003-11-21 22:20:57 +0000 | [diff] [blame] | 17 | In the following, the term \dfn{referent} means the |
Fred Drake | 7408da5 | 2001-10-26 17:40:22 +0000 | [diff] [blame] | 18 | object which is referred to by a weak reference. |
| 19 | |
Tim Peters | 5a5b243 | 2003-11-21 22:20:57 +0000 | [diff] [blame] | 20 | A weak reference to an object is not enough to keep the object alive: |
| 21 | when the only remaining references to a referent are weak references, |
| 22 | garbage collection is free to destroy the referent and reuse its memory |
| 23 | for something else. A primary use for weak references is to implement |
| 24 | caches or mappings holding large objects, where it's desired that a |
| 25 | large object not be kept alive solely because it appears in a cache or |
| 26 | mapping. For example, if you have a number of large binary image objects, |
| 27 | you may wish to associate a name with each. If you used a Python |
| 28 | dictionary to map names to images, or images to names, the image objects |
| 29 | would remain alive just because they appeared as values or keys in the |
| 30 | dictionaries. The \class{WeakKeyDictionary} and |
| 31 | \class{WeakValueDictionary} classes supplied by the \module{weakref} |
| 32 | module are an alternative, using weak references to construct mappings |
| 33 | that don't keep objects alive solely because they appear in the mapping |
| 34 | objects. If, for example, an image object is a value in a |
| 35 | \class{WeakValueDictionary}, then when the last remaining |
| 36 | references to that image object are the weak references held by weak |
| 37 | mappings, garbage collection can reclaim the object, and its corresponding |
| 38 | entries in weak mappings are simply deleted. |
| 39 | |
| 40 | \class{WeakKeyDictionary} and \class{WeakValueDictionary} use weak |
| 41 | references in their implementation, setting up callback functions on |
| 42 | the weak references that notify the weak dictionaries when a key or value |
| 43 | has been reclaimed by garbage collection. Most programs should find that |
| 44 | using one of these weak dictionary types is all they need -- it's |
| 45 | not usually necessary to create your own weak references directly. The |
| 46 | low-level machinery used by the weak dictionary implementations is exposed |
| 47 | by the \module{weakref} module for the benefit of advanced uses. |
Fred Drake | ebcf6a8 | 2001-02-01 05:20:20 +0000 | [diff] [blame] | 48 | |
Raymond Hettinger | f8020e0 | 2003-07-02 15:10:38 +0000 | [diff] [blame] | 49 | Not all objects can be weakly referenced; those objects which can |
Fred Drake | 5e0dfac | 2001-03-23 04:36:02 +0000 | [diff] [blame] | 50 | include class instances, functions written in Python (but not in C), |
Raymond Hettinger | 027bb63 | 2004-05-31 03:09:25 +0000 | [diff] [blame] | 51 | methods (both bound and unbound), sets, frozensets, file objects, |
Raymond Hettinger | 3480917 | 2004-06-12 06:56:44 +0000 | [diff] [blame^] | 52 | generators, type objects, DBcursor objects from the \module{bsddb} module, |
Raymond Hettinger | 027bb63 | 2004-05-31 03:09:25 +0000 | [diff] [blame] | 53 | sockets, arrays, deques, and regular expression pattern objects. |
| 54 | \versionchanged[Added support for files, sockets, arrays, and patterns]{2.4} |
| 55 | |
| 56 | Several builtin types such as \class{list} and \class{dict} do not |
| 57 | directly support weak references but can add support through subclassing: |
| 58 | |
| 59 | \begin{verbatim} |
| 60 | class Dict(dict): |
| 61 | pass |
| 62 | |
| 63 | obj = Dict(red=1, green=2, blue=3) # this object is weak referencable |
| 64 | \end{verbatim} |
| 65 | |
| 66 | Extension types can easily be made to support weak references; see section |
| 67 | \ref{weakref-extension}, ``Weak References in Extension Types,'' for more |
| 68 | information. |
Fred Drake | ebcf6a8 | 2001-02-01 05:20:20 +0000 | [diff] [blame] | 69 | |
Fred Drake | ebcf6a8 | 2001-02-01 05:20:20 +0000 | [diff] [blame] | 70 | |
| 71 | \begin{funcdesc}{ref}{object\optional{, callback}} |
Fred Drake | 7408da5 | 2001-10-26 17:40:22 +0000 | [diff] [blame] | 72 | Return a weak reference to \var{object}. The original object can be |
| 73 | retrieved by calling the reference object if the referent is still |
| 74 | alive; if the referent is no longer alive, calling the reference |
Fred Drake | 21ae4f9 | 2004-02-03 20:55:15 +0000 | [diff] [blame] | 75 | object will cause \constant{None} to be returned. If \var{callback} is |
Fred Drake | 4458ece | 2004-02-03 19:44:26 +0000 | [diff] [blame] | 76 | provided and not \constant{None}, |
| 77 | it will be called when the object is about to be |
Fred Drake | ebcf6a8 | 2001-02-01 05:20:20 +0000 | [diff] [blame] | 78 | finalized; the weak reference object will be passed as the only |
| 79 | parameter to the callback; the referent will no longer be available. |
Fred Drake | ebcf6a8 | 2001-02-01 05:20:20 +0000 | [diff] [blame] | 80 | |
| 81 | It is allowable for many weak references to be constructed for the |
| 82 | same object. Callbacks registered for each weak reference will be |
| 83 | called from the most recently registered callback to the oldest |
| 84 | registered callback. |
| 85 | |
| 86 | Exceptions raised by the callback will be noted on the standard |
Andrew M. Kuchling | e7d7e6c | 2001-02-14 02:39:11 +0000 | [diff] [blame] | 87 | error output, but cannot be propagated; they are handled in exactly |
Fred Drake | ebcf6a8 | 2001-02-01 05:20:20 +0000 | [diff] [blame] | 88 | the same way as exceptions raised from an object's |
| 89 | \method{__del__()} method. |
Tim Peters | 5a5b243 | 2003-11-21 22:20:57 +0000 | [diff] [blame] | 90 | |
Martin v. Löwis | 5e16333 | 2001-02-27 18:36:56 +0000 | [diff] [blame] | 91 | Weak references are hashable if the \var{object} is hashable. They |
| 92 | will maintain their hash value even after the \var{object} was |
| 93 | deleted. If \function{hash()} is called the first time only after |
| 94 | the \var{object} was deleted, the call will raise |
| 95 | \exception{TypeError}. |
Tim Peters | 5a5b243 | 2003-11-21 22:20:57 +0000 | [diff] [blame] | 96 | |
Fred Drake | 3a2c462 | 2001-10-26 03:00:39 +0000 | [diff] [blame] | 97 | Weak references support tests for equality, but not ordering. If |
Fred Drake | 7408da5 | 2001-10-26 17:40:22 +0000 | [diff] [blame] | 98 | the referents are still alive, two references have the same |
Fred Drake | b03d0cc | 2001-11-26 21:39:40 +0000 | [diff] [blame] | 99 | equality relationship as their referents (regardless of the |
Fred Drake | 7408da5 | 2001-10-26 17:40:22 +0000 | [diff] [blame] | 100 | \var{callback}). If either referent has been deleted, the |
| 101 | references are equal only if the reference objects are the same |
| 102 | object. |
Fred Drake | ebcf6a8 | 2001-02-01 05:20:20 +0000 | [diff] [blame] | 103 | \end{funcdesc} |
| 104 | |
Fred Drake | ebcf6a8 | 2001-02-01 05:20:20 +0000 | [diff] [blame] | 105 | \begin{funcdesc}{proxy}{object\optional{, callback}} |
| 106 | Return a proxy to \var{object} which uses a weak reference. This |
| 107 | supports use of the proxy in most contexts instead of requiring the |
| 108 | explicit dereferencing used with weak reference objects. The |
| 109 | returned object will have a type of either \code{ProxyType} or |
| 110 | \code{CallableProxyType}, depending on whether \var{object} is |
| 111 | callable. Proxy objects are not hashable regardless of the |
| 112 | referent; this avoids a number of problems related to their |
| 113 | fundamentally mutable nature, and prevent their use as dictionary |
Fred Drake | e7ec1ef | 2001-05-10 17:22:17 +0000 | [diff] [blame] | 114 | keys. \var{callback} is the same as the parameter of the same name |
Fred Drake | ebcf6a8 | 2001-02-01 05:20:20 +0000 | [diff] [blame] | 115 | to the \function{ref()} function. |
| 116 | \end{funcdesc} |
| 117 | |
| 118 | \begin{funcdesc}{getweakrefcount}{object} |
| 119 | Return the number of weak references and proxies which refer to |
| 120 | \var{object}. |
| 121 | \end{funcdesc} |
| 122 | |
| 123 | \begin{funcdesc}{getweakrefs}{object} |
| 124 | Return a list of all weak reference and proxy objects which refer to |
| 125 | \var{object}. |
| 126 | \end{funcdesc} |
| 127 | |
Martin v. Löwis | 5e16333 | 2001-02-27 18:36:56 +0000 | [diff] [blame] | 128 | \begin{classdesc}{WeakKeyDictionary}{\optional{dict}} |
Fred Drake | ac154a1 | 2001-04-10 19:57:58 +0000 | [diff] [blame] | 129 | Mapping class that references keys weakly. Entries in the |
| 130 | dictionary will be discarded when there is no longer a strong |
| 131 | reference to the key. This can be used to associate additional data |
| 132 | with an object owned by other parts of an application without adding |
| 133 | attributes to those objects. This can be especially useful with |
| 134 | objects that override attribute accesses. |
Tim Peters | 5a5b243 | 2003-11-21 22:20:57 +0000 | [diff] [blame] | 135 | |
| 136 | \note{Caution: Because a \class{WeakKeyDictionary} is built on top |
| 137 | of a Python dictionary, it must not change size when iterating |
| 138 | over it. This can be difficult to ensure for a |
| 139 | \class{WeakKeyDictionary} because actions performed by the |
| 140 | program during iteration may cause items in the dictionary |
| 141 | to vanish "by magic" (as a side effect of garbage collection).} |
Martin v. Löwis | 5e16333 | 2001-02-27 18:36:56 +0000 | [diff] [blame] | 142 | \end{classdesc} |
| 143 | |
| 144 | \begin{classdesc}{WeakValueDictionary}{\optional{dict}} |
Fred Drake | ac154a1 | 2001-04-10 19:57:58 +0000 | [diff] [blame] | 145 | Mapping class that references values weakly. Entries in the |
| 146 | dictionary will be discarded when no strong reference to the value |
Fred Drake | 7408da5 | 2001-10-26 17:40:22 +0000 | [diff] [blame] | 147 | exists any more. |
Tim Peters | 5a5b243 | 2003-11-21 22:20:57 +0000 | [diff] [blame] | 148 | |
| 149 | \note{Caution: Because a \class{WeakValueDictionary} is built on top |
| 150 | of a Python dictionary, it must not change size when iterating |
| 151 | over it. This can be difficult to ensure for a |
| 152 | \class{WeakValueDictionary} because actions performed by the |
| 153 | program during iteration may cause items in the dictionary |
| 154 | to vanish "by magic" (as a side effect of garbage collection).} |
Fred Drake | ebcf6a8 | 2001-02-01 05:20:20 +0000 | [diff] [blame] | 155 | \end{classdesc} |
| 156 | |
| 157 | \begin{datadesc}{ReferenceType} |
| 158 | The type object for weak references objects. |
| 159 | \end{datadesc} |
| 160 | |
| 161 | \begin{datadesc}{ProxyType} |
| 162 | The type object for proxies of objects which are not callable. |
| 163 | \end{datadesc} |
| 164 | |
| 165 | \begin{datadesc}{CallableProxyType} |
| 166 | The type object for proxies of callable objects. |
| 167 | \end{datadesc} |
| 168 | |
| 169 | \begin{datadesc}{ProxyTypes} |
| 170 | Sequence containing all the type objects for proxies. This can make |
| 171 | it simpler to test if an object is a proxy without being dependent |
| 172 | on naming both proxy types. |
| 173 | \end{datadesc} |
| 174 | |
Fred Drake | ac154a1 | 2001-04-10 19:57:58 +0000 | [diff] [blame] | 175 | \begin{excdesc}{ReferenceError} |
| 176 | Exception raised when a proxy object is used but the underlying |
Fred Drake | 8c2c3d3 | 2001-10-06 06:10:54 +0000 | [diff] [blame] | 177 | object has been collected. This is the same as the standard |
| 178 | \exception{ReferenceError} exception. |
Fred Drake | ac154a1 | 2001-04-10 19:57:58 +0000 | [diff] [blame] | 179 | \end{excdesc} |
| 180 | |
Fred Drake | ebcf6a8 | 2001-02-01 05:20:20 +0000 | [diff] [blame] | 181 | |
| 182 | \begin{seealso} |
| 183 | \seepep{0205}{Weak References}{The proposal and rationale for this |
| 184 | feature, including links to earlier implementations |
| 185 | and information about similar features in other |
| 186 | languages.} |
| 187 | \end{seealso} |
| 188 | |
| 189 | |
| 190 | \subsection{Weak Reference Objects |
| 191 | \label{weakref-objects}} |
| 192 | |
| 193 | Weak reference objects have no attributes or methods, but do allow the |
| 194 | referent to be obtained, if it still exists, by calling it: |
| 195 | |
| 196 | \begin{verbatim} |
| 197 | >>> import weakref |
| 198 | >>> class Object: |
| 199 | ... pass |
| 200 | ... |
| 201 | >>> o = Object() |
| 202 | >>> r = weakref.ref(o) |
| 203 | >>> o2 = r() |
| 204 | >>> o is o2 |
Martin v. Löwis | ccabed3 | 2003-11-27 19:48:03 +0000 | [diff] [blame] | 205 | True |
Fred Drake | ebcf6a8 | 2001-02-01 05:20:20 +0000 | [diff] [blame] | 206 | \end{verbatim} |
| 207 | |
| 208 | If the referent no longer exists, calling the reference object returns |
Fred Drake | 21ae4f9 | 2004-02-03 20:55:15 +0000 | [diff] [blame] | 209 | \constant{None}: |
Fred Drake | ebcf6a8 | 2001-02-01 05:20:20 +0000 | [diff] [blame] | 210 | |
| 211 | \begin{verbatim} |
| 212 | >>> del o, o2 |
| 213 | >>> print r() |
| 214 | None |
| 215 | \end{verbatim} |
| 216 | |
| 217 | Testing that a weak reference object is still live should be done |
Fred Drake | 5d54879 | 2001-08-03 03:50:28 +0000 | [diff] [blame] | 218 | using the expression \code{\var{ref}() is not None}. Normally, |
Fred Drake | ebcf6a8 | 2001-02-01 05:20:20 +0000 | [diff] [blame] | 219 | application code that needs to use a reference object should follow |
| 220 | this pattern: |
| 221 | |
| 222 | \begin{verbatim} |
Fred Drake | 7408da5 | 2001-10-26 17:40:22 +0000 | [diff] [blame] | 223 | # r is a weak reference object |
| 224 | o = r() |
Fred Drake | ebcf6a8 | 2001-02-01 05:20:20 +0000 | [diff] [blame] | 225 | if o is None: |
| 226 | # referent has been garbage collected |
| 227 | print "Object has been allocated; can't frobnicate." |
| 228 | else: |
| 229 | print "Object is still live!" |
| 230 | o.do_something_useful() |
| 231 | \end{verbatim} |
| 232 | |
| 233 | Using a separate test for ``liveness'' creates race conditions in |
| 234 | threaded applications; another thread can cause a weak reference to |
Fred Drake | 7408da5 | 2001-10-26 17:40:22 +0000 | [diff] [blame] | 235 | become invalidated before the weak reference is called; the |
Fred Drake | ebcf6a8 | 2001-02-01 05:20:20 +0000 | [diff] [blame] | 236 | idiom shown above is safe in threaded applications as well as |
| 237 | single-threaded applications. |
| 238 | |
| 239 | |
Fred Drake | cb83988 | 2001-03-28 21:15:41 +0000 | [diff] [blame] | 240 | \subsection{Example \label{weakref-example}} |
| 241 | |
| 242 | This simple example shows how an application can use objects IDs to |
| 243 | retrieve objects that it has seen before. The IDs of the objects can |
| 244 | then be used in other data structures without forcing the objects to |
| 245 | remain alive, but the objects can still be retrieved by ID if they |
| 246 | do. |
| 247 | |
Fred Drake | 4271310 | 2003-12-30 16:15:35 +0000 | [diff] [blame] | 248 | % Example contributed by Tim Peters. |
Fred Drake | cb83988 | 2001-03-28 21:15:41 +0000 | [diff] [blame] | 249 | \begin{verbatim} |
| 250 | import weakref |
| 251 | |
Fred Drake | ac154a1 | 2001-04-10 19:57:58 +0000 | [diff] [blame] | 252 | _id2obj_dict = weakref.WeakValueDictionary() |
Fred Drake | cb83988 | 2001-03-28 21:15:41 +0000 | [diff] [blame] | 253 | |
| 254 | def remember(obj): |
Fred Drake | 7408da5 | 2001-10-26 17:40:22 +0000 | [diff] [blame] | 255 | oid = id(obj) |
| 256 | _id2obj_dict[oid] = obj |
| 257 | return oid |
Fred Drake | cb83988 | 2001-03-28 21:15:41 +0000 | [diff] [blame] | 258 | |
Fred Drake | 7408da5 | 2001-10-26 17:40:22 +0000 | [diff] [blame] | 259 | def id2obj(oid): |
| 260 | return _id2obj_dict[oid] |
Fred Drake | cb83988 | 2001-03-28 21:15:41 +0000 | [diff] [blame] | 261 | \end{verbatim} |
| 262 | |
| 263 | |
Fred Drake | ebcf6a8 | 2001-02-01 05:20:20 +0000 | [diff] [blame] | 264 | \subsection{Weak References in Extension Types |
| 265 | \label{weakref-extension}} |
| 266 | |
| 267 | One of the goals of the implementation is to allow any type to |
| 268 | participate in the weak reference mechanism without incurring the |
| 269 | overhead on those objects which do not benefit by weak referencing |
| 270 | (such as numbers). |
| 271 | |
| 272 | For an object to be weakly referencable, the extension must include a |
Fred Drake | 7408da5 | 2001-10-26 17:40:22 +0000 | [diff] [blame] | 273 | \ctype{PyObject*} field in the instance structure for the use of the |
Fred Drake | 5e0dfac | 2001-03-23 04:36:02 +0000 | [diff] [blame] | 274 | weak reference mechanism; it must be initialized to \NULL{} by the |
| 275 | object's constructor. It must also set the \member{tp_weaklistoffset} |
Fred Drake | ebcf6a8 | 2001-02-01 05:20:20 +0000 | [diff] [blame] | 276 | field of the corresponding type object to the offset of the field. |
Raymond Hettinger | 22c001b | 2002-08-07 16:18:54 +0000 | [diff] [blame] | 277 | Also, it needs to add \constant{Py_TPFLAGS_HAVE_WEAKREFS} to the |
| 278 | tp_flags slot. For example, the instance type is defined with the |
| 279 | following structure: |
Fred Drake | ebcf6a8 | 2001-02-01 05:20:20 +0000 | [diff] [blame] | 280 | |
| 281 | \begin{verbatim} |
| 282 | typedef struct { |
| 283 | PyObject_HEAD |
| 284 | PyClassObject *in_class; /* The class object */ |
Martin v. Löwis | 5e16333 | 2001-02-27 18:36:56 +0000 | [diff] [blame] | 285 | PyObject *in_dict; /* A dictionary */ |
| 286 | PyObject *in_weakreflist; /* List of weak references */ |
Fred Drake | ebcf6a8 | 2001-02-01 05:20:20 +0000 | [diff] [blame] | 287 | } PyInstanceObject; |
| 288 | \end{verbatim} |
| 289 | |
| 290 | The statically-declared type object for instances is defined this way: |
| 291 | |
| 292 | \begin{verbatim} |
| 293 | PyTypeObject PyInstance_Type = { |
| 294 | PyObject_HEAD_INIT(&PyType_Type) |
| 295 | 0, |
Guido van Rossum | 1464839 | 2001-12-08 18:02:58 +0000 | [diff] [blame] | 296 | "module.instance", |
Fred Drake | ebcf6a8 | 2001-02-01 05:20:20 +0000 | [diff] [blame] | 297 | |
Fred Drake | f66cb5d | 2001-06-22 17:20:29 +0000 | [diff] [blame] | 298 | /* Lots of stuff omitted for brevity... */ |
Fred Drake | ebcf6a8 | 2001-02-01 05:20:20 +0000 | [diff] [blame] | 299 | |
Raymond Hettinger | 22c001b | 2002-08-07 16:18:54 +0000 | [diff] [blame] | 300 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_WEAKREFS /* tp_flags */ |
| 301 | 0, /* tp_doc */ |
| 302 | 0, /* tp_traverse */ |
| 303 | 0, /* tp_clear */ |
| 304 | 0, /* tp_richcompare */ |
| 305 | offsetof(PyInstanceObject, in_weakreflist), /* tp_weaklistoffset */ |
Fred Drake | ebcf6a8 | 2001-02-01 05:20:20 +0000 | [diff] [blame] | 306 | }; |
| 307 | \end{verbatim} |
| 308 | |
Raymond Hettinger | 22c001b | 2002-08-07 16:18:54 +0000 | [diff] [blame] | 309 | The type constructor is responsible for initializing the weak reference |
| 310 | list to \NULL: |
| 311 | |
| 312 | \begin{verbatim} |
Tim Peters | 5a5b243 | 2003-11-21 22:20:57 +0000 | [diff] [blame] | 313 | static PyObject * |
| 314 | instance_new() { |
| 315 | /* Other initialization stuff omitted for brevity */ |
Raymond Hettinger | 22c001b | 2002-08-07 16:18:54 +0000 | [diff] [blame] | 316 | |
Tim Peters | 5a5b243 | 2003-11-21 22:20:57 +0000 | [diff] [blame] | 317 | self->in_weakreflist = NULL; |
Raymond Hettinger | 22c001b | 2002-08-07 16:18:54 +0000 | [diff] [blame] | 318 | |
Tim Peters | 5a5b243 | 2003-11-21 22:20:57 +0000 | [diff] [blame] | 319 | return (PyObject *) self; |
| 320 | } |
Raymond Hettinger | 22c001b | 2002-08-07 16:18:54 +0000 | [diff] [blame] | 321 | \end{verbatim} |
| 322 | |
Fred Drake | ebcf6a8 | 2001-02-01 05:20:20 +0000 | [diff] [blame] | 323 | The only further addition is that the destructor needs to call the |
Fred Drake | f66cb5d | 2001-06-22 17:20:29 +0000 | [diff] [blame] | 324 | weak reference manager to clear any weak references. This should be |
Fred Drake | 7408da5 | 2001-10-26 17:40:22 +0000 | [diff] [blame] | 325 | done before any other parts of the destruction have occurred, but is |
| 326 | only required if the weak reference list is non-\NULL: |
Fred Drake | ebcf6a8 | 2001-02-01 05:20:20 +0000 | [diff] [blame] | 327 | |
| 328 | \begin{verbatim} |
| 329 | static void |
| 330 | instance_dealloc(PyInstanceObject *inst) |
| 331 | { |
Fred Drake | 7408da5 | 2001-10-26 17:40:22 +0000 | [diff] [blame] | 332 | /* Allocate temporaries if needed, but do not begin |
Fred Drake | f66cb5d | 2001-06-22 17:20:29 +0000 | [diff] [blame] | 333 | destruction just yet. |
Fred Drake | ebcf6a8 | 2001-02-01 05:20:20 +0000 | [diff] [blame] | 334 | */ |
| 335 | |
Fred Drake | 7408da5 | 2001-10-26 17:40:22 +0000 | [diff] [blame] | 336 | if (inst->in_weakreflist != NULL) |
| 337 | PyObject_ClearWeakRefs((PyObject *) inst); |
Fred Drake | ebcf6a8 | 2001-02-01 05:20:20 +0000 | [diff] [blame] | 338 | |
Fred Drake | de3d060 | 2001-10-26 11:27:54 +0000 | [diff] [blame] | 339 | /* Proceed with object destruction normally. */ |
Fred Drake | ebcf6a8 | 2001-02-01 05:20:20 +0000 | [diff] [blame] | 340 | } |
| 341 | \end{verbatim} |