1
- # Copyright (c) 2020-2024 , NVIDIA CORPORATION. All rights reserved.
1
+ # Copyright (c) 2020-2025 , NVIDIA CORPORATION. All rights reserved.
2
2
# See file LICENSE for terms.
3
3
4
4
# cython: language_level=3
5
5
6
6
7
- from cpython.buffer cimport PyBuffer_IsContiguous
7
+ from cpython.buffer cimport (
8
+ PyBUF_FULL_RO,
9
+ PyBuffer_IsContiguous,
10
+ PyBuffer_Release,
11
+ PyObject_GetBuffer,
12
+ )
8
13
from cpython.mem cimport PyMem_Free, PyMem_Malloc
9
- from cpython.memoryview cimport PyMemoryView_FromObject, PyMemoryView_GET_BUFFER
10
14
from cpython.ref cimport Py_INCREF
11
- from cpython.tuple cimport PyTuple_New, PyTuple_SET_ITEM
15
+ from cpython.tuple cimport PyTuple_New, PyTuple_SetItem
12
16
from cython cimport auto_pickle, boundscheck, initializedcheck, nonecheck, wraparound
13
17
from cython.view cimport array
14
18
from libc.stdint cimport uintptr_t
@@ -75,7 +79,7 @@ cdef class Array:
75
79
def __cinit__ (self , obj ):
76
80
cdef dict iface = getattr (obj, " __cuda_array_interface__" , None )
77
81
self .cuda = (iface is not None )
78
- cdef const Py_buffer* pybuf
82
+ cdef Py_buffer pybuf
79
83
cdef str typestr
80
84
cdef tuple data, shape, strides
81
85
cdef Py_ssize_t i
@@ -125,8 +129,7 @@ cdef class Array:
125
129
self .shape_mv = None
126
130
self .strides_mv = None
127
131
else :
128
- mv = PyMemoryView_FromObject(obj)
129
- pybuf = PyMemoryView_GET_BUFFER(mv)
132
+ PyObject_GetBuffer(obj, & pybuf, PyBUF_FULL_RO)
130
133
131
134
if pybuf.suboffsets != NULL :
132
135
raise NotImplementedError (" Suboffsets are not supported" )
@@ -144,7 +147,7 @@ cdef class Array:
144
147
pybuf.shape,
145
148
self .ndim * sizeof(Py_ssize_t)
146
149
)
147
- if not PyBuffer_IsContiguous(pybuf, b" C" ):
150
+ if not PyBuffer_IsContiguous(& pybuf, b" C" ):
148
151
self .strides_mv = new_Py_ssize_t_array(self .ndim)
149
152
memcpy(
150
153
& self .strides_mv[0 ],
@@ -156,6 +159,7 @@ cdef class Array:
156
159
else :
157
160
self .shape_mv = None
158
161
self .strides_mv = None
162
+ PyBuffer_Release(& pybuf)
159
163
160
164
cpdef bint _c_contiguous(self ):
161
165
return _c_contiguous(
@@ -203,7 +207,7 @@ cdef class Array:
203
207
for i in range (self .ndim):
204
208
o = self .shape_mv[i]
205
209
Py_INCREF(o)
206
- PyTuple_SET_ITEM (shape, i, o)
210
+ PyTuple_SetItem (shape, i, o)
207
211
return shape
208
212
209
213
@property
@@ -219,13 +223,13 @@ cdef class Array:
219
223
for i from self .ndim > i >= 0 by 1 :
220
224
o = self .strides_mv[i]
221
225
Py_INCREF(o)
222
- PyTuple_SET_ITEM (strides, i, o)
226
+ PyTuple_SetItem (strides, i, o)
223
227
else :
224
228
s = self .itemsize
225
229
for i from self .ndim > i >= 0 by 1 :
226
230
o = s
227
231
Py_INCREF(o)
228
- PyTuple_SET_ITEM (strides, i, o)
232
+ PyTuple_SetItem (strides, i, o)
229
233
s *= self .shape_mv[i]
230
234
return strides
231
235
0 commit comments