diff options
author | Charles Harris <charlesr.harris@gmail.com> | 2014-11-02 09:03:44 -0800 |
---|---|---|
committer | Charles Harris <charlesr.harris@gmail.com> | 2014-11-02 09:03:44 -0800 |
commit | cfa095a203586eae9466f5ebbbdc5f60905aeb20 (patch) | |
tree | db50a2a1ffbd60af8addd2fdb8411e97066d108b /numpy/core | |
parent | 066be2857408682a818e6967a5a91d342b59727c (diff) | |
parent | f7c0e78ac6a6ca8a3c72cf7167607a62f57637f9 (diff) | |
download | numpy-cfa095a203586eae9466f5ebbbdc5f60905aeb20.tar.gz |
Merge pull request #5257 from juliantaylor/py3-header-fix
BUG: fix header using symbols not available in py3
Diffstat (limited to 'numpy/core')
-rw-r--r-- | numpy/core/include/numpy/ndarrayobject.h | 21 | ||||
-rw-r--r-- | numpy/core/src/multiarray/multiarray_tests.c.src | 19 | ||||
-rw-r--r-- | numpy/core/tests/test_multiarray.py | 10 |
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 a22319cfe..77e699562 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" /* @@ -860,6 +876,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 d6f98e0b7..1e47a2297 100644 --- a/numpy/core/tests/test_multiarray.py +++ b/numpy/core/tests/test_multiarray.py @@ -2162,6 +2162,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]) |