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
- PyArray_SimpleNewFromData,
9
8
import_array,
10
9
ndarray,
11
10
npy_intp,
11
+ dtype as dtype_t,
12
12
)
13
13
14
14
import_array()
15
15
16
- cpdef int call_gxb_init(ffi, lib, int mode):
16
+ cpdef int call_gxb_init(object ffi, object lib, int mode):
17
17
# We need to call `GxB_init`, but we didn't compile Cython against GraphBLAS. So, we get it from cffi.
18
18
# Step 1: ffi.addressof(lib, "GxB_init")
19
19
# Return type: cffi.cdata object of a function pointer. Can't cast to int.
@@ -30,32 +30,37 @@ cpdef int call_gxb_init(ffi, lib, int mode):
30
30
return func(< GrB_Mode> mode, PyDataMem_NEW, PyDataMem_NEW_ZEROED, PyDataMem_RENEW, PyDataMem_FREE)
31
31
32
32
33
- 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):
34
34
cdef:
35
35
npy_intp dims = size
36
36
uintptr_t ptr = int (ffi.cast(" uintptr_t" , cdata))
37
- ndarray array = PyArray_SimpleNewFromData(1 , & dims, dtype.num, < void * > ptr)
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
+ )
38
42
PyArray_ENABLEFLAGS(array, NPY_ARRAY_OWNDATA)
39
43
return array
40
44
41
45
42
- 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
+ ):
43
49
cdef:
44
50
size_t size = nrows * ncols
45
51
ndarray array
46
52
uintptr_t ptr
47
53
npy_intp dims[2 ]
54
+ int flags = NPY_ARRAY_WRITEABLE
48
55
if cdata_size == size:
49
56
ptr = int (ffi.cast(" uintptr_t" , cdata))
50
57
dims[0 ] = nrows
51
58
dims[1 ] = ncols
52
- if is_c_order:
53
- array = PyArray_SimpleNewFromData(2 , dims, dtype.num, < void * > ptr)
54
- else :
55
- array = PyArray_New(
56
- ndarray, 2 , dims, dtype.num, NULL , < void * > ptr, - 1 ,
57
- NPY_ARRAY_F_CONTIGUOUS | NPY_ARRAY_WRITEABLE, < object > NULL
58
- )
59
+ if not is_c_order:
60
+ flags |= NPY_ARRAY_F_CONTIGUOUS
61
+ array = PyArray_NewFromDescr(
62
+ ndarray, dtype, 2 , dims, NULL , < void * > ptr, flags, < object > NULL
63
+ )
59
64
PyArray_ENABLEFLAGS(array, NPY_ARRAY_OWNDATA)
60
65
elif cdata_size > size: # pragma: no cover
61
66
array = claim_buffer(ffi, cdata, cdata_size, dtype)
0 commit comments