Skip to content

Commit

Permalink
Merge pull request #25095 from seberg/backport-cy-fix
Browse files Browse the repository at this point in the history
BUG: Avoid intp conversion regression in Cython 3 (backport)
  • Loading branch information
charris committed Nov 10, 2023
2 parents 55d3f26 + 09eb7d5 commit 86c2863
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion numpy/__init__.cython-30.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions numpy/core/tests/examples/cython/checks.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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
11 changes: 11 additions & 0 deletions numpy/core/tests/test_cython.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 86c2863

Please sign in to comment.