1
1
import numpy as np
2
+ from cpython.ref cimport Py_INCREF
2
3
from libc.stdint cimport uintptr_t
3
4
from numpy cimport (
4
5
NPY_ARRAY_F_CONTIGUOUS,
5
6
NPY_ARRAY_OWNDATA,
6
7
NPY_ARRAY_WRITEABLE,
7
- PyArray_New,
8
8
import_array,
9
9
ndarray,
10
10
npy_intp,
11
+ dtype as dtype_t,
11
12
)
12
13
13
14
import_array()
14
15
15
- cpdef int call_gxb_init(ffi, lib, int mode):
16
+ cpdef int call_gxb_init(object ffi, object lib, int mode):
16
17
# We need to call `GxB_init`, but we didn't compile Cython against GraphBLAS. So, we get it from cffi.
17
18
# Step 1: ffi.addressof(lib, "GxB_init")
18
19
# 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):
29
30
return func(< GrB_Mode> mode, PyDataMem_NEW, PyDataMem_NEW_ZEROED, PyDataMem_RENEW, PyDataMem_FREE)
30
31
31
32
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):
33
34
cdef:
34
35
npy_intp dims = size
35
36
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
+ )
40
42
PyArray_ENABLEFLAGS(array, NPY_ARRAY_OWNDATA)
41
- if dtype.num == 20 : # void type, so most likely a UDT
42
- array = array.view(dtype)
43
43
return array
44
44
45
45
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
+ ):
47
49
cdef:
48
50
size_t size = nrows * ncols
49
51
ndarray array
@@ -56,13 +58,10 @@ cpdef ndarray claim_buffer_2d(ffi, cdata, size_t cdata_size, size_t nrows, size_
56
58
dims[1 ] = ncols
57
59
if not is_c_order:
58
60
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
62
63
)
63
64
PyArray_ENABLEFLAGS(array, NPY_ARRAY_OWNDATA)
64
- if dtype.num == 20 : # void type, so most likely a UDT
65
- array = array.view(dtype)
66
65
elif cdata_size > size: # pragma: no cover
67
66
array = claim_buffer(ffi, cdata, cdata_size, dtype)
68
67
if is_c_order:
0 commit comments