Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

BUG: Fix return type of NpyIter_GetIterNext in Cython declarations #28582

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 24, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
MAINT: work around cython limitations, add test
  • Loading branch information
ngoldbaum authored and charris committed Mar 24, 2025
commit 48e3a43402c8a3c6e0d5b4761d1fbd197d75bd09
19 changes: 13 additions & 6 deletions 19 numpy/__init__.cython-30.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -858,6 +858,14 @@ cdef extern from "numpy/ndarraytypes.h":
int64_t year
int32_t month, day, hour, min, sec, us, ps, as

# Iterator API added in v1.6
#
# These don't match the definition in the C API because Cython can't wrap
# function pointers that return functions.
# https://github.com/cython/cython/issues/6720
ctypedef int (*NpyIter_IterNextFunc "NpyIter_IterNextFunc *")(NpyIter* it) noexcept nogil
ctypedef void (*NpyIter_GetMultiIndexFunc "NpyIter_GetMultiIndexFunc *")(NpyIter* it, npy_intp* outcoords) noexcept nogil


cdef extern from "numpy/arrayscalars.h":

Expand Down Expand Up @@ -1109,10 +1117,6 @@ cdef inline NPY_DATETIMEUNIT get_datetime64_unit(object obj) noexcept nogil:
return <NPY_DATETIMEUNIT>(<PyDatetimeScalarObject*>obj).obmeta.base


# Iterator API added in v1.6
ctypedef int (*NpyIter_IterNextFunc)(NpyIter* it) noexcept nogil
ctypedef void (*NpyIter_GetMultiIndexFunc)(NpyIter* it, npy_intp* outcoords) noexcept nogil

cdef extern from "numpy/arrayobject.h":

ctypedef struct NpyIter:
Expand Down Expand Up @@ -1230,9 +1234,12 @@ cdef extern from "numpy/arrayobject.h":
npy_intp* outstrides) except NPY_FAIL
npy_bool NpyIter_IsFirstVisit(NpyIter* it, int iop) nogil
# functions for iterating an NpyIter object
#
# These don't match the definition in the C API because Cython can't wrap
# function pointers that return functions.
NpyIter_IterNextFunc NpyIter_GetIterNext(NpyIter* it, char** errmsg) except NULL
NpyIter_GetMultiIndexFunc* NpyIter_GetGetMultiIndex(NpyIter* it,
char** errmsg) except NULL
NpyIter_GetMultiIndexFunc NpyIter_GetGetMultiIndex(NpyIter* it,
char** errmsg) except NULL
char** NpyIter_GetDataPtrArray(NpyIter* it) nogil
char** NpyIter_GetInitialDataPtrArray(NpyIter* it) nogil
npy_intp* NpyIter_GetIndexPtr(NpyIter* it)
Expand Down
16 changes: 11 additions & 5 deletions 16 numpy/__init__.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,13 @@ cdef extern from "numpy/ndarraytypes.h":
int64_t year
int32_t month, day, hour, min, sec, us, ps, as

# Iterator API added in v1.6
#
# These don't match the definition in the C API because Cython can't wrap
# function pointers that return functions.
# https://github.com/cython/cython/issues/6720
ctypedef int (*NpyIter_IterNextFunc "NpyIter_IterNextFunc *")(NpyIter* it) noexcept nogil
ctypedef void (*NpyIter_GetMultiIndexFunc "NpyIter_GetMultiIndexFunc *")(NpyIter* it, npy_intp* outcoords) noexcept nogil

cdef extern from "numpy/arrayscalars.h":

Expand Down Expand Up @@ -1024,10 +1031,6 @@ cdef inline NPY_DATETIMEUNIT get_datetime64_unit(object obj) nogil:
return <NPY_DATETIMEUNIT>(<PyDatetimeScalarObject*>obj).obmeta.base


# Iterator API added in v1.6
ctypedef int (*NpyIter_IterNextFunc)(NpyIter* it) noexcept nogil
ctypedef void (*NpyIter_GetMultiIndexFunc)(NpyIter* it, npy_intp* outcoords) noexcept nogil

cdef extern from "numpy/arrayobject.h":

ctypedef struct NpyIter:
Expand Down Expand Up @@ -1145,7 +1148,10 @@ cdef extern from "numpy/arrayobject.h":
npy_intp* outstrides) except NPY_FAIL
npy_bool NpyIter_IsFirstVisit(NpyIter* it, int iop) nogil
# functions for iterating an NpyIter object
NpyIter_IterNextFunc NpyIter_GetIterNext(NpyIter* it, char** errmsg) except NULL
#
# These don't match the definition in the C API because Cython can't wrap
# function pointers that return functions.
NpyIter_IterNextFunc* NpyIter_GetIterNext(NpyIter* it, char** errmsg) except NULL
NpyIter_GetMultiIndexFunc* NpyIter_GetGetMultiIndex(NpyIter* it,
char** errmsg) except NULL
char** NpyIter_GetDataPtrArray(NpyIter* it) nogil
Expand Down
9 changes: 9 additions & 0 deletions 9 numpy/_core/tests/examples/cython/checks.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,15 @@ def npyiter_has_multi_index(it: "nditer"):
return result


def test_get_multi_index_iter_next(it: "nditer", cnp.ndarray[cnp.float64_t, ndim=2] arr):
cdef cnp.NpyIter* cit = npyiter_from_nditer_obj(it)
cdef cnp.NpyIter_GetMultiIndexFunc get_multi_index = \
cnp.NpyIter_GetGetMultiIndex(cit, NULL)
cdef cnp.NpyIter_IterNextFunc iternext = \
cnp.NpyIter_GetIterNext(cit, NULL)
return 1


def npyiter_has_finished(it: "nditer"):
cdef cnp.NpyIter* cit
try:
Expand Down
1 change: 1 addition & 0 deletions 1 numpy/_core/tests/test_cython.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ def test_npyiter_api(install_temp):
assert checks.get_npyiter_size(it) == it.itersize == np.prod(arr.shape)
assert checks.npyiter_has_multi_index(it) == it.has_multi_index == True
assert checks.get_npyiter_ndim(it) == it.ndim == 2
assert checks.test_get_multi_index_iter_next(it, arr)

arr2 = np.random.rand(2, 1, 2)
it = np.nditer([arr, arr2])
Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.