source: python/trunk/Doc/c-api/refcounting.rst

Last change on this file was 391, checked in by dmik, 11 years ago

python: Merge vendor 2.7.6 to trunk.

  • Property svn:eol-style set to native
File size: 2.9 KB
RevLine 
[2]1.. highlightlang:: c
2
3
4.. _countingrefs:
5
6******************
7Reference Counting
8******************
9
10The macros in this section are used for managing reference counts of Python
11objects.
12
13
[391]14.. c:function:: void Py_INCREF(PyObject *o)
[2]15
16 Increment the reference count for object *o*. The object must not be *NULL*; if
[391]17 you aren't sure that it isn't *NULL*, use :c:func:`Py_XINCREF`.
[2]18
19
[391]20.. c:function:: void Py_XINCREF(PyObject *o)
[2]21
22 Increment the reference count for object *o*. The object may be *NULL*, in
23 which case the macro has no effect.
24
25
[391]26.. c:function:: void Py_DECREF(PyObject *o)
[2]27
28 Decrement the reference count for object *o*. The object must not be *NULL*; if
[391]29 you aren't sure that it isn't *NULL*, use :c:func:`Py_XDECREF`. If the reference
[2]30 count reaches zero, the object's type's deallocation function (which must not be
31 *NULL*) is invoked.
32
33 .. warning::
34
35 The deallocation function can cause arbitrary Python code to be invoked (e.g.
36 when a class instance with a :meth:`__del__` method is deallocated). While
37 exceptions in such code are not propagated, the executed code has free access to
38 all Python global variables. This means that any object that is reachable from
[391]39 a global variable should be in a consistent state before :c:func:`Py_DECREF` is
[2]40 invoked. For example, code to delete an object from a list should copy a
41 reference to the deleted object in a temporary variable, update the list data
[391]42 structure, and then call :c:func:`Py_DECREF` for the temporary variable.
[2]43
44
[391]45.. c:function:: void Py_XDECREF(PyObject *o)
[2]46
47 Decrement the reference count for object *o*. The object may be *NULL*, in
48 which case the macro has no effect; otherwise the effect is the same as for
[391]49 :c:func:`Py_DECREF`, and the same warning applies.
[2]50
51
[391]52.. c:function:: void Py_CLEAR(PyObject *o)
[2]53
54 Decrement the reference count for object *o*. The object may be *NULL*, in
55 which case the macro has no effect; otherwise the effect is the same as for
[391]56 :c:func:`Py_DECREF`, except that the argument is also set to *NULL*. The warning
57 for :c:func:`Py_DECREF` does not apply with respect to the object passed because
[2]58 the macro carefully uses a temporary variable and sets the argument to *NULL*
59 before decrementing its reference count.
60
61 It is a good idea to use this macro whenever decrementing the value of a
62 variable that might be traversed during garbage collection.
63
64 .. versionadded:: 2.4
65
66The following functions are for runtime dynamic embedding of Python:
67``Py_IncRef(PyObject *o)``, ``Py_DecRef(PyObject *o)``. They are
[391]68simply exported function versions of :c:func:`Py_XINCREF` and
69:c:func:`Py_XDECREF`, respectively.
[2]70
71The following functions or macros are only for use within the interpreter core:
[391]72:c:func:`_Py_Dealloc`, :c:func:`_Py_ForgetReference`, :c:func:`_Py_NewReference`,
73as well as the global variable :c:data:`_Py_RefTotal`.
[2]74
Note: See TracBrowser for help on using the repository browser.