1.. highlight:: 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 14.. c:function:: int PyWeakref_Check(ob) 15 16 Return true if *ob* is either a reference or proxy object. This function 17 always succeeds. 18 19 20.. c:function:: int PyWeakref_CheckRef(ob) 21 22 Return true if *ob* is a reference object. This function always succeeds. 23 24 25.. c:function:: int PyWeakref_CheckProxy(ob) 26 27 Return true if *ob* is a proxy object. This function always succeeds. 28 29 30.. c:function:: PyObject* PyWeakref_NewRef(PyObject *ob, PyObject *callback) 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 37 itself. *callback* may also be ``None`` or ``NULL``. If *ob* is not a 38 weakly-referencable object, or if *callback* is not callable, ``None``, or 39 ``NULL``, this will return ``NULL`` and raise :exc:`TypeError`. 40 41 42.. c:function:: PyObject* PyWeakref_NewProxy(PyObject *ob, PyObject *callback) 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 49 reference object itself. *callback* may also be ``None`` or ``NULL``. If *ob* 50 is not a weakly-referencable object, or if *callback* is not callable, 51 ``None``, or ``NULL``, this will return ``NULL`` and raise :exc:`TypeError`. 52 53 54.. c:function:: PyObject* PyWeakref_GetObject(PyObject *ref) 55 56 Return the referenced object from a weak reference, *ref*. If the referent is 57 no longer live, returns :const:`Py_None`. 58 59 .. note:: 60 61 This function returns a **borrowed reference** to the referenced object. 62 This means that you should always call :c:func:`Py_INCREF` on the object 63 except if you know that it cannot be destroyed while you are still 64 using it. 65 66 67.. c:function:: PyObject* PyWeakref_GET_OBJECT(PyObject *ref) 68 69 Similar to :c:func:`PyWeakref_GetObject`, but implemented as a macro that does no 70 error checking. 71