Skip to content

Commit d06f360

Browse files
authored
Remove usage of APIs outside the limited API (#692)
I hadn't revisited kvikio's usage of CPython's functions outside the limited API since #504, and while reviving rapidsai/devcontainers#278 I found that these usages persist and are easy enough to remove. Authors: - Vyas Ramasubramani (https://github.com/vyasr) Approvers: - Mads R. B. Kristensen (https://github.com/madsbk) URL: #692
1 parent 39853dd commit d06f360

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

python/kvikio/kvikio/_lib/arr.pyx

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
1-
# Copyright (c) 2020-2024, NVIDIA CORPORATION. All rights reserved.
1+
# Copyright (c) 2020-2025, NVIDIA CORPORATION. All rights reserved.
22
# See file LICENSE for terms.
33

44
# cython: language_level=3
55

66

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+
)
813
from cpython.mem cimport PyMem_Free, PyMem_Malloc
9-
from cpython.memoryview cimport PyMemoryView_FromObject, PyMemoryView_GET_BUFFER
1014
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
1216
from cython cimport auto_pickle, boundscheck, initializedcheck, nonecheck, wraparound
1317
from cython.view cimport array
1418
from libc.stdint cimport uintptr_t
@@ -75,7 +79,7 @@ cdef class Array:
7579
def __cinit__(self, obj):
7680
cdef dict iface = getattr(obj, "__cuda_array_interface__", None)
7781
self.cuda = (iface is not None)
78-
cdef const Py_buffer* pybuf
82+
cdef Py_buffer pybuf
7983
cdef str typestr
8084
cdef tuple data, shape, strides
8185
cdef Py_ssize_t i
@@ -125,8 +129,7 @@ cdef class Array:
125129
self.shape_mv = None
126130
self.strides_mv = None
127131
else:
128-
mv = PyMemoryView_FromObject(obj)
129-
pybuf = PyMemoryView_GET_BUFFER(mv)
132+
PyObject_GetBuffer(obj, &pybuf, PyBUF_FULL_RO)
130133

131134
if pybuf.suboffsets != NULL:
132135
raise NotImplementedError("Suboffsets are not supported")
@@ -144,7 +147,7 @@ cdef class Array:
144147
pybuf.shape,
145148
self.ndim * sizeof(Py_ssize_t)
146149
)
147-
if not PyBuffer_IsContiguous(pybuf, b"C"):
150+
if not PyBuffer_IsContiguous(&pybuf, b"C"):
148151
self.strides_mv = new_Py_ssize_t_array(self.ndim)
149152
memcpy(
150153
&self.strides_mv[0],
@@ -156,6 +159,7 @@ cdef class Array:
156159
else:
157160
self.shape_mv = None
158161
self.strides_mv = None
162+
PyBuffer_Release(&pybuf)
159163

160164
cpdef bint _c_contiguous(self):
161165
return _c_contiguous(
@@ -203,7 +207,7 @@ cdef class Array:
203207
for i in range(self.ndim):
204208
o = self.shape_mv[i]
205209
Py_INCREF(o)
206-
PyTuple_SET_ITEM(shape, i, o)
210+
PyTuple_SetItem(shape, i, o)
207211
return shape
208212

209213
@property
@@ -219,13 +223,13 @@ cdef class Array:
219223
for i from self.ndim > i >= 0 by 1:
220224
o = self.strides_mv[i]
221225
Py_INCREF(o)
222-
PyTuple_SET_ITEM(strides, i, o)
226+
PyTuple_SetItem(strides, i, o)
223227
else:
224228
s = self.itemsize
225229
for i from self.ndim > i >= 0 by 1:
226230
o = s
227231
Py_INCREF(o)
228-
PyTuple_SET_ITEM(strides, i, o)
232+
PyTuple_SetItem(strides, i, o)
229233
s *= self.shape_mv[i]
230234
return strides
231235

0 commit comments

Comments
 (0)