Stéphane Wirtel | cbb6484 | 2019-05-17 11:55:34 +0200 | [diff] [blame] | 1 | .. highlight:: c |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 2 | |
| 3 | .. _weakrefobjects: |
| 4 | |
| 5 | Weak Reference Objects |
| 6 | ---------------------- |
| 7 | |
| 8 | Python supports *weak references* as first-class objects. There are two |
| 9 | specific object types which directly implement weak references. The first is a |
| 10 | simple reference object, and the second acts as a proxy for the original object |
| 11 | as much as it can. |
| 12 | |
| 13 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 14 | .. c:function:: int PyWeakref_Check(ob) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 15 | |
Antonio Cuni | 315fc52 | 2021-01-06 12:38:26 +0100 | [diff] [blame] | 16 | Return true if *ob* is either a reference or proxy object. This function |
| 17 | always succeeds. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 18 | |
| 19 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 20 | .. c:function:: int PyWeakref_CheckRef(ob) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 21 | |
Antonio Cuni | 315fc52 | 2021-01-06 12:38:26 +0100 | [diff] [blame] | 22 | Return true if *ob* is a reference object. This function always succeeds. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 23 | |
| 24 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 25 | .. c:function:: int PyWeakref_CheckProxy(ob) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 26 | |
Antonio Cuni | 315fc52 | 2021-01-06 12:38:26 +0100 | [diff] [blame] | 27 | Return true if *ob* is a proxy object. This function always succeeds. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 28 | |
| 29 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 30 | .. c:function:: PyObject* PyWeakref_NewRef(PyObject *ob, PyObject *callback) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 31 | |
| 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 Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 37 | itself. *callback* may also be ``None`` or ``NULL``. If *ob* is not a |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 38 | weakly-referencable object, or if *callback* is not callable, ``None``, or |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 39 | ``NULL``, this will return ``NULL`` and raise :exc:`TypeError`. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 40 | |
| 41 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 42 | .. c:function:: PyObject* PyWeakref_NewProxy(PyObject *ob, PyObject *callback) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 43 | |
| 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 Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 49 | reference object itself. *callback* may also be ``None`` or ``NULL``. If *ob* |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 50 | is not a weakly-referencable object, or if *callback* is not callable, |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 51 | ``None``, or ``NULL``, this will return ``NULL`` and raise :exc:`TypeError`. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 52 | |
| 53 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 54 | .. c:function:: PyObject* PyWeakref_GetObject(PyObject *ref) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 55 | |
| 56 | Return the referenced object from a weak reference, *ref*. If the referent is |
Georg Brandl | 502c3eb | 2010-08-02 17:49:25 +0000 | [diff] [blame] | 57 | no longer live, returns :const:`Py_None`. |
| 58 | |
Benjamin Peterson | 2e3a38a | 2011-05-31 21:27:41 -0500 | [diff] [blame] | 59 | .. note:: |
Georg Brandl | 502c3eb | 2010-08-02 17:49:25 +0000 | [diff] [blame] | 60 | |
Victor Stinner | 23c5f93 | 2020-11-09 13:40:47 +0100 | [diff] [blame] | 61 | This function returns a :term:`borrowed reference` to the referenced object. |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 62 | This means that you should always call :c:func:`Py_INCREF` on the object |
kj | 78ba7c6 | 2020-11-11 07:56:55 +0800 | [diff] [blame] | 63 | except when it cannot be destroyed before the last usage of the borrowed |
Victor Stinner | 23c5f93 | 2020-11-09 13:40:47 +0100 | [diff] [blame] | 64 | reference. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 65 | |
| 66 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 67 | .. c:function:: PyObject* PyWeakref_GET_OBJECT(PyObject *ref) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 68 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 69 | Similar to :c:func:`PyWeakref_GetObject`, but implemented as a macro that does no |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 70 | error checking. |