summaryrefslogtreecommitdiff
path: root/numpy/core
diff options
context:
space:
mode:
authorJulian Taylor <jtaylor.debian@googlemail.com>2014-11-02 16:55:40 +0100
committerJulian Taylor <jtaylor.debian@googlemail.com>2014-11-02 17:29:23 +0100
commitf7c0e78ac6a6ca8a3c72cf7167607a62f57637f9 (patch)
tree828270c0b1600046af72fe9d821b7f9d1609490d /numpy/core
parentb40e686f10421099400a533b343d6d1212f786e9 (diff)
downloadnumpy-f7c0e78ac6a6ca8a3c72cf7167607a62f57637f9.tar.gz
BUG: fix header using symbols not available in py3
Inside numpy we handle this via the py3kcompat header which is not really public api. So for our public headers we need some explicit checks.
Diffstat (limited to 'numpy/core')
-rw-r--r--numpy/core/include/numpy/ndarrayobject.h21
-rw-r--r--numpy/core/src/multiarray/multiarray_tests.c.src19
-rw-r--r--numpy/core/tests/test_multiarray.py10
3 files changed, 44 insertions, 6 deletions
diff --git a/numpy/core/include/numpy/ndarrayobject.h b/numpy/core/include/numpy/ndarrayobject.h
index b8c7c3a2d..fbaaeacea 100644
--- a/numpy/core/include/numpy/ndarrayobject.h
+++ b/numpy/core/include/numpy/ndarrayobject.h
@@ -14,6 +14,7 @@ extern "C" CONFUSE_EMACS
everything when you're typing */
#endif
+#include <Python.h>
#include "ndarraytypes.h"
/* Includes the "function" C-API -- these are all stored in a
@@ -50,14 +51,26 @@ extern "C" CONFUSE_EMACS
#define PyArray_CheckScalar(m) (PyArray_IsScalar(m, Generic) || \
PyArray_IsZeroDim(m))
-
+#if PY_MAJOR_VERSION >= 3
+#define PyArray_IsPythonNumber(obj) \
+ (PyFloat_Check(obj) || PyComplex_Check(obj) || \
+ PyLong_Check(obj) || PyBool_Check(obj))
+#define PyArray_IsIntegerScalar(obj) (PyLong_Check(obj) \
+ || PyArray_IsScalar((obj), Integer))
+#define PyArray_IsPythonScalar(obj) \
+ (PyArray_IsPythonNumber(obj) || PyBytes_Check(obj) || \
+ PyUnicode_Check(obj))
+#else
#define PyArray_IsPythonNumber(obj) \
(PyInt_Check(obj) || PyFloat_Check(obj) || PyComplex_Check(obj) || \
PyLong_Check(obj) || PyBool_Check(obj))
-
+#define PyArray_IsIntegerScalar(obj) (PyInt_Check(obj) \
+ || PyLong_Check(obj) \
+ || PyArray_IsScalar((obj), Integer))
#define PyArray_IsPythonScalar(obj) \
(PyArray_IsPythonNumber(obj) || PyString_Check(obj) || \
PyUnicode_Check(obj))
+#endif
#define PyArray_IsAnyScalar(obj) \
(PyArray_IsScalar(obj, Generic) || PyArray_IsPythonScalar(obj))
@@ -65,10 +78,6 @@ extern "C" CONFUSE_EMACS
#define PyArray_CheckAnyScalar(obj) (PyArray_IsPythonScalar(obj) || \
PyArray_CheckScalar(obj))
-#define PyArray_IsIntegerScalar(obj) (PyInt_Check(obj) \
- || PyLong_Check(obj) \
- || PyArray_IsScalar((obj), Integer))
-
#define PyArray_GETCONTIGUOUS(m) (PyArray_ISCONTIGUOUS(m) ? \
Py_INCREF(m), (m) : \
diff --git a/numpy/core/src/multiarray/multiarray_tests.c.src b/numpy/core/src/multiarray/multiarray_tests.c.src
index bd0366bd5..9d4aabe31 100644
--- a/numpy/core/src/multiarray/multiarray_tests.c.src
+++ b/numpy/core/src/multiarray/multiarray_tests.c.src
@@ -2,6 +2,22 @@
#include <Python.h>
#include "numpy/arrayobject.h"
+/* test PyArray_IsPythonScalar, before including private py3 compat header */
+static PyObject *
+IsPythonScalar(PyObject * dummy, PyObject *args)
+{
+ PyObject *arg = NULL;
+ if (!PyArg_ParseTuple(args, "O", &arg)) {
+ return NULL;
+ }
+ if (PyArray_IsPythonScalar(arg)) {
+ Py_RETURN_TRUE;
+ }
+ else {
+ Py_RETURN_FALSE;
+ }
+}
+
#include "npy_pycompat.h"
/*
@@ -824,6 +840,9 @@ test_nditer_too_large(PyObject *NPY_UNUSED(self), PyObject *args) {
static PyMethodDef Multiarray_TestsMethods[] = {
+ {"IsPythonScalar",
+ IsPythonScalar,
+ METH_VARARGS, NULL},
{"test_neighborhood_iterator",
test_neighborhood_iterator,
METH_VARARGS, NULL},
diff --git a/numpy/core/tests/test_multiarray.py b/numpy/core/tests/test_multiarray.py
index d02821cba..b92c45a1f 100644
--- a/numpy/core/tests/test_multiarray.py
+++ b/numpy/core/tests/test_multiarray.py
@@ -1892,6 +1892,16 @@ class TestBinop(object):
assert_(isinstance(obj2, SomeClass2))
+class TestCAPI(TestCase):
+ def test_IsPythonScalar(self):
+ from numpy.core.multiarray_tests import IsPythonScalar
+ assert_(IsPythonScalar(b'foobar'))
+ assert_(IsPythonScalar(1))
+ assert_(IsPythonScalar(2**80))
+ assert_(IsPythonScalar(2.))
+ assert_(IsPythonScalar("a"))
+
+
class TestSubscripting(TestCase):
def test_test_zero_rank(self):
x = array([1, 2, 3])