Skip to content

Commit

Permalink
Use Cython's array to back Py_ssize_t[::1]
Browse files Browse the repository at this point in the history
In `Array`, `Py_ssize_t[::1]` objects are currently backed by CPython
`array`'s with some internal bits expressed in Cython. However these are
not compatible with Python's Limited API and Stable ABI. To address
that, switch to Cython's own`array` type. As this is baked into Cython
and doesn't use anything special, it is compatible with Python's Limited
API and Stable ABI.

https://cython.readthedocs.io/en/latest/src/userguide/memoryviews.html#cython-arrays
  • Loading branch information
jakirkham committed Oct 22, 2024
1 parent 36c5c27 commit 35da294
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions python/kvikio/kvikio/_lib/arr.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
# cython: language_level=3


from cpython.array cimport array, newarrayobject
from cpython.buffer cimport PyBuffer_IsContiguous
from cpython.mem cimport PyMem_Malloc, PyMem_Free
from cpython.memoryview cimport PyMemoryView_FromObject, PyMemoryView_GET_BUFFER
from cpython.object cimport PyObject
from cpython.ref cimport Py_INCREF
from cpython.tuple cimport PyTuple_New, PyTuple_SET_ITEM
from cython cimport auto_pickle, boundscheck, initializedcheck, nonecheck, wraparound
from cython.view cimport array
from libc.stdint cimport uintptr_t
from libc.string cimport memcpy

Expand Down Expand Up @@ -53,13 +54,14 @@ cdef dict itemsize_mapping = {
}


cdef array array_Py_ssize_t = array("q")
cdef sizeof_Py_ssize_t = sizeof(Py_ssize_t)


cdef inline Py_ssize_t[::1] new_Py_ssize_t_array(Py_ssize_t n):
return newarrayobject(
(<PyObject*>array_Py_ssize_t).ob_type, n, array_Py_ssize_t.ob_descr
)
cdef Py_ssize_t[::1] new_Py_ssize_t_array(Py_ssize_t n):
cdef array a = array((n,), sizeof_Py_ssize_t, b"q", "c", False)
a.data = <char*>PyMem_Malloc(n * sizeof(Py_ssize_t))
a.callback_free_data = PyMem_Free
return a


@auto_pickle(False)
Expand Down

0 comments on commit 35da294

Please sign in to comment.