ref.pxd 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. from .object cimport PyObject, PyTypeObject, Py_TYPE # legacy imports for re-export
  2. cdef extern from "Python.h":
  3. #####################################################################
  4. # 3. Reference Counts
  5. #####################################################################
  6. # The macros in this section are used for managing reference counts of Python objects.
  7. void Py_INCREF(object o)
  8. # Increment the reference count for object o. The object must not
  9. # be NULL; if you aren't sure that it isn't NULL, use
  10. # Py_XINCREF().
  11. void Py_XINCREF(PyObject* o)
  12. # Increment the reference count for object o. The object may be NULL, in which case the macro has no effect.
  13. void Py_DECREF(object o)
  14. # Decrement the reference count for object o. The object must not
  15. # be NULL; if you aren't sure that it isn't NULL, use
  16. # Py_XDECREF(). If the reference count reaches zero, the object's
  17. # type's deallocation function (which must not be NULL) is
  18. # invoked.
  19. # Warning: The deallocation function can cause arbitrary Python
  20. # code to be invoked (e.g. when a class instance with a __del__()
  21. # method is deallocated). While exceptions in such code are not
  22. # propagated, the executed code has free access to all Python
  23. # global variables. This means that any object that is reachable
  24. # from a global variable should be in a consistent state before
  25. # Py_DECREF() is invoked. For example, code to delete an object
  26. # from a list should copy a reference to the deleted object in a
  27. # temporary variable, update the list data structure, and then
  28. # call Py_DECREF() for the temporary variable.
  29. void Py_XDECREF(PyObject* o)
  30. # Decrement the reference count for object o. The object may be
  31. # NULL, in which case the macro has no effect; otherwise the
  32. # effect is the same as for Py_DECREF(), and the same warning
  33. # applies.
  34. void Py_CLEAR(PyObject* o)
  35. # Decrement the reference count for object o. The object may be
  36. # NULL, in which case the macro has no effect; otherwise the
  37. # effect is the same as for Py_DECREF(), except that the argument
  38. # is also set to NULL. The warning for Py_DECREF() does not apply
  39. # with respect to the object passed because the macro carefully
  40. # uses a temporary variable and sets the argument to NULL before
  41. # decrementing its reference count.
  42. # It is a good idea to use this macro whenever decrementing the
  43. # value of a variable that might be traversed during garbage
  44. # collection.