| LEFT | RIGHT |
| 1 :mod:`weakref` --- Weak references | 1 :mod:`weakref` --- Weak references |
| 2 ================================== | 2 ================================== |
| 3 | 3 |
| 4 .. module:: weakref | 4 .. module:: weakref |
| 5 :synopsis: Support for weak references and weak dictionaries. | 5 :synopsis: Support for weak references and weak dictionaries. |
| 6 .. moduleauthor:: Fred L. Drake, Jr. <fdrake@acm.org> | 6 .. moduleauthor:: Fred L. Drake, Jr. <fdrake@acm.org> |
| 7 .. moduleauthor:: Neil Schemenauer <nas@arctrix.com> | 7 .. moduleauthor:: Neil Schemenauer <nas@arctrix.com> |
| 8 .. moduleauthor:: Martin von Löwis <martin@loewis.home.cs.tu-berlin.de> | 8 .. moduleauthor:: Martin von Löwis <martin@loewis.home.cs.tu-berlin.de> |
| 9 .. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org> | 9 .. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org> |
| 10 | 10 |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 51 :class:`finalize` provides a straight forward way to register a | 51 :class:`finalize` provides a straight forward way to register a |
| 52 cleanup function to be called when an object is garbage collected. | 52 cleanup function to be called when an object is garbage collected. |
| 53 This is simpler to use than setting up a callback function on a raw | 53 This is simpler to use than setting up a callback function on a raw |
| 54 weak reference. | 54 weak reference. |
| 55 | 55 |
| 56 Most programs should find that using one of these weak container types | 56 Most programs should find that using one of these weak container types |
| 57 or :class:`finalize` is all they need -- it's not usually necessary to | 57 or :class:`finalize` is all they need -- it's not usually necessary to |
| 58 create your own weak references directly. The low-level machinery is | 58 create your own weak references directly. The low-level machinery is |
| 59 exposed by the :mod:`weakref` module for the benefit of advanced uses. | 59 exposed by the :mod:`weakref` module for the benefit of advanced uses. |
| 60 | 60 |
| 61 .. note:: | |
| 62 | |
| 63 Weak references to an object are cleared before the object's :meth:`__del__` | |
| 64 is called, to ensure that the weak reference callback (if any) finds the | |
| 65 object still alive. | |
| 66 | |
| 67 Not all objects can be weakly referenced; those objects which can include class | 61 Not all objects can be weakly referenced; those objects which can include class |
| 68 instances, functions written in Python (but not in C), instance methods, sets, | 62 instances, functions written in Python (but not in C), instance methods, sets, |
| 69 frozensets, some :term:`file objects <file object>`, :term:`generator`\s, type | 63 frozensets, some :term:`file objects <file object>`, :term:`generator`\s, type |
| 70 objects, sockets, arrays, deques, regular expression pattern objects, and code | 64 objects, sockets, arrays, deques, regular expression pattern objects, and code |
| 71 objects. | 65 objects. |
| 72 | 66 |
| 73 .. versionchanged:: 3.2 | 67 .. versionchanged:: 3.2 |
| 74 Added support for thread.lock, threading.Lock, and code objects. | 68 Added support for thread.lock, threading.Lock, and code objects. |
| 75 | 69 |
| 76 Several built-in types such as :class:`list` and :class:`dict` do not directly | 70 Several built-in types such as :class:`list` and :class:`dict` do not directly |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 202 | 196 |
| 203 Return an iterable of the weak references to the values. | 197 Return an iterable of the weak references to the values. |
| 204 | 198 |
| 205 | 199 |
| 206 .. class:: WeakSet([elements]) | 200 .. class:: WeakSet([elements]) |
| 207 | 201 |
| 208 Set class that keeps weak references to its elements. An element will be | 202 Set class that keeps weak references to its elements. An element will be |
| 209 discarded when no strong reference to it exists any more. | 203 discarded when no strong reference to it exists any more. |
| 210 | 204 |
| 211 | 205 |
| 212 .. class:: finalize(obj, func, *args, **kwds) | 206 .. class:: finalize(obj, func, *args, **kwargs) |
| 213 | 207 |
| 214 Return a callable finalizer object which will be called when *obj* | 208 Return a callable finalizer object which will be called when *obj* |
| 215 is garbage collected. When the finalizer is called, if it is alive | 209 is garbage collected. When the finalizer is called, if it is alive |
| 216 then it is marked as dead and the result of evaluating ``func(*arg, | 210 then it is marked as dead and the result of evaluating ``func(*arg, |
| 217 **kwds)`` is returned. If it is dead then :const:`None` is | 211 **kwargs)`` is returned. If it is dead then :const:`None` is |
| 218 returned instead. | 212 returned instead. |
| 219 | 213 |
| 220 It is important to ensure that *func*, *args* and *kwds* do not own | 214 It is important to ensure that *func*, *args* and *kwargs* do not own |
| 221 any references to *obj*, either directly or indirectly, since | 215 any references to *obj*, either directly or indirectly, since |
| 222 otherwise *obj* will never be garbage collected. In particular, | 216 otherwise *obj* will never be garbage collected. In particular, |
| 223 *func* must not be a bound method of *obj*. | 217 *func* must not be a bound method of *obj*. |
| 224 | 218 |
| 225 Exceptions raised by finalizer callbacks during garbage collection | 219 Exceptions raised by finalizer callbacks during garbage collection |
| 226 will be shown on the standard error output, but cannot be | 220 will be shown on the standard error output, but cannot be |
| 227 propagated. They are handled in the same way as exceptions raised | 221 propagated. They are handled in the same way as exceptions raised |
| 228 from an object's :meth:`__del__` method or a weak reference's | 222 from an object's :meth:`__del__` method or a weak reference's |
| 229 callback. | 223 callback. |
| 230 | 224 |
| 231 When the program exits, each remaining live finalizer is called | 225 When the program exits, each remaining live finalizer is called |
| 232 unless its :attr:`atexit` attribute has been set to false. They | 226 unless its :attr:`atexit` attribute has been set to false. They |
| 233 are called in reverse order of creation. | 227 are called in reverse order of creation. |
| 234 | 228 |
| 235 A finalizer will never invoke its callback during the later part of | 229 A finalizer will never invoke its callback during the later part of |
| 236 the interpreter shutdown when module globals are liable to have | 230 the interpreter shutdown when module globals are liable to have |
| 237 been replaced by :const:`None`. | 231 been replaced by :const:`None`. |
| 238 | 232 |
| 239 .. method:: __call__() | 233 .. method:: __call__() |
| 240 | 234 |
| 241 If *self* is alive then mark it as dead and return the result of | 235 If *self* is alive then mark it as dead and return the result of |
| 242 calling ``func(*args, **kwds)``. If *self* is dead then return | 236 calling ``func(*args, **kwargs)``. If *self* is dead then return |
| 243 :const:`None`. | 237 :const:`None`. |
| 244 | 238 |
| 245 .. method:: detach() | 239 .. method:: detach() |
| 246 | 240 |
| 247 If *self* is alive then mark it as dead and return the tuple | 241 If *self* is alive then mark it as dead and return the tuple |
| 248 ``(obj, func, args, kwds)``. If *self* is dead then return | 242 ``(obj, func, args, kwargs)``. If *self* is dead then return |
| 249 :const:`None`. | 243 :const:`None`. |
| 250 | 244 |
| 251 .. method:: peek() | 245 .. method:: peek() |
| 252 | 246 |
| 253 If *self* is alive then return the tuple ``(obj, func, args, | 247 If *self* is alive then return the tuple ``(obj, func, args, |
| 254 kwds)``. If *self* is dead then return :const:`None`. | 248 kwargs)``. If *self* is dead then return :const:`None`. |
| 255 | 249 |
| 256 .. attribute:: alive | 250 .. attribute:: alive |
| 257 | 251 |
| 258 Property which is true if the finalizer is alive, false otherwise. | 252 Property which is true if the finalizer is alive, false otherwise. |
| 259 | 253 |
| 260 .. attribute:: atexit | 254 .. attribute:: atexit |
| 261 | 255 |
| 262 A writable boolean property which by default is true. When the | 256 A writable boolean property which by default is true. When the |
| 263 program exits, it calls all remaining live finalizers for which | 257 program exits, it calls all remaining live finalizers for which |
| 264 :attr:`.atexit` is true. They are called in reverse order of | 258 :attr:`.atexit` is true. They are called in reverse order of |
| (...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 437 >>> del obj # callback not called because finalizer dead | 431 >>> del obj # callback not called because finalizer dead |
| 438 | 432 |
| 439 You can unregister a finalizer using its :meth:`~finalize.detach` | 433 You can unregister a finalizer using its :meth:`~finalize.detach` |
| 440 method. This kills the finalizer and returns the arguments passed to | 434 method. This kills the finalizer and returns the arguments passed to |
| 441 the constructor when it was created. | 435 the constructor when it was created. |
| 442 | 436 |
| 443 >>> obj = Object() | 437 >>> obj = Object() |
| 444 >>> f = weakref.finalize(obj, callback, 1, 2, z=3) | 438 >>> f = weakref.finalize(obj, callback, 1, 2, z=3) |
| 445 >>> f.detach() #doctest:+ELLIPSIS | 439 >>> f.detach() #doctest:+ELLIPSIS |
| 446 (<__main__.Object object ...>, <function callback ...>, (1, 2), {'z': 3}) | 440 (<__main__.Object object ...>, <function callback ...>, (1, 2), {'z': 3}) |
| 447 >>> newobj, func, args, kwds = _ | 441 >>> newobj, func, args, kwargs = _ |
| 448 >>> assert not f.alive | 442 >>> assert not f.alive |
| 449 >>> assert newobj is obj | 443 >>> assert newobj is obj |
| 450 >>> assert func(*args, **kwds) == 6 | 444 >>> assert func(*args, **kwargs) == 6 |
| 451 CALLBACK | 445 CALLBACK |
| 452 | 446 |
| 453 Unless you set the :attr:`~finalize.atexit` attribute to | 447 Unless you set the :attr:`~finalize.atexit` attribute to |
| 454 :const:`False`, a finalizer will be called when the program exit if it | 448 :const:`False`, a finalizer will be called when the program exit if it |
| 455 is still alive. For instance | 449 is still alive. For instance |
| 456 | 450 |
| 457 >>> obj = Object() | 451 >>> obj = Object() |
| 458 >>> weakref.finalize(obj, print, "obj dead or exiting") #doctest:+ELLIPSIS | 452 >>> weakref.finalize(obj, print, "obj dead or exiting") #doctest:+ELLIPSIS |
| 459 <finalize object at ...; for 'Object' at ...> | 453 <finalize object at ...; for 'Object' at ...> |
| 460 >>> exit() #doctest:+SKIP | 454 >>> exit() #doctest:+SKIP |
| (...skipping 22 matching lines...) Expand all Loading... |
| 483 def detach(self): | 477 def detach(self): |
| 484 if not self._finalizer.detach(): | 478 if not self._finalizer.detach(): |
| 485 raise IOError("already closed or detached") | 479 raise IOError("already closed or detached") |
| 486 return self._fd | 480 return self._fd |
| 487 | 481 |
| 488 def fileno(self): | 482 def fileno(self): |
| 489 if not self._finalizer.alive: | 483 if not self._finalizer.alive: |
| 490 raise IOError("already closed or detached") | 484 raise IOError("already closed or detached") |
| 491 return self._fd | 485 return self._fd |
| 492 | 486 |
| 493 __index__ = __int__ = fileno | 487 __index__ = __int__ = fileno |
| 494 | 488 |
| 495 @property | 489 @property |
| 496 def closed(self): | 490 def closed(self): |
| 497 return not self._finalizer.alive | 491 return not self._finalizer.alive |
| LEFT | RIGHT |