blob: 6cb3e33fe843ac9c4a5d0e9e13d2aaa5b0f7ced3 [file] [log] [blame]
Georg Brandl54a3faa2008-01-20 09:30:57 +00001.. highlightlang:: c
2
3.. _weakrefobjects:
4
5Weak Reference Objects
6----------------------
7
8Python supports *weak references* as first-class objects. There are two
9specific object types which directly implement weak references. The first is a
10simple reference object, and the second acts as a proxy for the original object
11as much as it can.
12
13
Georg Brandl60203b42010-10-06 10:11:56 +000014.. c:function:: int PyWeakref_Check(ob)
Georg Brandl54a3faa2008-01-20 09:30:57 +000015
16 Return true if *ob* is either a reference or proxy object.
17
18
Georg Brandl60203b42010-10-06 10:11:56 +000019.. c:function:: int PyWeakref_CheckRef(ob)
Georg Brandl54a3faa2008-01-20 09:30:57 +000020
21 Return true if *ob* is a reference object.
22
23
Georg Brandl60203b42010-10-06 10:11:56 +000024.. c:function:: int PyWeakref_CheckProxy(ob)
Georg Brandl54a3faa2008-01-20 09:30:57 +000025
26 Return true if *ob* is a proxy object.
27
28
Georg Brandl60203b42010-10-06 10:11:56 +000029.. c:function:: PyObject* PyWeakref_NewRef(PyObject *ob, PyObject *callback)
Georg Brandl54a3faa2008-01-20 09:30:57 +000030
31 Return a weak reference object for the object *ob*. This will always return
32 a new reference, but is not guaranteed to create a new object; an existing
33 reference object may be returned. The second parameter, *callback*, can be a
34 callable object that receives notification when *ob* is garbage collected; it
35 should accept a single parameter, which will be the weak reference object
36 itself. *callback* may also be ``None`` or *NULL*. If *ob* is not a
37 weakly-referencable object, or if *callback* is not callable, ``None``, or
38 *NULL*, this will return *NULL* and raise :exc:`TypeError`.
39
40
Georg Brandl60203b42010-10-06 10:11:56 +000041.. c:function:: PyObject* PyWeakref_NewProxy(PyObject *ob, PyObject *callback)
Georg Brandl54a3faa2008-01-20 09:30:57 +000042
43 Return a weak reference proxy object for the object *ob*. This will always
44 return a new reference, but is not guaranteed to create a new object; an
45 existing proxy object may be returned. The second parameter, *callback*, can
46 be a callable object that receives notification when *ob* is garbage
47 collected; it should accept a single parameter, which will be the weak
48 reference object itself. *callback* may also be ``None`` or *NULL*. If *ob*
49 is not a weakly-referencable object, or if *callback* is not callable,
50 ``None``, or *NULL*, this will return *NULL* and raise :exc:`TypeError`.
51
52
Georg Brandl60203b42010-10-06 10:11:56 +000053.. c:function:: PyObject* PyWeakref_GetObject(PyObject *ref)
Georg Brandl54a3faa2008-01-20 09:30:57 +000054
55 Return the referenced object from a weak reference, *ref*. If the referent is
Georg Brandl502c3eb2010-08-02 17:49:25 +000056 no longer live, returns :const:`Py_None`.
57
Benjamin Peterson2e3a38a2011-05-31 21:27:41 -050058 .. note::
Georg Brandl502c3eb2010-08-02 17:49:25 +000059
60 This function returns a **borrowed reference** to the referenced object.
Georg Brandl60203b42010-10-06 10:11:56 +000061 This means that you should always call :c:func:`Py_INCREF` on the object
Georg Brandl502c3eb2010-08-02 17:49:25 +000062 except if you know that it cannot be destroyed while you are still
63 using it.
Georg Brandl54a3faa2008-01-20 09:30:57 +000064
65
Georg Brandl60203b42010-10-06 10:11:56 +000066.. c:function:: PyObject* PyWeakref_GET_OBJECT(PyObject *ref)
Georg Brandl54a3faa2008-01-20 09:30:57 +000067
Georg Brandl60203b42010-10-06 10:11:56 +000068 Similar to :c:func:`PyWeakref_GetObject`, but implemented as a macro that does no
Georg Brandl54a3faa2008-01-20 09:30:57 +000069 error checking.