/Doc/c-api/refcounting.rst

http://unladen-swallow.googlecode.com/ · ReStructuredText · 74 lines · 48 code · 26 blank · 0 comment · 0 complexity · a0f24494003dfe2d666b331f57e5a1be MD5 · raw file

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