blob: 98ebe711adaeb46f2011df2502ae3817bad54660 [file] [log] [blame]
Stéphane Wirtelcbb64842019-05-17 11:55:34 +02001.. highlight:: c
Georg Brandl54a3faa2008-01-20 09:30:57 +00002
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
Antonio Cuni315fc522021-01-06 12:38:26 +010016 Return true if *ob* is either a reference or proxy object. This function
17 always succeeds.
Georg Brandl54a3faa2008-01-20 09:30:57 +000018
19
Georg Brandl60203b42010-10-06 10:11:56 +000020.. c:function:: int PyWeakref_CheckRef(ob)
Georg Brandl54a3faa2008-01-20 09:30:57 +000021
Antonio Cuni315fc522021-01-06 12:38:26 +010022 Return true if *ob* is a reference object. This function always succeeds.
Georg Brandl54a3faa2008-01-20 09:30:57 +000023
24
Georg Brandl60203b42010-10-06 10:11:56 +000025.. c:function:: int PyWeakref_CheckProxy(ob)
Georg Brandl54a3faa2008-01-20 09:30:57 +000026
Antonio Cuni315fc522021-01-06 12:38:26 +010027 Return true if *ob* is a proxy object. This function always succeeds.
Georg Brandl54a3faa2008-01-20 09:30:57 +000028
29
Georg Brandl60203b42010-10-06 10:11:56 +000030.. c:function:: PyObject* PyWeakref_NewRef(PyObject *ob, PyObject *callback)
Georg Brandl54a3faa2008-01-20 09:30:57 +000031
32 Return a weak reference object for the object *ob*. This will always return
33 a new reference, but is not guaranteed to create a new object; an existing
34 reference object may be returned. The second parameter, *callback*, can be a
35 callable object that receives notification when *ob* is garbage collected; it
36 should accept a single parameter, which will be the weak reference object
Serhiy Storchaka25fc0882019-10-30 12:03:20 +020037 itself. *callback* may also be ``None`` or ``NULL``. If *ob* is not a
Georg Brandl54a3faa2008-01-20 09:30:57 +000038 weakly-referencable object, or if *callback* is not callable, ``None``, or
Serhiy Storchaka25fc0882019-10-30 12:03:20 +020039 ``NULL``, this will return ``NULL`` and raise :exc:`TypeError`.
Georg Brandl54a3faa2008-01-20 09:30:57 +000040
41
Georg Brandl60203b42010-10-06 10:11:56 +000042.. c:function:: PyObject* PyWeakref_NewProxy(PyObject *ob, PyObject *callback)
Georg Brandl54a3faa2008-01-20 09:30:57 +000043
44 Return a weak reference proxy object for the object *ob*. This will always
45 return a new reference, but is not guaranteed to create a new object; an
46 existing proxy object may be returned. The second parameter, *callback*, can
47 be a callable object that receives notification when *ob* is garbage
48 collected; it should accept a single parameter, which will be the weak
Serhiy Storchaka25fc0882019-10-30 12:03:20 +020049 reference object itself. *callback* may also be ``None`` or ``NULL``. If *ob*
Georg Brandl54a3faa2008-01-20 09:30:57 +000050 is not a weakly-referencable object, or if *callback* is not callable,
Serhiy Storchaka25fc0882019-10-30 12:03:20 +020051 ``None``, or ``NULL``, this will return ``NULL`` and raise :exc:`TypeError`.
Georg Brandl54a3faa2008-01-20 09:30:57 +000052
53
Georg Brandl60203b42010-10-06 10:11:56 +000054.. c:function:: PyObject* PyWeakref_GetObject(PyObject *ref)
Georg Brandl54a3faa2008-01-20 09:30:57 +000055
56 Return the referenced object from a weak reference, *ref*. If the referent is
Georg Brandl502c3eb2010-08-02 17:49:25 +000057 no longer live, returns :const:`Py_None`.
58
Benjamin Peterson2e3a38a2011-05-31 21:27:41 -050059 .. note::
Georg Brandl502c3eb2010-08-02 17:49:25 +000060
Victor Stinner23c5f932020-11-09 13:40:47 +010061 This function returns a :term:`borrowed reference` to the referenced object.
Georg Brandl60203b42010-10-06 10:11:56 +000062 This means that you should always call :c:func:`Py_INCREF` on the object
kj78ba7c62020-11-11 07:56:55 +080063 except when it cannot be destroyed before the last usage of the borrowed
Victor Stinner23c5f932020-11-09 13:40:47 +010064 reference.
Georg Brandl54a3faa2008-01-20 09:30:57 +000065
66
Georg Brandl60203b42010-10-06 10:11:56 +000067.. c:function:: PyObject* PyWeakref_GET_OBJECT(PyObject *ref)
Georg Brandl54a3faa2008-01-20 09:30:57 +000068
Georg Brandl60203b42010-10-06 10:11:56 +000069 Similar to :c:func:`PyWeakref_GetObject`, but implemented as a macro that does no
Georg Brandl54a3faa2008-01-20 09:30:57 +000070 error checking.