Movatterモバイル変換


[0]ホーム

URL:


Navigation

Reference Counting

The macros in this section are used for managing reference counts of Pythonobjects.

voidPy_INCREF(PyObject *o)

Increment the reference count for objecto. The object must not beNULL; ifyou aren’t sure that it isn’tNULL, usePy_XINCREF().

voidPy_XINCREF(PyObject *o)

Increment the reference count for objecto. The object may beNULL, inwhich case the macro has no effect.

voidPy_DECREF(PyObject *o)

Decrement the reference count for objecto. The object must not beNULL; ifyou aren’t sure that it isn’tNULL, usePy_XDECREF(). If the referencecount reaches zero, the object’s type’s deallocation function (which must not beNULL) is invoked.

Warning

The deallocation function can cause arbitrary Python code to be invoked (e.g.when a class instance with a__del__() method is deallocated). Whileexceptions in such code are not propagated, the executed code has free access toall Python global variables. This means that any object that is reachable froma global variable should be in a consistent state beforePy_DECREF() isinvoked. For example, code to delete an object from a list should copy areference to the deleted object in a temporary variable, update the list datastructure, and then callPy_DECREF() for the temporary variable.

voidPy_XDECREF(PyObject *o)

Decrement the reference count for objecto. The object may beNULL, inwhich case the macro has no effect; otherwise the effect is the same as forPy_DECREF(), and the same warning applies.

voidPy_CLEAR(PyObject *o)

Decrement the reference count for objecto. The object may beNULL, inwhich case the macro has no effect; otherwise the effect is the same as forPy_DECREF(), except that the argument is also set toNULL. The warningforPy_DECREF() does not apply with respect to the object passed becausethe macro carefully uses a temporary variable and sets the argument toNULLbefore decrementing its reference count.

It is a good idea to use this macro whenever decrementing the value of avariable that might be traversed during garbage collection.

The following functions are for runtime dynamic embedding of Python:Py_IncRef(PyObject*o),Py_DecRef(PyObject*o). They aresimply exported function versions ofPy_XINCREF() andPy_XDECREF(), respectively.

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.

Previous topic

The Very High Level Layer

Next topic

Exception Handling

This Page

Quick search

Enter search terms or a module, class or function name.

Navigation

©Copyright 1990-2017, Python Software Foundation.
The Python Software Foundation is a non-profit corporation.Please donate.
Last updated on Sep 19, 2017.Found a bug?
Created usingSphinx 1.2.

[8]ページ先頭

©2009-2025 Movatter.jp