11import numpy as np
2+ from cpython.ref cimport Py_INCREF
23from libc.stdint cimport uintptr_t
34from numpy cimport (
45 NPY_ARRAY_F_CONTIGUOUS,
56 NPY_ARRAY_OWNDATA,
67 NPY_ARRAY_WRITEABLE,
7- PyArray_New,
88 import_array,
99 ndarray,
1010 npy_intp,
11+ dtype as dtype_t,
1112)
1213
1314import_array()
1415
15- cpdef int call_gxb_init(ffi, lib, int mode):
16+ cpdef int call_gxb_init(object ffi, object lib, int mode):
1617 # We need to call `GxB_init`, but we didn't compile Cython against GraphBLAS. So, we get it from cffi.
1718 # Step 1: ffi.addressof(lib, "GxB_init")
1819 # Return type: cffi.cdata object of a function pointer. Can't cast to int.
@@ -29,21 +30,22 @@ cpdef int call_gxb_init(ffi, lib, int mode):
2930 return func(< GrB_Mode> mode, PyDataMem_NEW, PyDataMem_NEW_ZEROED, PyDataMem_RENEW, PyDataMem_FREE)
3031
3132
32- cpdef ndarray claim_buffer(ffi, cdata, size_t size, dtype):
33+ cpdef ndarray claim_buffer(object ffi, object cdata, size_t size, dtype_t dtype):
3334 cdef:
3435 npy_intp dims = size
3536 uintptr_t ptr = int (ffi.cast(" uintptr_t" , cdata))
36- ndarray array = PyArray_New(
37- ndarray, 1 , & dims, dtype.num, NULL , < void * > ptr, dtype.itemsize,
38- NPY_ARRAY_WRITEABLE, < object > NULL
39- )
37+ ndarray array
38+ Py_INCREF(dtype)
39+ array = PyArray_NewFromDescr(
40+ ndarray, dtype, 1 , & dims, NULL , < void * > ptr, NPY_ARRAY_WRITEABLE, < object > NULL
41+ )
4042 PyArray_ENABLEFLAGS(array, NPY_ARRAY_OWNDATA)
41- if dtype.num == 20 : # void type, so most likely a UDT
42- array = array.view(dtype)
4343 return array
4444
4545
46- cpdef ndarray claim_buffer_2d(ffi, cdata, size_t cdata_size, size_t nrows, size_t ncols, dtype, bint is_c_order):
46+ cpdef ndarray claim_buffer_2d(
47+ object ffi, object cdata, size_t cdata_size, size_t nrows, size_t ncols, dtype_t dtype, bint is_c_order
48+ ):
4749 cdef:
4850 size_t size = nrows * ncols
4951 ndarray array
@@ -56,13 +58,10 @@ cpdef ndarray claim_buffer_2d(ffi, cdata, size_t cdata_size, size_t nrows, size_
5658 dims[1 ] = ncols
5759 if not is_c_order:
5860 flags |= NPY_ARRAY_F_CONTIGUOUS
59- array = PyArray_New(
60- ndarray, 2 , dims, dtype.num, NULL , < void * > ptr, dtype.itemsize,
61- flags, < object > NULL
61+ array = PyArray_NewFromDescr(
62+ ndarray, dtype, 2 , dims, NULL , < void * > ptr, flags, < object > NULL
6263 )
6364 PyArray_ENABLEFLAGS(array, NPY_ARRAY_OWNDATA)
64- if dtype.num == 20 : # void type, so most likely a UDT
65- array = array.view(dtype)
6665 elif cdata_size > size: # pragma: no cover
6766 array = claim_buffer(ffi, cdata, cdata_size, dtype)
6867 if is_c_order:
0 commit comments