弱引用对象

Python supports weak references as first-class objects. There are two specific object types which directly implement weak references. The first is a simple reference object, and the second acts as a proxy for the original object as much as it can.

int PyWeakref_Check ( PyObject * ob )

Return non-zero if ob is either a reference or proxy object. This function always succeeds.

int PyWeakref_CheckRef ( PyObject * ob )

Return non-zero if ob is a reference object. This function always succeeds.

int PyWeakref_CheckProxy ( PyObject * ob )

Return non-zero if ob is a proxy object. This function always succeeds.

PyObject * PyWeakref_NewRef ( PyObject * ob , PyObject * callback )
返回值:新引用。 属于 稳定 ABI (应用程序二进制接口) .

Return a weak reference object for the object ob . This will always return a new reference, but is not guaranteed to create a new object; an existing reference object may be returned. The second parameter, callback , can be a callable object that receives notification when ob is garbage collected; it should accept a single parameter, which will be the weak reference object itself. callback 还可以为 None or NULL 。若 ob is not a weakly referenceable object, or if callback is not callable, None ,或 NULL , this will return NULL and raise TypeError .

PyObject * PyWeakref_NewProxy ( PyObject * ob , PyObject * callback )
返回值:新引用。 属于 稳定 ABI (应用程序二进制接口) .

Return a weak reference proxy object for the object ob . This will always return a new reference, but is not guaranteed to create a new object; an existing proxy object may be returned. The second parameter, callback , can be a callable object that receives notification when ob is garbage collected; it should accept a single parameter, which will be the weak reference object itself. callback 还可以为 None or NULL 。若 ob is not a weakly referenceable object, or if callback is not callable, None ,或 NULL , this will return NULL and raise TypeError .

int PyWeakref_GetRef ( PyObject * ref , PyObject * * pobj )
属于 稳定 ABI (应用程序二进制接口) since version 3.13.

Get a 强引用 to the referenced object from a weak reference, ref ,进 *pobj .

  • On success, set *pobj to a new 强引用 to the referenced object and return 1.

  • If the reference is dead, set *pobj to NULL and return 0.

  • On error, raise an exception and return -1.

3.13 版添加。