The macros in this section are used for managing reference counts of Python objects.
voidPy_INCREF(
*o)
Indicate taking a new
to object o, indicating it is in use and should not be destroyed.
This function is usually used to convert a
to a
in-place. The
function can be used to create a new
.
When done using the object, release it by calling
.
The object must not be NULL; if you aren’t sure that it isn’t NULL, use
.
Do not expect this function to actually modify o in any way.
voidPy_XINCREF(
*o)
Similar to
, but the object o can be NULL, in which case this has no effect.
See also
.
*Py_NewRef(
*o)
Part of the
since version 3.10.Create a new
to an object: call
on o and return the object o.
When the
is no longer needed,
should be called on it to release the reference.
The object o must not be NULL; use
if o can be NULL.
For example:
Py_INCREF(obj);self->attr=obj;can be written as:
self->attr=Py_NewRef(obj);See also
.
New in version 3.10.
*Py_XNewRef(
*o)
Part of the
since version 3.10.Similar to
, but the object o can be NULL.
If the object o is NULL, the function just returns NULL.
New in version 3.10.
voidPy_DECREF(
*o)
Release a
to object o, indicating the reference is no longer used.
Once the last
is released (i.e. the object’s reference count reaches 0), 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, use
.
Do not expect this function to actually modify o in any way.
Warning
The deallocation function can cause arbitrary Python code to be invoked (e.g. when a class instance with a
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
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
for the temporary variable.
voidPy_XDECREF(
*o)
Similar to
, but the object o can be NULL, in which case this has no effect. The same warning from
applies here as well.
voidPy_CLEAR(
*o)
Release a
for object o. The object may be NULL, in which case the macro has no effect; otherwise the effect is the same as for
, except that the argument is also set to NULL. The warning for
does not apply with respect to the object passed because the macro carefully uses a temporary variable and sets the argument to NULL before releasing the reference.
It is a good idea to use this macro whenever releasing a reference to an object that might be traversed during garbage collection.
voidPy_IncRef(
*o)
Part of the
.Indicate taking a new
to object o. A function version of
. It can be used for runtime dynamic embedding of Python.
voidPy_DecRef(
*o)
Part of the
.Release a
to object o. A function version of
. 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.