__init__.cython-30.pxd 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050
  1. # NumPy static imports for Cython >= 3.0
  2. #
  3. # If any of the PyArray_* functions are called, import_array must be
  4. # called first. This is done automatically by Cython 3.0+ if a call
  5. # is not detected inside of the module.
  6. #
  7. # Author: Dag Sverre Seljebotn
  8. #
  9. from cpython.ref cimport Py_INCREF
  10. from cpython.object cimport PyObject, PyTypeObject, PyObject_TypeCheck
  11. cimport libc.stdio as stdio
  12. cdef extern from *:
  13. # Leave a marker that the NumPy declarations came from NumPy itself and not from Cython.
  14. # See https://github.com/cython/cython/issues/3573
  15. """
  16. /* Using NumPy API declarations from "numpy/__init__.cython-30.pxd" */
  17. """
  18. cdef extern from "Python.h":
  19. ctypedef int Py_intptr_t
  20. cdef extern from "numpy/arrayobject.h":
  21. ctypedef Py_intptr_t npy_intp
  22. ctypedef size_t npy_uintp
  23. cdef enum NPY_TYPES:
  24. NPY_BOOL
  25. NPY_BYTE
  26. NPY_UBYTE
  27. NPY_SHORT
  28. NPY_USHORT
  29. NPY_INT
  30. NPY_UINT
  31. NPY_LONG
  32. NPY_ULONG
  33. NPY_LONGLONG
  34. NPY_ULONGLONG
  35. NPY_FLOAT
  36. NPY_DOUBLE
  37. NPY_LONGDOUBLE
  38. NPY_CFLOAT
  39. NPY_CDOUBLE
  40. NPY_CLONGDOUBLE
  41. NPY_OBJECT
  42. NPY_STRING
  43. NPY_UNICODE
  44. NPY_VOID
  45. NPY_DATETIME
  46. NPY_TIMEDELTA
  47. NPY_NTYPES
  48. NPY_NOTYPE
  49. NPY_INT8
  50. NPY_INT16
  51. NPY_INT32
  52. NPY_INT64
  53. NPY_INT128
  54. NPY_INT256
  55. NPY_UINT8
  56. NPY_UINT16
  57. NPY_UINT32
  58. NPY_UINT64
  59. NPY_UINT128
  60. NPY_UINT256
  61. NPY_FLOAT16
  62. NPY_FLOAT32
  63. NPY_FLOAT64
  64. NPY_FLOAT80
  65. NPY_FLOAT96
  66. NPY_FLOAT128
  67. NPY_FLOAT256
  68. NPY_COMPLEX32
  69. NPY_COMPLEX64
  70. NPY_COMPLEX128
  71. NPY_COMPLEX160
  72. NPY_COMPLEX192
  73. NPY_COMPLEX256
  74. NPY_COMPLEX512
  75. NPY_INTP
  76. ctypedef enum NPY_ORDER:
  77. NPY_ANYORDER
  78. NPY_CORDER
  79. NPY_FORTRANORDER
  80. NPY_KEEPORDER
  81. ctypedef enum NPY_CASTING:
  82. NPY_NO_CASTING
  83. NPY_EQUIV_CASTING
  84. NPY_SAFE_CASTING
  85. NPY_SAME_KIND_CASTING
  86. NPY_UNSAFE_CASTING
  87. ctypedef enum NPY_CLIPMODE:
  88. NPY_CLIP
  89. NPY_WRAP
  90. NPY_RAISE
  91. ctypedef enum NPY_SCALARKIND:
  92. NPY_NOSCALAR,
  93. NPY_BOOL_SCALAR,
  94. NPY_INTPOS_SCALAR,
  95. NPY_INTNEG_SCALAR,
  96. NPY_FLOAT_SCALAR,
  97. NPY_COMPLEX_SCALAR,
  98. NPY_OBJECT_SCALAR
  99. ctypedef enum NPY_SORTKIND:
  100. NPY_QUICKSORT
  101. NPY_HEAPSORT
  102. NPY_MERGESORT
  103. ctypedef enum NPY_SEARCHSIDE:
  104. NPY_SEARCHLEFT
  105. NPY_SEARCHRIGHT
  106. enum:
  107. # DEPRECATED since NumPy 1.7 ! Do not use in new code!
  108. NPY_C_CONTIGUOUS
  109. NPY_F_CONTIGUOUS
  110. NPY_CONTIGUOUS
  111. NPY_FORTRAN
  112. NPY_OWNDATA
  113. NPY_FORCECAST
  114. NPY_ENSURECOPY
  115. NPY_ENSUREARRAY
  116. NPY_ELEMENTSTRIDES
  117. NPY_ALIGNED
  118. NPY_NOTSWAPPED
  119. NPY_WRITEABLE
  120. NPY_ARR_HAS_DESCR
  121. NPY_BEHAVED
  122. NPY_BEHAVED_NS
  123. NPY_CARRAY
  124. NPY_CARRAY_RO
  125. NPY_FARRAY
  126. NPY_FARRAY_RO
  127. NPY_DEFAULT
  128. NPY_IN_ARRAY
  129. NPY_OUT_ARRAY
  130. NPY_INOUT_ARRAY
  131. NPY_IN_FARRAY
  132. NPY_OUT_FARRAY
  133. NPY_INOUT_FARRAY
  134. NPY_UPDATE_ALL
  135. enum:
  136. # Added in NumPy 1.7 to replace the deprecated enums above.
  137. NPY_ARRAY_C_CONTIGUOUS
  138. NPY_ARRAY_F_CONTIGUOUS
  139. NPY_ARRAY_OWNDATA
  140. NPY_ARRAY_FORCECAST
  141. NPY_ARRAY_ENSURECOPY
  142. NPY_ARRAY_ENSUREARRAY
  143. NPY_ARRAY_ELEMENTSTRIDES
  144. NPY_ARRAY_ALIGNED
  145. NPY_ARRAY_NOTSWAPPED
  146. NPY_ARRAY_WRITEABLE
  147. NPY_ARRAY_WRITEBACKIFCOPY
  148. NPY_ARRAY_BEHAVED
  149. NPY_ARRAY_BEHAVED_NS
  150. NPY_ARRAY_CARRAY
  151. NPY_ARRAY_CARRAY_RO
  152. NPY_ARRAY_FARRAY
  153. NPY_ARRAY_FARRAY_RO
  154. NPY_ARRAY_DEFAULT
  155. NPY_ARRAY_IN_ARRAY
  156. NPY_ARRAY_OUT_ARRAY
  157. NPY_ARRAY_INOUT_ARRAY
  158. NPY_ARRAY_IN_FARRAY
  159. NPY_ARRAY_OUT_FARRAY
  160. NPY_ARRAY_INOUT_FARRAY
  161. NPY_ARRAY_UPDATE_ALL
  162. cdef enum:
  163. NPY_MAXDIMS
  164. npy_intp NPY_MAX_ELSIZE
  165. ctypedef void (*PyArray_VectorUnaryFunc)(void *, void *, npy_intp, void *, void *)
  166. ctypedef struct PyArray_ArrayDescr:
  167. # shape is a tuple, but Cython doesn't support "tuple shape"
  168. # inside a non-PyObject declaration, so we have to declare it
  169. # as just a PyObject*.
  170. PyObject* shape
  171. ctypedef struct PyArray_Descr:
  172. pass
  173. ctypedef class numpy.dtype [object PyArray_Descr, check_size ignore]:
  174. # Use PyDataType_* macros when possible, however there are no macros
  175. # for accessing some of the fields, so some are defined.
  176. cdef PyTypeObject* typeobj
  177. cdef char kind
  178. cdef char type
  179. # Numpy sometimes mutates this without warning (e.g. it'll
  180. # sometimes change "|" to "<" in shared dtype objects on
  181. # little-endian machines). If this matters to you, use
  182. # PyArray_IsNativeByteOrder(dtype.byteorder) instead of
  183. # directly accessing this field.
  184. cdef char byteorder
  185. cdef char flags
  186. cdef int type_num
  187. cdef int itemsize "elsize"
  188. cdef int alignment
  189. cdef object fields
  190. cdef tuple names
  191. # Use PyDataType_HASSUBARRAY to test whether this field is
  192. # valid (the pointer can be NULL). Most users should access
  193. # this field via the inline helper method PyDataType_SHAPE.
  194. cdef PyArray_ArrayDescr* subarray
  195. ctypedef class numpy.flatiter [object PyArrayIterObject, check_size ignore]:
  196. # Use through macros
  197. pass
  198. ctypedef class numpy.broadcast [object PyArrayMultiIterObject, check_size ignore]:
  199. # Use through macros
  200. pass
  201. ctypedef struct PyArrayObject:
  202. # For use in situations where ndarray can't replace PyArrayObject*,
  203. # like PyArrayObject**.
  204. pass
  205. ctypedef class numpy.ndarray [object PyArrayObject, check_size ignore]:
  206. cdef __cythonbufferdefaults__ = {"mode": "strided"}
  207. # NOTE: no field declarations since direct access is deprecated since NumPy 1.7
  208. # Instead, we use properties that map to the corresponding C-API functions.
  209. @property
  210. cdef inline PyObject* base(self) nogil:
  211. """Returns a borrowed reference to the object owning the data/memory.
  212. """
  213. return PyArray_BASE(self)
  214. @property
  215. cdef inline dtype descr(self):
  216. """Returns an owned reference to the dtype of the array.
  217. """
  218. return <dtype>PyArray_DESCR(self)
  219. @property
  220. cdef inline int ndim(self) nogil:
  221. """Returns the number of dimensions in the array.
  222. """
  223. return PyArray_NDIM(self)
  224. @property
  225. cdef inline npy_intp *shape(self) nogil:
  226. """Returns a pointer to the dimensions/shape of the array.
  227. The number of elements matches the number of dimensions of the array (ndim).
  228. Can return NULL for 0-dimensional arrays.
  229. """
  230. return PyArray_DIMS(self)
  231. @property
  232. cdef inline npy_intp *strides(self) nogil:
  233. """Returns a pointer to the strides of the array.
  234. The number of elements matches the number of dimensions of the array (ndim).
  235. """
  236. return PyArray_STRIDES(self)
  237. @property
  238. cdef inline npy_intp size(self) nogil:
  239. """Returns the total size (in number of elements) of the array.
  240. """
  241. return PyArray_SIZE(self)
  242. @property
  243. cdef inline char* data(self) nogil:
  244. """The pointer to the data buffer as a char*.
  245. This is provided for legacy reasons to avoid direct struct field access.
  246. For new code that needs this access, you probably want to cast the result
  247. of `PyArray_DATA()` instead, which returns a 'void*'.
  248. """
  249. return PyArray_BYTES(self)
  250. ctypedef unsigned char npy_bool
  251. ctypedef signed char npy_byte
  252. ctypedef signed short npy_short
  253. ctypedef signed int npy_int
  254. ctypedef signed long npy_long
  255. ctypedef signed long long npy_longlong
  256. ctypedef unsigned char npy_ubyte
  257. ctypedef unsigned short npy_ushort
  258. ctypedef unsigned int npy_uint
  259. ctypedef unsigned long npy_ulong
  260. ctypedef unsigned long long npy_ulonglong
  261. ctypedef float npy_float
  262. ctypedef double npy_double
  263. ctypedef long double npy_longdouble
  264. ctypedef signed char npy_int8
  265. ctypedef signed short npy_int16
  266. ctypedef signed int npy_int32
  267. ctypedef signed long long npy_int64
  268. ctypedef signed long long npy_int96
  269. ctypedef signed long long npy_int128
  270. ctypedef unsigned char npy_uint8
  271. ctypedef unsigned short npy_uint16
  272. ctypedef unsigned int npy_uint32
  273. ctypedef unsigned long long npy_uint64
  274. ctypedef unsigned long long npy_uint96
  275. ctypedef unsigned long long npy_uint128
  276. ctypedef float npy_float32
  277. ctypedef double npy_float64
  278. ctypedef long double npy_float80
  279. ctypedef long double npy_float96
  280. ctypedef long double npy_float128
  281. ctypedef struct npy_cfloat:
  282. float real
  283. float imag
  284. ctypedef struct npy_cdouble:
  285. double real
  286. double imag
  287. ctypedef struct npy_clongdouble:
  288. long double real
  289. long double imag
  290. ctypedef struct npy_complex64:
  291. float real
  292. float imag
  293. ctypedef struct npy_complex128:
  294. double real
  295. double imag
  296. ctypedef struct npy_complex160:
  297. long double real
  298. long double imag
  299. ctypedef struct npy_complex192:
  300. long double real
  301. long double imag
  302. ctypedef struct npy_complex256:
  303. long double real
  304. long double imag
  305. ctypedef struct PyArray_Dims:
  306. npy_intp *ptr
  307. int len
  308. int _import_array() except -1
  309. # A second definition so _import_array isn't marked as used when we use it here.
  310. # Do not use - subject to change any time.
  311. int __pyx_import_array "_import_array"() except -1
  312. #
  313. # Macros from ndarrayobject.h
  314. #
  315. bint PyArray_CHKFLAGS(ndarray m, int flags) nogil
  316. bint PyArray_IS_C_CONTIGUOUS(ndarray arr) nogil
  317. bint PyArray_IS_F_CONTIGUOUS(ndarray arr) nogil
  318. bint PyArray_ISCONTIGUOUS(ndarray m) nogil
  319. bint PyArray_ISWRITEABLE(ndarray m) nogil
  320. bint PyArray_ISALIGNED(ndarray m) nogil
  321. int PyArray_NDIM(ndarray) nogil
  322. bint PyArray_ISONESEGMENT(ndarray) nogil
  323. bint PyArray_ISFORTRAN(ndarray) nogil
  324. int PyArray_FORTRANIF(ndarray) nogil
  325. void* PyArray_DATA(ndarray) nogil
  326. char* PyArray_BYTES(ndarray) nogil
  327. npy_intp* PyArray_DIMS(ndarray) nogil
  328. npy_intp* PyArray_STRIDES(ndarray) nogil
  329. npy_intp PyArray_DIM(ndarray, size_t) nogil
  330. npy_intp PyArray_STRIDE(ndarray, size_t) nogil
  331. PyObject *PyArray_BASE(ndarray) nogil # returns borrowed reference!
  332. PyArray_Descr *PyArray_DESCR(ndarray) nogil # returns borrowed reference to dtype!
  333. PyArray_Descr *PyArray_DTYPE(ndarray) nogil # returns borrowed reference to dtype! NP 1.7+ alias for descr.
  334. int PyArray_FLAGS(ndarray) nogil
  335. void PyArray_CLEARFLAGS(ndarray, int flags) nogil # Added in NumPy 1.7
  336. void PyArray_ENABLEFLAGS(ndarray, int flags) nogil # Added in NumPy 1.7
  337. npy_intp PyArray_ITEMSIZE(ndarray) nogil
  338. int PyArray_TYPE(ndarray arr) nogil
  339. object PyArray_GETITEM(ndarray arr, void *itemptr)
  340. int PyArray_SETITEM(ndarray arr, void *itemptr, object obj) except -1
  341. bint PyTypeNum_ISBOOL(int) nogil
  342. bint PyTypeNum_ISUNSIGNED(int) nogil
  343. bint PyTypeNum_ISSIGNED(int) nogil
  344. bint PyTypeNum_ISINTEGER(int) nogil
  345. bint PyTypeNum_ISFLOAT(int) nogil
  346. bint PyTypeNum_ISNUMBER(int) nogil
  347. bint PyTypeNum_ISSTRING(int) nogil
  348. bint PyTypeNum_ISCOMPLEX(int) nogil
  349. bint PyTypeNum_ISPYTHON(int) nogil
  350. bint PyTypeNum_ISFLEXIBLE(int) nogil
  351. bint PyTypeNum_ISUSERDEF(int) nogil
  352. bint PyTypeNum_ISEXTENDED(int) nogil
  353. bint PyTypeNum_ISOBJECT(int) nogil
  354. bint PyDataType_ISBOOL(dtype) nogil
  355. bint PyDataType_ISUNSIGNED(dtype) nogil
  356. bint PyDataType_ISSIGNED(dtype) nogil
  357. bint PyDataType_ISINTEGER(dtype) nogil
  358. bint PyDataType_ISFLOAT(dtype) nogil
  359. bint PyDataType_ISNUMBER(dtype) nogil
  360. bint PyDataType_ISSTRING(dtype) nogil
  361. bint PyDataType_ISCOMPLEX(dtype) nogil
  362. bint PyDataType_ISPYTHON(dtype) nogil
  363. bint PyDataType_ISFLEXIBLE(dtype) nogil
  364. bint PyDataType_ISUSERDEF(dtype) nogil
  365. bint PyDataType_ISEXTENDED(dtype) nogil
  366. bint PyDataType_ISOBJECT(dtype) nogil
  367. bint PyDataType_HASFIELDS(dtype) nogil
  368. bint PyDataType_HASSUBARRAY(dtype) nogil
  369. bint PyArray_ISBOOL(ndarray) nogil
  370. bint PyArray_ISUNSIGNED(ndarray) nogil
  371. bint PyArray_ISSIGNED(ndarray) nogil
  372. bint PyArray_ISINTEGER(ndarray) nogil
  373. bint PyArray_ISFLOAT(ndarray) nogil
  374. bint PyArray_ISNUMBER(ndarray) nogil
  375. bint PyArray_ISSTRING(ndarray) nogil
  376. bint PyArray_ISCOMPLEX(ndarray) nogil
  377. bint PyArray_ISPYTHON(ndarray) nogil
  378. bint PyArray_ISFLEXIBLE(ndarray) nogil
  379. bint PyArray_ISUSERDEF(ndarray) nogil
  380. bint PyArray_ISEXTENDED(ndarray) nogil
  381. bint PyArray_ISOBJECT(ndarray) nogil
  382. bint PyArray_HASFIELDS(ndarray) nogil
  383. bint PyArray_ISVARIABLE(ndarray) nogil
  384. bint PyArray_SAFEALIGNEDCOPY(ndarray) nogil
  385. bint PyArray_ISNBO(char) nogil # works on ndarray.byteorder
  386. bint PyArray_IsNativeByteOrder(char) nogil # works on ndarray.byteorder
  387. bint PyArray_ISNOTSWAPPED(ndarray) nogil
  388. bint PyArray_ISBYTESWAPPED(ndarray) nogil
  389. bint PyArray_FLAGSWAP(ndarray, int) nogil
  390. bint PyArray_ISCARRAY(ndarray) nogil
  391. bint PyArray_ISCARRAY_RO(ndarray) nogil
  392. bint PyArray_ISFARRAY(ndarray) nogil
  393. bint PyArray_ISFARRAY_RO(ndarray) nogil
  394. bint PyArray_ISBEHAVED(ndarray) nogil
  395. bint PyArray_ISBEHAVED_RO(ndarray) nogil
  396. bint PyDataType_ISNOTSWAPPED(dtype) nogil
  397. bint PyDataType_ISBYTESWAPPED(dtype) nogil
  398. bint PyArray_DescrCheck(object)
  399. bint PyArray_Check(object)
  400. bint PyArray_CheckExact(object)
  401. # Cannot be supported due to out arg:
  402. # bint PyArray_HasArrayInterfaceType(object, dtype, object, object&)
  403. # bint PyArray_HasArrayInterface(op, out)
  404. bint PyArray_IsZeroDim(object)
  405. # Cannot be supported due to ## ## in macro:
  406. # bint PyArray_IsScalar(object, verbatim work)
  407. bint PyArray_CheckScalar(object)
  408. bint PyArray_IsPythonNumber(object)
  409. bint PyArray_IsPythonScalar(object)
  410. bint PyArray_IsAnyScalar(object)
  411. bint PyArray_CheckAnyScalar(object)
  412. ndarray PyArray_GETCONTIGUOUS(ndarray)
  413. bint PyArray_SAMESHAPE(ndarray, ndarray) nogil
  414. npy_intp PyArray_SIZE(ndarray) nogil
  415. npy_intp PyArray_NBYTES(ndarray) nogil
  416. object PyArray_FROM_O(object)
  417. object PyArray_FROM_OF(object m, int flags)
  418. object PyArray_FROM_OT(object m, int type)
  419. object PyArray_FROM_OTF(object m, int type, int flags)
  420. object PyArray_FROMANY(object m, int type, int min, int max, int flags)
  421. object PyArray_ZEROS(int nd, npy_intp* dims, int type, int fortran)
  422. object PyArray_EMPTY(int nd, npy_intp* dims, int type, int fortran)
  423. void PyArray_FILLWBYTE(object, int val)
  424. npy_intp PyArray_REFCOUNT(object)
  425. object PyArray_ContiguousFromAny(op, int, int min_depth, int max_depth)
  426. unsigned char PyArray_EquivArrTypes(ndarray a1, ndarray a2)
  427. bint PyArray_EquivByteorders(int b1, int b2) nogil
  428. object PyArray_SimpleNew(int nd, npy_intp* dims, int typenum)
  429. object PyArray_SimpleNewFromData(int nd, npy_intp* dims, int typenum, void* data)
  430. #object PyArray_SimpleNewFromDescr(int nd, npy_intp* dims, dtype descr)
  431. object PyArray_ToScalar(void* data, ndarray arr)
  432. void* PyArray_GETPTR1(ndarray m, npy_intp i) nogil
  433. void* PyArray_GETPTR2(ndarray m, npy_intp i, npy_intp j) nogil
  434. void* PyArray_GETPTR3(ndarray m, npy_intp i, npy_intp j, npy_intp k) nogil
  435. void* PyArray_GETPTR4(ndarray m, npy_intp i, npy_intp j, npy_intp k, npy_intp l) nogil
  436. # Cannot be supported due to out arg
  437. # void PyArray_DESCR_REPLACE(descr)
  438. object PyArray_Copy(ndarray)
  439. object PyArray_FromObject(object op, int type, int min_depth, int max_depth)
  440. object PyArray_ContiguousFromObject(object op, int type, int min_depth, int max_depth)
  441. object PyArray_CopyFromObject(object op, int type, int min_depth, int max_depth)
  442. object PyArray_Cast(ndarray mp, int type_num)
  443. object PyArray_Take(ndarray ap, object items, int axis)
  444. object PyArray_Put(ndarray ap, object items, object values)
  445. void PyArray_ITER_RESET(flatiter it) nogil
  446. void PyArray_ITER_NEXT(flatiter it) nogil
  447. void PyArray_ITER_GOTO(flatiter it, npy_intp* destination) nogil
  448. void PyArray_ITER_GOTO1D(flatiter it, npy_intp ind) nogil
  449. void* PyArray_ITER_DATA(flatiter it) nogil
  450. bint PyArray_ITER_NOTDONE(flatiter it) nogil
  451. void PyArray_MultiIter_RESET(broadcast multi) nogil
  452. void PyArray_MultiIter_NEXT(broadcast multi) nogil
  453. void PyArray_MultiIter_GOTO(broadcast multi, npy_intp dest) nogil
  454. void PyArray_MultiIter_GOTO1D(broadcast multi, npy_intp ind) nogil
  455. void* PyArray_MultiIter_DATA(broadcast multi, npy_intp i) nogil
  456. void PyArray_MultiIter_NEXTi(broadcast multi, npy_intp i) nogil
  457. bint PyArray_MultiIter_NOTDONE(broadcast multi) nogil
  458. # Functions from __multiarray_api.h
  459. # Functions taking dtype and returning object/ndarray are disabled
  460. # for now as they steal dtype references. I'm conservative and disable
  461. # more than is probably needed until it can be checked further.
  462. int PyArray_SetNumericOps (object) except -1
  463. object PyArray_GetNumericOps ()
  464. int PyArray_INCREF (ndarray) except * # uses PyArray_Item_INCREF...
  465. int PyArray_XDECREF (ndarray) except * # uses PyArray_Item_DECREF...
  466. void PyArray_SetStringFunction (object, int)
  467. dtype PyArray_DescrFromType (int)
  468. object PyArray_TypeObjectFromType (int)
  469. char * PyArray_Zero (ndarray)
  470. char * PyArray_One (ndarray)
  471. #object PyArray_CastToType (ndarray, dtype, int)
  472. int PyArray_CastTo (ndarray, ndarray) except -1
  473. int PyArray_CastAnyTo (ndarray, ndarray) except -1
  474. int PyArray_CanCastSafely (int, int) # writes errors
  475. npy_bool PyArray_CanCastTo (dtype, dtype) # writes errors
  476. int PyArray_ObjectType (object, int) except 0
  477. dtype PyArray_DescrFromObject (object, dtype)
  478. #ndarray* PyArray_ConvertToCommonType (object, int *)
  479. dtype PyArray_DescrFromScalar (object)
  480. dtype PyArray_DescrFromTypeObject (object)
  481. npy_intp PyArray_Size (object)
  482. #object PyArray_Scalar (void *, dtype, object)
  483. #object PyArray_FromScalar (object, dtype)
  484. void PyArray_ScalarAsCtype (object, void *)
  485. #int PyArray_CastScalarToCtype (object, void *, dtype)
  486. #int PyArray_CastScalarDirect (object, dtype, void *, int)
  487. object PyArray_ScalarFromObject (object)
  488. #PyArray_VectorUnaryFunc * PyArray_GetCastFunc (dtype, int)
  489. object PyArray_FromDims (int, int *, int)
  490. #object PyArray_FromDimsAndDataAndDescr (int, int *, dtype, char *)
  491. #object PyArray_FromAny (object, dtype, int, int, int, object)
  492. object PyArray_EnsureArray (object)
  493. object PyArray_EnsureAnyArray (object)
  494. #object PyArray_FromFile (stdio.FILE *, dtype, npy_intp, char *)
  495. #object PyArray_FromString (char *, npy_intp, dtype, npy_intp, char *)
  496. #object PyArray_FromBuffer (object, dtype, npy_intp, npy_intp)
  497. #object PyArray_FromIter (object, dtype, npy_intp)
  498. object PyArray_Return (ndarray)
  499. #object PyArray_GetField (ndarray, dtype, int)
  500. #int PyArray_SetField (ndarray, dtype, int, object) except -1
  501. object PyArray_Byteswap (ndarray, npy_bool)
  502. object PyArray_Resize (ndarray, PyArray_Dims *, int, NPY_ORDER)
  503. int PyArray_MoveInto (ndarray, ndarray) except -1
  504. int PyArray_CopyInto (ndarray, ndarray) except -1
  505. int PyArray_CopyAnyInto (ndarray, ndarray) except -1
  506. int PyArray_CopyObject (ndarray, object) except -1
  507. object PyArray_NewCopy (ndarray, NPY_ORDER)
  508. object PyArray_ToList (ndarray)
  509. object PyArray_ToString (ndarray, NPY_ORDER)
  510. int PyArray_ToFile (ndarray, stdio.FILE *, char *, char *) except -1
  511. int PyArray_Dump (object, object, int) except -1
  512. object PyArray_Dumps (object, int)
  513. int PyArray_ValidType (int) # Cannot error
  514. void PyArray_UpdateFlags (ndarray, int)
  515. object PyArray_New (type, int, npy_intp *, int, npy_intp *, void *, int, int, object)
  516. #object PyArray_NewFromDescr (type, dtype, int, npy_intp *, npy_intp *, void *, int, object)
  517. #dtype PyArray_DescrNew (dtype)
  518. dtype PyArray_DescrNewFromType (int)
  519. double PyArray_GetPriority (object, double) # clears errors as of 1.25
  520. object PyArray_IterNew (object)
  521. object PyArray_MultiIterNew (int, ...)
  522. int PyArray_PyIntAsInt (object) except? -1
  523. npy_intp PyArray_PyIntAsIntp (object)
  524. int PyArray_Broadcast (broadcast) except -1
  525. void PyArray_FillObjectArray (ndarray, object) except *
  526. int PyArray_FillWithScalar (ndarray, object) except -1
  527. npy_bool PyArray_CheckStrides (int, int, npy_intp, npy_intp, npy_intp *, npy_intp *)
  528. dtype PyArray_DescrNewByteorder (dtype, char)
  529. object PyArray_IterAllButAxis (object, int *)
  530. #object PyArray_CheckFromAny (object, dtype, int, int, int, object)
  531. #object PyArray_FromArray (ndarray, dtype, int)
  532. object PyArray_FromInterface (object)
  533. object PyArray_FromStructInterface (object)
  534. #object PyArray_FromArrayAttr (object, dtype, object)
  535. #NPY_SCALARKIND PyArray_ScalarKind (int, ndarray*)
  536. int PyArray_CanCoerceScalar (int, int, NPY_SCALARKIND)
  537. object PyArray_NewFlagsObject (object)
  538. npy_bool PyArray_CanCastScalar (type, type)
  539. #int PyArray_CompareUCS4 (npy_ucs4 *, npy_ucs4 *, register size_t)
  540. int PyArray_RemoveSmallest (broadcast) except -1
  541. int PyArray_ElementStrides (object)
  542. void PyArray_Item_INCREF (char *, dtype) except *
  543. void PyArray_Item_XDECREF (char *, dtype) except *
  544. object PyArray_FieldNames (object)
  545. object PyArray_Transpose (ndarray, PyArray_Dims *)
  546. object PyArray_TakeFrom (ndarray, object, int, ndarray, NPY_CLIPMODE)
  547. object PyArray_PutTo (ndarray, object, object, NPY_CLIPMODE)
  548. object PyArray_PutMask (ndarray, object, object)
  549. object PyArray_Repeat (ndarray, object, int)
  550. object PyArray_Choose (ndarray, object, ndarray, NPY_CLIPMODE)
  551. int PyArray_Sort (ndarray, int, NPY_SORTKIND) except -1
  552. object PyArray_ArgSort (ndarray, int, NPY_SORTKIND)
  553. object PyArray_SearchSorted (ndarray, object, NPY_SEARCHSIDE, PyObject *)
  554. object PyArray_ArgMax (ndarray, int, ndarray)
  555. object PyArray_ArgMin (ndarray, int, ndarray)
  556. object PyArray_Reshape (ndarray, object)
  557. object PyArray_Newshape (ndarray, PyArray_Dims *, NPY_ORDER)
  558. object PyArray_Squeeze (ndarray)
  559. #object PyArray_View (ndarray, dtype, type)
  560. object PyArray_SwapAxes (ndarray, int, int)
  561. object PyArray_Max (ndarray, int, ndarray)
  562. object PyArray_Min (ndarray, int, ndarray)
  563. object PyArray_Ptp (ndarray, int, ndarray)
  564. object PyArray_Mean (ndarray, int, int, ndarray)
  565. object PyArray_Trace (ndarray, int, int, int, int, ndarray)
  566. object PyArray_Diagonal (ndarray, int, int, int)
  567. object PyArray_Clip (ndarray, object, object, ndarray)
  568. object PyArray_Conjugate (ndarray, ndarray)
  569. object PyArray_Nonzero (ndarray)
  570. object PyArray_Std (ndarray, int, int, ndarray, int)
  571. object PyArray_Sum (ndarray, int, int, ndarray)
  572. object PyArray_CumSum (ndarray, int, int, ndarray)
  573. object PyArray_Prod (ndarray, int, int, ndarray)
  574. object PyArray_CumProd (ndarray, int, int, ndarray)
  575. object PyArray_All (ndarray, int, ndarray)
  576. object PyArray_Any (ndarray, int, ndarray)
  577. object PyArray_Compress (ndarray, object, int, ndarray)
  578. object PyArray_Flatten (ndarray, NPY_ORDER)
  579. object PyArray_Ravel (ndarray, NPY_ORDER)
  580. npy_intp PyArray_MultiplyList (npy_intp *, int)
  581. int PyArray_MultiplyIntList (int *, int)
  582. void * PyArray_GetPtr (ndarray, npy_intp*)
  583. int PyArray_CompareLists (npy_intp *, npy_intp *, int)
  584. #int PyArray_AsCArray (object*, void *, npy_intp *, int, dtype)
  585. #int PyArray_As1D (object*, char **, int *, int)
  586. #int PyArray_As2D (object*, char ***, int *, int *, int)
  587. int PyArray_Free (object, void *)
  588. #int PyArray_Converter (object, object*)
  589. int PyArray_IntpFromSequence (object, npy_intp *, int) except -1
  590. object PyArray_Concatenate (object, int)
  591. object PyArray_InnerProduct (object, object)
  592. object PyArray_MatrixProduct (object, object)
  593. object PyArray_CopyAndTranspose (object)
  594. object PyArray_Correlate (object, object, int)
  595. int PyArray_TypestrConvert (int, int)
  596. #int PyArray_DescrConverter (object, dtype*) except 0
  597. #int PyArray_DescrConverter2 (object, dtype*) except 0
  598. int PyArray_IntpConverter (object, PyArray_Dims *) except 0
  599. #int PyArray_BufferConverter (object, chunk) except 0
  600. int PyArray_AxisConverter (object, int *) except 0
  601. int PyArray_BoolConverter (object, npy_bool *) except 0
  602. int PyArray_ByteorderConverter (object, char *) except 0
  603. int PyArray_OrderConverter (object, NPY_ORDER *) except 0
  604. unsigned char PyArray_EquivTypes (dtype, dtype) # clears errors
  605. #object PyArray_Zeros (int, npy_intp *, dtype, int)
  606. #object PyArray_Empty (int, npy_intp *, dtype, int)
  607. object PyArray_Where (object, object, object)
  608. object PyArray_Arange (double, double, double, int)
  609. #object PyArray_ArangeObj (object, object, object, dtype)
  610. int PyArray_SortkindConverter (object, NPY_SORTKIND *) except 0
  611. object PyArray_LexSort (object, int)
  612. object PyArray_Round (ndarray, int, ndarray)
  613. unsigned char PyArray_EquivTypenums (int, int)
  614. int PyArray_RegisterDataType (dtype) except -1
  615. int PyArray_RegisterCastFunc (dtype, int, PyArray_VectorUnaryFunc *) except -1
  616. int PyArray_RegisterCanCast (dtype, int, NPY_SCALARKIND) except -1
  617. #void PyArray_InitArrFuncs (PyArray_ArrFuncs *)
  618. object PyArray_IntTupleFromIntp (int, npy_intp *)
  619. int PyArray_TypeNumFromName (char *)
  620. int PyArray_ClipmodeConverter (object, NPY_CLIPMODE *) except 0
  621. #int PyArray_OutputConverter (object, ndarray*) except 0
  622. object PyArray_BroadcastToShape (object, npy_intp *, int)
  623. void _PyArray_SigintHandler (int)
  624. void* _PyArray_GetSigintBuf ()
  625. #int PyArray_DescrAlignConverter (object, dtype*) except 0
  626. #int PyArray_DescrAlignConverter2 (object, dtype*) except 0
  627. int PyArray_SearchsideConverter (object, void *) except 0
  628. object PyArray_CheckAxis (ndarray, int *, int)
  629. npy_intp PyArray_OverflowMultiplyList (npy_intp *, int)
  630. int PyArray_CompareString (char *, char *, size_t)
  631. int PyArray_SetBaseObject(ndarray, base) except -1 # NOTE: steals a reference to base! Use "set_array_base()" instead.
  632. # Typedefs that matches the runtime dtype objects in
  633. # the numpy module.
  634. # The ones that are commented out needs an IFDEF function
  635. # in Cython to enable them only on the right systems.
  636. ctypedef npy_int8 int8_t
  637. ctypedef npy_int16 int16_t
  638. ctypedef npy_int32 int32_t
  639. ctypedef npy_int64 int64_t
  640. #ctypedef npy_int96 int96_t
  641. #ctypedef npy_int128 int128_t
  642. ctypedef npy_uint8 uint8_t
  643. ctypedef npy_uint16 uint16_t
  644. ctypedef npy_uint32 uint32_t
  645. ctypedef npy_uint64 uint64_t
  646. #ctypedef npy_uint96 uint96_t
  647. #ctypedef npy_uint128 uint128_t
  648. ctypedef npy_float32 float32_t
  649. ctypedef npy_float64 float64_t
  650. #ctypedef npy_float80 float80_t
  651. #ctypedef npy_float128 float128_t
  652. ctypedef float complex complex64_t
  653. ctypedef double complex complex128_t
  654. # The int types are mapped a bit surprising --
  655. # numpy.int corresponds to 'l' and numpy.long to 'q'
  656. ctypedef npy_long int_t
  657. ctypedef npy_longlong longlong_t
  658. ctypedef npy_ulong uint_t
  659. ctypedef npy_ulonglong ulonglong_t
  660. ctypedef npy_intp intp_t
  661. ctypedef npy_uintp uintp_t
  662. ctypedef npy_double float_t
  663. ctypedef npy_double double_t
  664. ctypedef npy_longdouble longdouble_t
  665. ctypedef npy_cfloat cfloat_t
  666. ctypedef npy_cdouble cdouble_t
  667. ctypedef npy_clongdouble clongdouble_t
  668. ctypedef npy_cdouble complex_t
  669. cdef inline object PyArray_MultiIterNew1(a):
  670. return PyArray_MultiIterNew(1, <void*>a)
  671. cdef inline object PyArray_MultiIterNew2(a, b):
  672. return PyArray_MultiIterNew(2, <void*>a, <void*>b)
  673. cdef inline object PyArray_MultiIterNew3(a, b, c):
  674. return PyArray_MultiIterNew(3, <void*>a, <void*>b, <void*> c)
  675. cdef inline object PyArray_MultiIterNew4(a, b, c, d):
  676. return PyArray_MultiIterNew(4, <void*>a, <void*>b, <void*>c, <void*> d)
  677. cdef inline object PyArray_MultiIterNew5(a, b, c, d, e):
  678. return PyArray_MultiIterNew(5, <void*>a, <void*>b, <void*>c, <void*> d, <void*> e)
  679. cdef inline tuple PyDataType_SHAPE(dtype d):
  680. if PyDataType_HASSUBARRAY(d):
  681. return <tuple>d.subarray.shape
  682. else:
  683. return ()
  684. cdef extern from "numpy/ndarrayobject.h":
  685. PyTypeObject PyTimedeltaArrType_Type
  686. PyTypeObject PyDatetimeArrType_Type
  687. ctypedef int64_t npy_timedelta
  688. ctypedef int64_t npy_datetime
  689. cdef extern from "numpy/ndarraytypes.h":
  690. ctypedef struct PyArray_DatetimeMetaData:
  691. NPY_DATETIMEUNIT base
  692. int64_t num
  693. cdef extern from "numpy/arrayscalars.h":
  694. # abstract types
  695. ctypedef class numpy.generic [object PyObject]:
  696. pass
  697. ctypedef class numpy.number [object PyObject]:
  698. pass
  699. ctypedef class numpy.integer [object PyObject]:
  700. pass
  701. ctypedef class numpy.signedinteger [object PyObject]:
  702. pass
  703. ctypedef class numpy.unsignedinteger [object PyObject]:
  704. pass
  705. ctypedef class numpy.inexact [object PyObject]:
  706. pass
  707. ctypedef class numpy.floating [object PyObject]:
  708. pass
  709. ctypedef class numpy.complexfloating [object PyObject]:
  710. pass
  711. ctypedef class numpy.flexible [object PyObject]:
  712. pass
  713. ctypedef class numpy.character [object PyObject]:
  714. pass
  715. ctypedef struct PyDatetimeScalarObject:
  716. # PyObject_HEAD
  717. npy_datetime obval
  718. PyArray_DatetimeMetaData obmeta
  719. ctypedef struct PyTimedeltaScalarObject:
  720. # PyObject_HEAD
  721. npy_timedelta obval
  722. PyArray_DatetimeMetaData obmeta
  723. ctypedef enum NPY_DATETIMEUNIT:
  724. NPY_FR_Y
  725. NPY_FR_M
  726. NPY_FR_W
  727. NPY_FR_D
  728. NPY_FR_B
  729. NPY_FR_h
  730. NPY_FR_m
  731. NPY_FR_s
  732. NPY_FR_ms
  733. NPY_FR_us
  734. NPY_FR_ns
  735. NPY_FR_ps
  736. NPY_FR_fs
  737. NPY_FR_as
  738. NPY_FR_GENERIC
  739. #
  740. # ufunc API
  741. #
  742. cdef extern from "numpy/ufuncobject.h":
  743. ctypedef void (*PyUFuncGenericFunction) (char **, npy_intp *, npy_intp *, void *)
  744. ctypedef class numpy.ufunc [object PyUFuncObject, check_size ignore]:
  745. cdef:
  746. int nin, nout, nargs
  747. int identity
  748. PyUFuncGenericFunction *functions
  749. void **data
  750. int ntypes
  751. int check_return
  752. char *name
  753. char *types
  754. char *doc
  755. void *ptr
  756. PyObject *obj
  757. PyObject *userloops
  758. cdef enum:
  759. PyUFunc_Zero
  760. PyUFunc_One
  761. PyUFunc_None
  762. UFUNC_ERR_IGNORE
  763. UFUNC_ERR_WARN
  764. UFUNC_ERR_RAISE
  765. UFUNC_ERR_CALL
  766. UFUNC_ERR_PRINT
  767. UFUNC_ERR_LOG
  768. UFUNC_MASK_DIVIDEBYZERO
  769. UFUNC_MASK_OVERFLOW
  770. UFUNC_MASK_UNDERFLOW
  771. UFUNC_MASK_INVALID
  772. UFUNC_SHIFT_DIVIDEBYZERO
  773. UFUNC_SHIFT_OVERFLOW
  774. UFUNC_SHIFT_UNDERFLOW
  775. UFUNC_SHIFT_INVALID
  776. UFUNC_FPE_DIVIDEBYZERO
  777. UFUNC_FPE_OVERFLOW
  778. UFUNC_FPE_UNDERFLOW
  779. UFUNC_FPE_INVALID
  780. UFUNC_ERR_DEFAULT
  781. UFUNC_ERR_DEFAULT2
  782. object PyUFunc_FromFuncAndData(PyUFuncGenericFunction *,
  783. void **, char *, int, int, int, int, char *, char *, int)
  784. int PyUFunc_RegisterLoopForType(ufunc, int,
  785. PyUFuncGenericFunction, int *, void *) except -1
  786. void PyUFunc_f_f_As_d_d \
  787. (char **, npy_intp *, npy_intp *, void *)
  788. void PyUFunc_d_d \
  789. (char **, npy_intp *, npy_intp *, void *)
  790. void PyUFunc_f_f \
  791. (char **, npy_intp *, npy_intp *, void *)
  792. void PyUFunc_g_g \
  793. (char **, npy_intp *, npy_intp *, void *)
  794. void PyUFunc_F_F_As_D_D \
  795. (char **, npy_intp *, npy_intp *, void *)
  796. void PyUFunc_F_F \
  797. (char **, npy_intp *, npy_intp *, void *)
  798. void PyUFunc_D_D \
  799. (char **, npy_intp *, npy_intp *, void *)
  800. void PyUFunc_G_G \
  801. (char **, npy_intp *, npy_intp *, void *)
  802. void PyUFunc_O_O \
  803. (char **, npy_intp *, npy_intp *, void *)
  804. void PyUFunc_ff_f_As_dd_d \
  805. (char **, npy_intp *, npy_intp *, void *)
  806. void PyUFunc_ff_f \
  807. (char **, npy_intp *, npy_intp *, void *)
  808. void PyUFunc_dd_d \
  809. (char **, npy_intp *, npy_intp *, void *)
  810. void PyUFunc_gg_g \
  811. (char **, npy_intp *, npy_intp *, void *)
  812. void PyUFunc_FF_F_As_DD_D \
  813. (char **, npy_intp *, npy_intp *, void *)
  814. void PyUFunc_DD_D \
  815. (char **, npy_intp *, npy_intp *, void *)
  816. void PyUFunc_FF_F \
  817. (char **, npy_intp *, npy_intp *, void *)
  818. void PyUFunc_GG_G \
  819. (char **, npy_intp *, npy_intp *, void *)
  820. void PyUFunc_OO_O \
  821. (char **, npy_intp *, npy_intp *, void *)
  822. void PyUFunc_O_O_method \
  823. (char **, npy_intp *, npy_intp *, void *)
  824. void PyUFunc_OO_O_method \
  825. (char **, npy_intp *, npy_intp *, void *)
  826. void PyUFunc_On_Om \
  827. (char **, npy_intp *, npy_intp *, void *)
  828. int PyUFunc_GetPyValues \
  829. (char *, int *, int *, PyObject **)
  830. int PyUFunc_checkfperr \
  831. (int, PyObject *, int *)
  832. void PyUFunc_clearfperr()
  833. int PyUFunc_getfperr()
  834. int PyUFunc_handlefperr \
  835. (int, PyObject *, int, int *) except -1
  836. int PyUFunc_ReplaceLoopBySignature \
  837. (ufunc, PyUFuncGenericFunction, int *, PyUFuncGenericFunction *)
  838. object PyUFunc_FromFuncAndDataAndSignature \
  839. (PyUFuncGenericFunction *, void **, char *, int, int, int,
  840. int, char *, char *, int, char *)
  841. int _import_umath() except -1
  842. cdef inline void set_array_base(ndarray arr, object base):
  843. Py_INCREF(base) # important to do this before stealing the reference below!
  844. PyArray_SetBaseObject(arr, base)
  845. cdef inline object get_array_base(ndarray arr):
  846. base = PyArray_BASE(arr)
  847. if base is NULL:
  848. return None
  849. return <object>base
  850. # Versions of the import_* functions which are more suitable for
  851. # Cython code.
  852. cdef inline int import_array() except -1:
  853. try:
  854. __pyx_import_array()
  855. except Exception:
  856. raise ImportError("numpy.core.multiarray failed to import")
  857. cdef inline int import_umath() except -1:
  858. try:
  859. _import_umath()
  860. except Exception:
  861. raise ImportError("numpy.core.umath failed to import")
  862. cdef inline int import_ufunc() except -1:
  863. try:
  864. _import_umath()
  865. except Exception:
  866. raise ImportError("numpy.core.umath failed to import")
  867. cdef inline bint is_timedelta64_object(object obj):
  868. """
  869. Cython equivalent of `isinstance(obj, np.timedelta64)`
  870. Parameters
  871. ----------
  872. obj : object
  873. Returns
  874. -------
  875. bool
  876. """
  877. return PyObject_TypeCheck(obj, &PyTimedeltaArrType_Type)
  878. cdef inline bint is_datetime64_object(object obj):
  879. """
  880. Cython equivalent of `isinstance(obj, np.datetime64)`
  881. Parameters
  882. ----------
  883. obj : object
  884. Returns
  885. -------
  886. bool
  887. """
  888. return PyObject_TypeCheck(obj, &PyDatetimeArrType_Type)
  889. cdef inline npy_datetime get_datetime64_value(object obj) nogil:
  890. """
  891. returns the int64 value underlying scalar numpy datetime64 object
  892. Note that to interpret this as a datetime, the corresponding unit is
  893. also needed. That can be found using `get_datetime64_unit`.
  894. """
  895. return (<PyDatetimeScalarObject*>obj).obval
  896. cdef inline npy_timedelta get_timedelta64_value(object obj) nogil:
  897. """
  898. returns the int64 value underlying scalar numpy timedelta64 object
  899. """
  900. return (<PyTimedeltaScalarObject*>obj).obval
  901. cdef inline NPY_DATETIMEUNIT get_datetime64_unit(object obj) nogil:
  902. """
  903. returns the unit part of the dtype for a numpy datetime64 object.
  904. """
  905. return <NPY_DATETIMEUNIT>(<PyDatetimeScalarObject*>obj).obmeta.base