引用计数

此节中的宏用于管理 Python 对象的引用计数。

void Py_INCREF ( PyObject * o )

递增引用计数为对象 o .

This function is usually used to convert a 借位引用 强引用 in-place. The Py_NewRef() function can be used to create a new 强引用 .

The object must not be NULL ; if you aren’t sure that it isn’t NULL ,使用 Py_XINCREF() .

void Py_XINCREF ( PyObject * o )

递增引用计数为对象 o 。对象可能是 NULL , in which case the macro has no effect.

另请参阅 Py_XNewRef() .

PyObject * Py_NewRef ( PyObject * o )
属于 稳定 ABI (应用程序二进制接口) since version 3.10.

创建新的 强引用 to an object: increment the reference count of the object o and return the object o .

强引用 is no longer needed, Py_DECREF() should be called on it to decrement the object reference count.

The object o 不得为 NULL ;使用 Py_XNewRef() if o 可以是 NULL .

例如:

Py_INCREF(obj);
self->attr = obj;
						

can be written as:

self->attr = Py_NewRef(obj);
						

另请参阅 Py_INCREF() .

3.10 版新增。

PyObject * Py_XNewRef ( PyObject * o )
属于 稳定 ABI (应用程序二进制接口) since version 3.10.

类似于 Py_NewRef() , but the object o can be NULL.

If the object o is NULL , the function just returns NULL .

3.10 版新增。

void Py_DECREF ( PyObject * o )

递减引用计数为对象 o .

If the reference count reaches zero, the object’s type’s deallocation function (which must not be NULL ) is invoked.

This function is usually used to delete a 强引用 before exiting its scope.

The object must not be NULL ; if you aren’t sure that it isn’t NULL ,使用 Py_XDECREF() .

警告

The deallocation function can cause arbitrary Python code to be invoked (e.g. when a class instance with a __del__() method is deallocated). While exceptions in such code are not propagated, the executed code has free access to all Python global variables. This means that any object that is reachable from a global variable should be in a consistent state before Py_DECREF() is invoked. For example, code to delete an object from a list should copy a reference to the deleted object in a temporary variable, update the list data structure, and then call Py_DECREF() for the temporary variable.

void Py_XDECREF ( PyObject * o )

递减引用计数为对象 o 。对象可能是 NULL , in which case the macro has no effect; otherwise the effect is the same as for Py_DECREF() , and the same warning applies.

void Py_CLEAR ( PyObject * o )

递减引用计数为对象 o 。对象可能是 NULL , in which case the macro has no effect; otherwise the effect is the same as for Py_DECREF() , except that the argument is also set to NULL . The warning for Py_DECREF() does not apply with respect to the object passed because the macro carefully uses a temporary variable and sets the argument to NULL before decrementing its reference count.

It is a good idea to use this macro whenever decrementing the reference count of an object that might be traversed during garbage collection.

void Py_IncRef ( PyObject * o )
属于 稳定 ABI (应用程序二进制接口) .

递增引用计数为对象 o . A function version of Py_XINCREF() . It can be used for runtime dynamic embedding of Python.

void Py_DecRef ( PyObject * o )
属于 稳定 ABI (应用程序二进制接口) .

递减引用计数为对象 o . A function version of Py_XDECREF() . It can be used for runtime dynamic embedding of Python.

以下函数或宏仅用于解释器核心: _Py_Dealloc() , _Py_ForgetReference() , _Py_NewReference() ,及全局变量 _Py_RefTotal .

上一话题

很高级层

下一话题

异常处理

本页