summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSebastian Berg <sebastian@sipsolutions.net>2020-09-12 15:36:09 -0500
committerGitHub <noreply@github.com>2020-09-12 15:36:09 -0500
commit8adeca227a39640b0ad4db38ca34a77a0bb50c8f (patch)
treef8cfa27a28eabe4b52ee4465419c391bbc64420d
parentc7d83e7412169399586272176d5ad839b568ba57 (diff)
parent52b4c49ef8ad74a811b9b0e751d913da9d1a5933 (diff)
downloadnumpy-8adeca227a39640b0ad4db38ca34a77a0bb50c8f.tar.gz
Merge pull request #17150 from eric-wieser/cython-isinstance
ENH: Add support for the abstract scalars to cython code
-rw-r--r--numpy/__init__.cython-30.pxd23
-rw-r--r--numpy/__init__.pxd23
-rw-r--r--numpy/core/tests/examples/checks.pyx4
-rw-r--r--numpy/core/tests/test_cython.py8
4 files changed, 58 insertions, 0 deletions
diff --git a/numpy/__init__.cython-30.pxd b/numpy/__init__.cython-30.pxd
index 4d9ec1fed..a2c451bc1 100644
--- a/numpy/__init__.cython-30.pxd
+++ b/numpy/__init__.cython-30.pxd
@@ -808,6 +808,29 @@ cdef extern from "numpy/ndarraytypes.h":
int64_t num
cdef extern from "numpy/arrayscalars.h":
+
+ # abstract types
+ ctypedef class numpy.generic [object PyObject]:
+ pass
+ ctypedef class numpy.number [object PyObject]:
+ pass
+ ctypedef class numpy.integer [object PyObject]:
+ pass
+ ctypedef class numpy.signedinteger [object PyObject]:
+ pass
+ ctypedef class numpy.unsignedinteger [object PyObject]:
+ pass
+ ctypedef class numpy.inexact [object PyObject]:
+ pass
+ ctypedef class numpy.floating [object PyObject]:
+ pass
+ ctypedef class numpy.complexfloating [object PyObject]:
+ pass
+ ctypedef class numpy.flexible [object PyObject]:
+ pass
+ ctypedef class numpy.character [object PyObject]:
+ pass
+
ctypedef struct PyDatetimeScalarObject:
# PyObject_HEAD
npy_datetime obval
diff --git a/numpy/__init__.pxd b/numpy/__init__.pxd
index bf4298e59..fd704b7e3 100644
--- a/numpy/__init__.pxd
+++ b/numpy/__init__.pxd
@@ -766,6 +766,29 @@ cdef extern from "numpy/ndarraytypes.h":
int64_t num
cdef extern from "numpy/arrayscalars.h":
+
+ # abstract types
+ ctypedef class numpy.generic [object PyObject]:
+ pass
+ ctypedef class numpy.number [object PyObject]:
+ pass
+ ctypedef class numpy.integer [object PyObject]:
+ pass
+ ctypedef class numpy.signedinteger [object PyObject]:
+ pass
+ ctypedef class numpy.unsignedinteger [object PyObject]:
+ pass
+ ctypedef class numpy.inexact [object PyObject]:
+ pass
+ ctypedef class numpy.floating [object PyObject]:
+ pass
+ ctypedef class numpy.complexfloating [object PyObject]:
+ pass
+ ctypedef class numpy.flexible [object PyObject]:
+ pass
+ ctypedef class numpy.character [object PyObject]:
+ pass
+
ctypedef struct PyDatetimeScalarObject:
# PyObject_HEAD
npy_datetime obval
diff --git a/numpy/core/tests/examples/checks.pyx b/numpy/core/tests/examples/checks.pyx
index ecf0ad3fa..151979db7 100644
--- a/numpy/core/tests/examples/checks.pyx
+++ b/numpy/core/tests/examples/checks.pyx
@@ -24,3 +24,7 @@ def get_td64_value(obj):
def get_dt64_unit(obj):
return cnp.get_datetime64_unit(obj)
+
+
+def is_integer(obj):
+ return isinstance(obj, (cnp.integer, int))
diff --git a/numpy/core/tests/test_cython.py b/numpy/core/tests/test_cython.py
index 63524b269..bfdb692d7 100644
--- a/numpy/core/tests/test_cython.py
+++ b/numpy/core/tests/test_cython.py
@@ -126,3 +126,11 @@ def test_get_datetime64_unit(install_temp):
result = checks.get_dt64_unit(td64)
expected = 5
assert result == expected
+
+
+def test_abstract_scalars(install_temp):
+ import checks
+
+ assert checks.is_integer(1)
+ assert checks.is_integer(np.int8(1))
+ assert checks.is_integer(np.uint64(1))