参照カウント¶
The macros in this section are used for managing reference counts of Python objects.
-
void Py_INCREF(PyObject *o)¶
Increment the reference count for object o.
This function is usually used to convert a borrowed reference to a strong reference in-place. The
Py_NewRef()
function can be used to create a new strong reference.オブジェクトが
NULL
であってはいけません。それがNULL
ではないと確信が持てないならば、Py_XINCREF()
を使ってください。
-
void Py_XINCREF(PyObject *o)¶
オブジェクト o に対する参照カウントを一つ増やします。 オブジェクトが
NULL
であってもよく、その場合マクロは何の影響も与えません。See also
Py_XNewRef()
.
-
PyObject *Py_NewRef(PyObject *o)¶
- Part of the Stable ABI since version 3.10.
Create a new strong reference to an object: increment the reference count of the object o and return the object o.
When the strong reference is no longer needed,
Py_DECREF()
should be called on it to decrement the object reference count.The object o must not be
NULL
; usePy_XNewRef()
if o can beNULL
.例えば:
Py_INCREF(obj); self->attr = obj;
can be written as:
self->attr = Py_NewRef(obj);
See also
Py_INCREF()
.バージョン 3.10 で追加.
-
PyObject *Py_XNewRef(PyObject *o)¶
- Part of the Stable ABI since version 3.10.
Similar to
Py_NewRef()
, but the object o can be NULL.If the object o is
NULL
, the function just returnsNULL
.バージョン 3.10 で追加.
-
void Py_DECREF(PyObject *o)¶
Decrement the reference count for object 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 strong reference before exiting its scope.
オブジェクトが
NULL
であってはいけません。それがNULL
ではないと確信が持てないならば、Py_XDECREF()
を使ってください。警告
(例えば
__del__()
メソッドをもつクラスインスタンスがメモリ解放されたときに)メモリ解放関数は任意のPythonコードを呼び出すことができます。このようなコードでは例外は伝播しませんが、実行されたコードはすべてのPythonグローバル変数に自由にアクセスできます。これが意味するのは、Py_DECREF()
が呼び出されるより前では、グローバル変数から到達可能などんなオブジェクトも一貫した状態にあるべきであるということです。例えば、リストからオブジェクトを削除するコードは削除するオブジェクトへの参照を一時変数にコピーし、リストデータ構造を更新し、それから一時変数に対してPy_DECREF()
を呼び出すべきです。
-
void Py_XDECREF(PyObject *o)¶
オブジェクト o への参照カウントを一つ減らします。 オブジェクトは
NULL
でもかまいませんが、その場合マクロは何の影響も与えません。 それ以外の場合、結果はPy_DECREF()
と同じです。 また、注意すべきことも同じです。
-
void Py_CLEAR(PyObject *o)¶
o の参照カウントを減らします。 オブジェクトは
NULL
でもよく、その場合このマクロは何も行いません。 オブジェクトがNULL
でなければ、引数をNULL
にしたPy_DECREF()
と同じ効果をもたらします。 このマクロは一時変数を使って、参照カウントをデクリメントする前に引数をNULL
にセットしてくれるので、Py_DECREF()
に使うときの警告を気にしなくてすみます。ガベージコレクション中に追跡される可能性のあるオブジェクトの参照カウントのデクリメントを行うには、このマクロを使うのがよいでしょう。
-
void Py_IncRef(PyObject *o)¶
- Part of the Stable ABI.
Increment the reference count for object o. A function version of
Py_XINCREF()
. It can be used for runtime dynamic embedding of Python.
-
void Py_DecRef(PyObject *o)¶
- Part of the Stable ABI.
Decrement the reference count for object o. A function version of
Py_XDECREF()
. It can be used for runtime dynamic embedding of Python.
The following functions or macros are only for use within the interpreter core:
_Py_Dealloc()
, _Py_ForgetReference()
, _Py_NewReference()
,
as well as the global variable _Py_RefTotal
.