number.pxd 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. from .object cimport PyObject
  2. cdef extern from "Python.h":
  3. #####################################################################
  4. # 6.2 Number Protocol
  5. #####################################################################
  6. bint PyNumber_Check(object o)
  7. # Returns 1 if the object o provides numeric protocols, and false
  8. # otherwise. This function always succeeds.
  9. object PyNumber_Add(object o1, object o2)
  10. # Return value: New reference.
  11. # Returns the result of adding o1 and o2, or NULL on failure. This
  12. # is the equivalent of the Python expression "o1 + o2".
  13. object PyNumber_Subtract(object o1, object o2)
  14. # Return value: New reference.
  15. # Returns the result of subtracting o2 from o1, or NULL on
  16. # failure. This is the equivalent of the Python expression "o1 -
  17. # o2".
  18. object PyNumber_Multiply(object o1, object o2)
  19. # Return value: New reference.
  20. # Returns the result of multiplying o1 and o2, or NULL on
  21. # failure. This is the equivalent of the Python expression "o1 *
  22. # o2".
  23. object PyNumber_MatrixMultiply(object o1, object o2)
  24. # Return value: New reference.
  25. # Returns the result of matrix multiplication on o1 and o2, or
  26. # NULL on failure. This is the equivalent of the Python
  27. # expression "o1 @ o2".
  28. # New in version 3.5.
  29. object PyNumber_Divide(object o1, object o2)
  30. # Return value: New reference.
  31. # Returns the result of dividing o1 by o2, or NULL on
  32. # failure. This is the equivalent of the Python expression "o1 /
  33. # o2".
  34. object PyNumber_FloorDivide(object o1, object o2)
  35. # Return value: New reference.
  36. # Return the floor of o1 divided by o2, or NULL on failure. This
  37. # is equivalent to the ``classic'' division of integers.
  38. object PyNumber_TrueDivide(object o1, object o2)
  39. # Return value: New reference.
  40. # Return a reasonable approximation for the mathematical value of
  41. # o1 divided by o2, or NULL on failure. The return value is
  42. # ``approximate'' because binary floating point numbers are
  43. # approximate; it is not possible to represent all real numbers in
  44. # base two. This function can return a floating point value when
  45. # passed two integers.
  46. object PyNumber_Remainder(object o1, object o2)
  47. # Return value: New reference.
  48. # Returns the remainder of dividing o1 by o2, or NULL on
  49. # failure. This is the equivalent of the Python expression "o1 %
  50. # o2".
  51. object PyNumber_Divmod(object o1, object o2)
  52. # Return value: New reference.
  53. # See the built-in function divmod(). Returns NULL on
  54. # failure. This is the equivalent of the Python expression
  55. # "divmod(o1, o2)".
  56. object PyNumber_Power(object o1, object o2, object o3)
  57. # Return value: New reference.
  58. # See the built-in function pow(). Returns NULL on failure. This
  59. # is the equivalent of the Python expression "pow(o1, o2, o3)",
  60. # where o3 is optional. If o3 is to be ignored, pass Py_None in
  61. # its place (passing NULL for o3 would cause an illegal memory
  62. # access).
  63. object PyNumber_Negative(object o)
  64. # Return value: New reference.
  65. # Returns the negation of o on success, or NULL on failure. This
  66. # is the equivalent of the Python expression "-o".
  67. object PyNumber_Positive(object o)
  68. # Return value: New reference.
  69. # Returns o on success, or NULL on failure. This is the equivalent
  70. # of the Python expression "+o".
  71. object PyNumber_Absolute(object o)
  72. # Return value: New reference.
  73. # Returns the absolute value of o, or NULL on failure. This is the
  74. # equivalent of the Python expression "abs(o)".
  75. object PyNumber_Invert(object o)
  76. # Return value: New reference.
  77. # Returns the bitwise negation of o on success, or NULL on
  78. # failure. This is the equivalent of the Python expression "~o".
  79. object PyNumber_Lshift(object o1, object o2)
  80. # Return value: New reference.
  81. # Returns the result of left shifting o1 by o2 on success, or NULL
  82. # on failure. This is the equivalent of the Python expression "o1
  83. # << o2".
  84. object PyNumber_Rshift(object o1, object o2)
  85. # Return value: New reference.
  86. # Returns the result of right shifting o1 by o2 on success, or
  87. # NULL on failure. This is the equivalent of the Python expression
  88. # "o1 >> o2".
  89. object PyNumber_And(object o1, object o2)
  90. # Return value: New reference.
  91. # Returns the ``bitwise and'' of o1 and o2 on success and NULL on
  92. # failure. This is the equivalent of the Python expression "o1 &
  93. # o2".
  94. object PyNumber_Xor(object o1, object o2)
  95. # Return value: New reference.
  96. # Returns the ``bitwise exclusive or'' of o1 by o2 on success, or
  97. # NULL on failure. This is the equivalent of the Python expression
  98. # "o1 ^ o2".
  99. object PyNumber_Or(object o1, object o2)
  100. # Return value: New reference.
  101. # Returns the ``bitwise or'' of o1 and o2 on success, or NULL on failure. This is the equivalent of the Python expression "o1 | o2".
  102. object PyNumber_InPlaceAdd(object o1, object o2)
  103. # Return value: New reference.
  104. # Returns the result of adding o1 and o2, or NULL on failure. The
  105. # operation is done in-place when o1 supports it. This is the
  106. # equivalent of the Python statement "o1 += o2".
  107. object PyNumber_InPlaceSubtract(object o1, object o2)
  108. # Return value: New reference.
  109. # Returns the result of subtracting o2 from o1, or NULL on
  110. # failure. The operation is done in-place when o1 supports
  111. # it. This is the equivalent of the Python statement "o1 -= o2".
  112. object PyNumber_InPlaceMultiply(object o1, object o2)
  113. # Return value: New reference.
  114. # Returns the result of multiplying o1 and o2, or NULL on
  115. # failure. The operation is done in-place when o1 supports
  116. # it. This is the equivalent of the Python statement "o1 *= o2".
  117. object PyNumber_InPlaceMatrixMultiply(object o1, object o2)
  118. # Return value: New reference.
  119. # Returns the result of matrix multiplication on o1 and o2, or
  120. # NULL on failure. The operation is done in-place when o1 supports
  121. # it. This is the equivalent of the Python statement "o1 @= o2".
  122. # New in version 3.5.
  123. object PyNumber_InPlaceDivide(object o1, object o2)
  124. # Return value: New reference.
  125. # Returns the result of dividing o1 by o2, or NULL on failure. The
  126. # operation is done in-place when o1 supports it. This is the
  127. # equivalent of the Python statement "o1 /= o2".
  128. object PyNumber_InPlaceFloorDivide(object o1, object o2)
  129. # Return value: New reference.
  130. # Returns the mathematical floor of dividing o1 by o2, or NULL on
  131. # failure. The operation is done in-place when o1 supports
  132. # it. This is the equivalent of the Python statement "o1 //=
  133. # o2".
  134. object PyNumber_InPlaceTrueDivide(object o1, object o2)
  135. # Return value: New reference.
  136. # Return a reasonable approximation for the mathematical value of
  137. # o1 divided by o2, or NULL on failure. The return value is
  138. # ``approximate'' because binary floating point numbers are
  139. # approximate; it is not possible to represent all real numbers in
  140. # base two. This function can return a floating point value when
  141. # passed two integers. The operation is done in-place when o1
  142. # supports it.
  143. object PyNumber_InPlaceRemainder(object o1, object o2)
  144. # Return value: New reference.
  145. # Returns the remainder of dividing o1 by o2, or NULL on
  146. # failure. The operation is done in-place when o1 supports
  147. # it. This is the equivalent of the Python statement "o1 %= o2".
  148. object PyNumber_InPlacePower(object o1, object o2, object o3)
  149. # Return value: New reference.
  150. # See the built-in function pow(). Returns NULL on failure. The
  151. # operation is done in-place when o1 supports it. This is the
  152. # equivalent of the Python statement "o1 **= o2" when o3 is
  153. # Py_None, or an in-place variant of "pow(o1, o2, o3)"
  154. # otherwise. If o3 is to be ignored, pass Py_None in its place
  155. # (passing NULL for o3 would cause an illegal memory access).
  156. object PyNumber_InPlaceLshift(object o1, object o2)
  157. # Return value: New reference.
  158. # Returns the result of left shifting o1 by o2 on success, or NULL
  159. # on failure. The operation is done in-place when o1 supports
  160. # it. This is the equivalent of the Python statement "o1 <<= o2".
  161. object PyNumber_InPlaceRshift(object o1, object o2)
  162. # Return value: New reference.
  163. # Returns the result of right shifting o1 by o2 on success, or
  164. # NULL on failure. The operation is done in-place when o1 supports
  165. # it. This is the equivalent of the Python statement "o1 >>= o2".
  166. object PyNumber_InPlaceAnd(object o1, object o2)
  167. # Return value: New reference.
  168. # Returns the ``bitwise and'' of o1 and o2 on success and NULL on
  169. # failure. The operation is done in-place when o1 supports
  170. # it. This is the equivalent of the Python statement "o1 &= o2".
  171. object PyNumber_InPlaceXor(object o1, object o2)
  172. # Return value: New reference.
  173. # Returns the ``bitwise exclusive or'' of o1 by o2 on success, or
  174. # NULL on failure. The operation is done in-place when o1 supports
  175. # it. This is the equivalent of the Python statement "o1 ^= o2".
  176. object PyNumber_InPlaceOr(object o1, object o2)
  177. # Return value: New reference.
  178. # Returns the ``bitwise or'' of o1 and o2 on success, or NULL on
  179. # failure. The operation is done in-place when o1 supports
  180. # it. This is the equivalent of the Python statement "o1 |= o2".
  181. int PyNumber_Coerce(PyObject **p1, PyObject **p2) except -1
  182. # This function takes the addresses of two variables of type
  183. # PyObject*. If the objects pointed to by *p1 and *p2 have the
  184. # same type, increment their reference count and return 0
  185. # (success). If the objects can be converted to a common numeric
  186. # type, replace *p1 and *p2 by their converted value (with 'new'
  187. # reference counts), and return 0. If no conversion is possible,
  188. # or if some other error occurs, return -1 (failure) and don't
  189. # increment the reference counts. The call PyNumber_Coerce(&o1,
  190. # &o2) is equivalent to the Python statement "o1, o2 = coerce(o1,
  191. # o2)".
  192. object PyNumber_Int(object o)
  193. # Return value: New reference.
  194. # Returns the o converted to an integer object on success, or NULL
  195. # on failure. If the argument is outside the integer range a long
  196. # object will be returned instead. This is the equivalent of the
  197. # Python expression "int(o)".
  198. object PyNumber_Long(object o)
  199. # Return value: New reference.
  200. # Returns the o converted to a long integer object on success, or
  201. # NULL on failure. This is the equivalent of the Python expression
  202. # "long(o)".
  203. object PyNumber_Float(object o)
  204. # Return value: New reference.
  205. # Returns the o converted to a float object on success, or NULL on
  206. # failure. This is the equivalent of the Python expression
  207. # "float(o)".
  208. object PyNumber_Index(object o)
  209. # Returns the o converted to a Python int or long on success or
  210. # NULL with a TypeError exception raised on failure.
  211. Py_ssize_t PyNumber_AsSsize_t(object o, object exc) except? -1
  212. # Returns o converted to a Py_ssize_t value if o can be
  213. # interpreted as an integer. If o can be converted to a Python int
  214. # or long but the attempt to convert to a Py_ssize_t value would
  215. # raise an OverflowError, then the exc argument is the type of
  216. # exception that will be raised (usually IndexError or
  217. # OverflowError). If exc is NULL, then the exception is cleared
  218. # and the value is clipped to PY_SSIZE_T_MIN for a negative
  219. # integer or PY_SSIZE_T_MAX for a positive integer.
  220. bint PyIndex_Check(object)
  221. # Returns True if o is an index integer (has the nb_index slot of
  222. # the tp_as_number structure filled in).