From af2c4cffe2119bf377bd1a8cb287c5ce9bbb842b Mon Sep 17 00:00:00 2001 From: Sebastian Berg Date: Thu, 9 Nov 2023 11:57:30 +0100 Subject: [PATCH 1/2] BUG: Avoid intp conversion regression in Cython 3 (backport) This is the minimal backport version of gh-25094, which simply aligns the Cython 2 and Cython 3 definitions. --- numpy/__init__.cython-30.pxd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/numpy/__init__.cython-30.pxd b/numpy/__init__.cython-30.pxd index 3ad186e40926..1409514f7a84 100644 --- a/numpy/__init__.cython-30.pxd +++ b/numpy/__init__.cython-30.pxd @@ -21,7 +21,7 @@ cdef extern from *: cdef extern from "Python.h": - ctypedef Py_ssize_t Py_intptr_t + ctypedef int Py_intptr_t cdef extern from "numpy/arrayobject.h": ctypedef Py_intptr_t npy_intp From 09eb7d5237346033ad0953499ed112394c276662 Mon Sep 17 00:00:00 2001 From: Sebastian Berg Date: Thu, 9 Nov 2023 12:03:13 +0100 Subject: [PATCH 2/2] TST: Check that Cython intp conversion uses normal integer rules --- numpy/core/tests/examples/cython/checks.pyx | 3 +++ numpy/core/tests/test_cython.py | 11 +++++++++++ 2 files changed, 14 insertions(+) diff --git a/numpy/core/tests/examples/cython/checks.pyx b/numpy/core/tests/examples/cython/checks.pyx index e41c6d657351..c5529ee8fcaf 100644 --- a/numpy/core/tests/examples/cython/checks.pyx +++ b/numpy/core/tests/examples/cython/checks.pyx @@ -30,3 +30,6 @@ def get_dt64_unit(obj): def is_integer(obj): return isinstance(obj, (cnp.integer, int)) + +def conv_intp(cnp.intp_t val): + return val diff --git a/numpy/core/tests/test_cython.py b/numpy/core/tests/test_cython.py index 29473f5ba424..99dd57e4c62d 100644 --- a/numpy/core/tests/test_cython.py +++ b/numpy/core/tests/test_cython.py @@ -122,3 +122,14 @@ def test_abstract_scalars(install_temp): assert checks.is_integer(1) assert checks.is_integer(np.int8(1)) assert checks.is_integer(np.uint64(1)) + +def test_conv_intp(install_temp): + import checks + + class myint: + def __int__(self): + return 3 + + # These conversion passes via `__int__`, not `__index__`: + assert checks.conv_intp(3.) == 3 + assert checks.conv_intp(myint()) == 3