Weak Reference Objects¶
Python supportsweak references as first-class objects. There are twospecific object types which directly implement weak references. The first is asimple reference object, and the second acts as a proxy for the original objectas much as it can.
- intPyWeakref_Check(PyObject*ob)¶
Return non-zero ifob is either a reference or proxy object. This functionalways succeeds.
- intPyWeakref_CheckRef(PyObject*ob)¶
Return non-zero ifob is a reference object. This function always succeeds.
- intPyWeakref_CheckProxy(PyObject*ob)¶
Return non-zero ifob is a proxy object. This function always succeeds.
- PyObject*PyWeakref_NewRef(PyObject*ob,PyObject*callback)¶
- Return value: New reference. Part of theStable ABI.
Return a weak reference object for the objectob. This will always returna new reference, but is not guaranteed to create a new object; an existingreference object may be returned. The second parameter,callback, can be acallable object that receives notification whenob is garbage collected; itshould accept a single parameter, which will be the weak reference objectitself.callback may also be
None
orNULL
. Ifob is not aweakly referenceable object, or ifcallback is not callable,None
, orNULL
, this will returnNULL
and raiseTypeError
.
- PyObject*PyWeakref_NewProxy(PyObject*ob,PyObject*callback)¶
- Return value: New reference. Part of theStable ABI.
Return a weak reference proxy object for the objectob. This will alwaysreturn a new reference, but is not guaranteed to create a new object; anexisting proxy object may be returned. The second parameter,callback, canbe a callable object that receives notification whenob is garbagecollected; it should accept a single parameter, which will be the weakreference object itself.callback may also be
None
orNULL
. Ifobis not a weakly referenceable object, or ifcallback is not callable,None
, orNULL
, this will returnNULL
and raiseTypeError
.
- intPyWeakref_GetRef(PyObject*ref,PyObject**pobj)¶
- Part of theStable ABI since version 3.13.
Get astrong reference to the referenced object from a weakreference,ref, into*pobj.
On success, set*pobj to a newstrong reference to thereferenced object and return 1.
If the reference is dead, set*pobj to
NULL
and return 0.On error, raise an exception and return -1.
Added in version 3.13.
- PyObject*PyWeakref_GetObject(PyObject*ref)¶
- Return value: Borrowed reference. Part of theStable ABI.
Return aborrowed reference to the referenced object from a weakreference,ref. If the referent is no longer live, returns
Py_None
.Note
This function returns aborrowed reference to the referenced object.This means that you should always call
Py_INCREF()
on the objectexcept when it cannot be destroyed before the last usage of the borrowedreference.Deprecated since version 3.13, will be removed in version 3.15:Use
PyWeakref_GetRef()
instead.
- PyObject*PyWeakref_GET_OBJECT(PyObject*ref)¶
- Return value: Borrowed reference.
Similar to
PyWeakref_GetObject()
, but does no error checking.Deprecated since version 3.13, will be removed in version 3.15:Use
PyWeakref_GetRef()
instead.
- voidPyObject_ClearWeakRefs(PyObject*object)¶
- Part of theStable ABI.
This function is called by the
tp_dealloc
handlerto clear weak references.This iterates through the weak references forobject and calls callbacksfor those references which have one. It returns when all callbacks havebeen attempted.
- voidPyUnstable_Object_ClearWeakRefsNoCallbacks(PyObject*object)¶
- This isUnstable API. It may change without warning in minor releases.
Clears the weakrefs forobject without calling the callbacks.
This function is called by the
tp_dealloc
handlerfor types with finalizers (i.e.,__del__()
). The handler forthose objects first callsPyObject_ClearWeakRefs()
to clear weakrefsand call their callbacks, then the finalizer, and finally this function toclear any weakrefs that may have been created by the finalizer.In most circumstances, it’s more appropriate to use
PyObject_ClearWeakRefs()
to clear weakrefs instead of this function.Added in version 3.13.