17db96d56Sopenharmony_ci.. highlight:: c
27db96d56Sopenharmony_ci
37db96d56Sopenharmony_ci.. _weakrefobjects:
47db96d56Sopenharmony_ci
57db96d56Sopenharmony_ciWeak Reference Objects
67db96d56Sopenharmony_ci----------------------
77db96d56Sopenharmony_ci
87db96d56Sopenharmony_ciPython supports *weak references* as first-class objects.  There are two
97db96d56Sopenharmony_cispecific object types which directly implement weak references.  The first is a
107db96d56Sopenharmony_cisimple reference object, and the second acts as a proxy for the original object
117db96d56Sopenharmony_cias much as it can.
127db96d56Sopenharmony_ci
137db96d56Sopenharmony_ci
147db96d56Sopenharmony_ci.. c:function:: int PyWeakref_Check(ob)
157db96d56Sopenharmony_ci
167db96d56Sopenharmony_ci   Return true if *ob* is either a reference or proxy object.  This function
177db96d56Sopenharmony_ci   always succeeds.
187db96d56Sopenharmony_ci
197db96d56Sopenharmony_ci
207db96d56Sopenharmony_ci.. c:function:: int PyWeakref_CheckRef(ob)
217db96d56Sopenharmony_ci
227db96d56Sopenharmony_ci   Return true if *ob* is a reference object.  This function always succeeds.
237db96d56Sopenharmony_ci
247db96d56Sopenharmony_ci
257db96d56Sopenharmony_ci.. c:function:: int PyWeakref_CheckProxy(ob)
267db96d56Sopenharmony_ci
277db96d56Sopenharmony_ci   Return true if *ob* is a proxy object.  This function always succeeds.
287db96d56Sopenharmony_ci
297db96d56Sopenharmony_ci
307db96d56Sopenharmony_ci.. c:function:: PyObject* PyWeakref_NewRef(PyObject *ob, PyObject *callback)
317db96d56Sopenharmony_ci
327db96d56Sopenharmony_ci   Return a weak reference object for the object *ob*.  This will always return
337db96d56Sopenharmony_ci   a new reference, but is not guaranteed to create a new object; an existing
347db96d56Sopenharmony_ci   reference object may be returned.  The second parameter, *callback*, can be a
357db96d56Sopenharmony_ci   callable object that receives notification when *ob* is garbage collected; it
367db96d56Sopenharmony_ci   should accept a single parameter, which will be the weak reference object
377db96d56Sopenharmony_ci   itself. *callback* may also be ``None`` or ``NULL``.  If *ob* is not a
387db96d56Sopenharmony_ci   weakly referencable object, or if *callback* is not callable, ``None``, or
397db96d56Sopenharmony_ci   ``NULL``, this will return ``NULL`` and raise :exc:`TypeError`.
407db96d56Sopenharmony_ci
417db96d56Sopenharmony_ci
427db96d56Sopenharmony_ci.. c:function:: PyObject* PyWeakref_NewProxy(PyObject *ob, PyObject *callback)
437db96d56Sopenharmony_ci
447db96d56Sopenharmony_ci   Return a weak reference proxy object for the object *ob*.  This will always
457db96d56Sopenharmony_ci   return a new reference, but is not guaranteed to create a new object; an
467db96d56Sopenharmony_ci   existing proxy object may be returned.  The second parameter, *callback*, can
477db96d56Sopenharmony_ci   be a callable object that receives notification when *ob* is garbage
487db96d56Sopenharmony_ci   collected; it should accept a single parameter, which will be the weak
497db96d56Sopenharmony_ci   reference object itself. *callback* may also be ``None`` or ``NULL``.  If *ob*
507db96d56Sopenharmony_ci   is not a weakly referencable object, or if *callback* is not callable,
517db96d56Sopenharmony_ci   ``None``, or ``NULL``, this will return ``NULL`` and raise :exc:`TypeError`.
527db96d56Sopenharmony_ci
537db96d56Sopenharmony_ci
547db96d56Sopenharmony_ci.. c:function:: PyObject* PyWeakref_GetObject(PyObject *ref)
557db96d56Sopenharmony_ci
567db96d56Sopenharmony_ci   Return the referenced object from a weak reference, *ref*.  If the referent is
577db96d56Sopenharmony_ci   no longer live, returns :const:`Py_None`.
587db96d56Sopenharmony_ci
597db96d56Sopenharmony_ci   .. note::
607db96d56Sopenharmony_ci
617db96d56Sopenharmony_ci      This function returns a :term:`borrowed reference` to the referenced object.
627db96d56Sopenharmony_ci      This means that you should always call :c:func:`Py_INCREF` on the object
637db96d56Sopenharmony_ci      except when it cannot be destroyed before the last usage of the borrowed
647db96d56Sopenharmony_ci      reference.
657db96d56Sopenharmony_ci
667db96d56Sopenharmony_ci
677db96d56Sopenharmony_ci.. c:function:: PyObject* PyWeakref_GET_OBJECT(PyObject *ref)
687db96d56Sopenharmony_ci
697db96d56Sopenharmony_ci   Similar to :c:func:`PyWeakref_GetObject`, but does no error checking.
707db96d56Sopenharmony_ci
717db96d56Sopenharmony_ci
727db96d56Sopenharmony_ci.. c:function:: void PyObject_ClearWeakRefs(PyObject *object)
737db96d56Sopenharmony_ci
747db96d56Sopenharmony_ci   This function is called by the :c:member:`~PyTypeObject.tp_dealloc` handler
757db96d56Sopenharmony_ci   to clear weak references.
767db96d56Sopenharmony_ci
777db96d56Sopenharmony_ci   This iterates through the weak references for *object* and calls callbacks
787db96d56Sopenharmony_ci   for those references which have one. It returns when all callbacks have
797db96d56Sopenharmony_ci   been attempted.
80