__init__.pxd 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #####################################################################
  2. #
  3. # These are the Cython pxd files for (most of) the Python/C API.
  4. #
  5. # REFERENCE COUNTING:
  6. #
  7. # JUST TO SCARE YOU:
  8. # If you are going to use any of the Python/C API in your Cython
  9. # program, you might be responsible for doing reference counting.
  10. # Read http://docs.python.org/api/refcounts.html which is so
  11. # important I've copied it below.
  12. #
  13. # For all the declaration below, whenever the Py_ function returns
  14. # a *new reference* to a PyObject*, the return type is "object".
  15. # When the function returns a borrowed reference, the return
  16. # type is PyObject*. When Cython sees "object" as a return type
  17. # it doesn't increment the reference count. When it sees PyObject*
  18. # in order to use the result you must explicitly cast to <object>,
  19. # and when you do that Cython increments the reference count whether
  20. # you want it to or not, forcing you to an explicit DECREF (or leak memory).
  21. # To avoid this we make the above convention. Note, you can
  22. # always locally override this convention by putting something like
  23. #
  24. # cdef extern from "Python.h":
  25. # PyObject* PyNumber_Add(PyObject *o1, PyObject *o2)
  26. #
  27. # in your .pyx file or into a cimported .pxd file. You just have to
  28. # use the one from the right (pxd-)namespace then.
  29. #
  30. # Cython automatically takes care of reference counting for anything
  31. # of type object.
  32. #
  33. ## More precisely, I think the correct convention for
  34. ## using the Python/C API from Cython is as follows.
  35. ##
  36. ## (1) Declare all input arguments as type "object". This way no explicit
  37. ## <PyObject*> casting is needed, and moreover Cython doesn't generate
  38. ## any funny reference counting.
  39. ## (2) Declare output as object if a new reference is returned.
  40. ## (3) Declare output as PyObject* if a borrowed reference is returned.
  41. ##
  42. ## This way when you call objects, no cast is needed, and if the api
  43. ## calls returns a new reference (which is about 95% of them), then
  44. ## you can just assign to a variable of type object. With borrowed
  45. ## references if you do an explicit typecast to <object>, Cython generates an
  46. ## INCREF and DECREF so you have to be careful. However, you got a
  47. ## borrowed reference in this case, so there's got to be another reference
  48. ## to your object, so you're OK, as long as you relealize this
  49. ## and use the result of an explicit cast to <object> as a borrowed
  50. ## reference (and you can call Py_INCREF if you want to turn it
  51. ## into another reference for some reason).
  52. #
  53. # "The reference count is important because today's computers have
  54. # a finite (and often severely limited) memory size; it counts how
  55. # many different places there are that have a reference to an
  56. # object. Such a place could be another object, or a global (or
  57. # static) C variable, or a local variable in some C function. When
  58. # an object's reference count becomes zero, the object is
  59. # deallocated. If it contains references to other objects, their
  60. # reference count is decremented. Those other objects may be
  61. # deallocated in turn, if this decrement makes their reference
  62. # count become zero, and so on. (There's an obvious problem with
  63. # objects that reference each other here; for now, the solution is
  64. # ``don't do that.'')
  65. #
  66. # Reference counts are always manipulated explicitly. The normal
  67. # way is to use the macro Py_INCREF() to increment an object's
  68. # reference count by one, and Py_DECREF() to decrement it by
  69. # one. The Py_DECREF() macro is considerably more complex than the
  70. # incref one, since it must check whether the reference count
  71. # becomes zero and then cause the object's deallocator to be
  72. # called. The deallocator is a function pointer contained in the
  73. # object's type structure. The type-specific deallocator takes
  74. # care of decrementing the reference counts for other objects
  75. # contained in the object if this is a compound object type, such
  76. # as a list, as well as performing any additional finalization
  77. # that's needed. There's no chance that the reference count can
  78. # overflow; at least as many bits are used to hold the reference
  79. # count as there are distinct memory locations in virtual memory
  80. # (assuming sizeof(long) >= sizeof(char*)). Thus, the reference
  81. # count increment is a simple operation.
  82. #
  83. # It is not necessary to increment an object's reference count for
  84. # every local variable that contains a pointer to an object. In
  85. # theory, the object's reference count goes up by one when the
  86. # variable is made to point to it and it goes down by one when the
  87. # variable goes out of scope. However, these two cancel each other
  88. # out, so at the end the reference count hasn't changed. The only
  89. # real reason to use the reference count is to prevent the object
  90. # from being deallocated as long as our variable is pointing to
  91. # it. If we know that there is at least one other reference to the
  92. # object that lives at least as long as our variable, there is no
  93. # need to increment the reference count temporarily. An important
  94. # situation where this arises is in objects that are passed as
  95. # arguments to C functions in an extension module that are called
  96. # from Python; the call mechanism guarantees to hold a reference
  97. # to every argument for the duration of the call.
  98. #
  99. # However, a common pitfall is to extract an object from a list
  100. # and hold on to it for a while without incrementing its reference
  101. # count. Some other operation might conceivably remove the object
  102. # from the list, decrementing its reference count and possible
  103. # deallocating it. The real danger is that innocent-looking
  104. # operations may invoke arbitrary Python code which could do this;
  105. # there is a code path which allows control to flow back to the
  106. # user from a Py_DECREF(), so almost any operation is potentially
  107. # dangerous.
  108. #
  109. # A safe approach is to always use the generic operations
  110. # (functions whose name begins with "PyObject_", "PyNumber_",
  111. # "PySequence_" or "PyMapping_"). These operations always
  112. # increment the reference count of the object they return. This
  113. # leaves the caller with the responsibility to call Py_DECREF()
  114. # when they are done with the result; this soon becomes second
  115. # nature.
  116. #
  117. # Now you should read http://docs.python.org/api/refcountDetails.html
  118. # just to be sure you understand what is going on.
  119. #
  120. #################################################################
  121. #################################################################
  122. # BIG FAT DEPRECATION WARNING
  123. #################################################################
  124. # Do NOT cimport any names directly from the cpython package,
  125. # despite of the star-imports below. They will be removed at
  126. # some point.
  127. # Instead, use the correct sub-module to draw your cimports from.
  128. #
  129. # A direct cimport from the package will make your code depend on
  130. # all of the existing declarations. This may have side-effects
  131. # and reduces the portability of your code.
  132. #################################################################
  133. # START OF DEPRECATED SECTION
  134. #################################################################
  135. from cpython.version cimport *
  136. from cpython.ref cimport *
  137. from cpython.exc cimport *
  138. from cpython.module cimport *
  139. from cpython.mem cimport *
  140. from cpython.tuple cimport *
  141. from cpython.list cimport *
  142. from cpython.object cimport *
  143. from cpython.sequence cimport *
  144. from cpython.mapping cimport *
  145. from cpython.iterator cimport *
  146. from cpython.type cimport *
  147. from cpython.number cimport *
  148. from cpython.int cimport *
  149. from cpython.bool cimport *
  150. from cpython.long cimport *
  151. from cpython.float cimport *
  152. from cpython.complex cimport *
  153. from cpython.string cimport *
  154. from cpython.unicode cimport *
  155. from cpython.dict cimport *
  156. from cpython.instance cimport *
  157. from cpython.function cimport *
  158. from cpython.method cimport *
  159. from cpython.weakref cimport *
  160. from cpython.getargs cimport *
  161. from cpython.pythread cimport *
  162. from cpython.pystate cimport *
  163. # Python <= 2.x
  164. from cpython.cobject cimport *
  165. from cpython.oldbuffer cimport *
  166. # Python >= 2.4
  167. from cpython.set cimport *
  168. # Python >= 2.6
  169. from cpython.buffer cimport *
  170. from cpython.bytes cimport *
  171. # Python >= 3.0
  172. from cpython.pycapsule cimport *
  173. #################################################################
  174. # END OF DEPRECATED SECTION
  175. #################################################################