8000 Merge pull request #5257 from juliantaylor/py3-header-fix · numpy/numpy@cfa095a · GitHub
[go: up one dir, main page]

Skip to content

Commit cfa095a

Browse files
committed
Merge pull request #5257 from juliantaylor/py3-header-fix
BUG: fix header using symbols not available in py3
2 parents 066be28 + f7c0e78 commit cfa095a

File tree

3 files changed

+44
-6
lines changed

3 files changed

+44
-6
lines changed

numpy/core/include/numpy/ndarrayobject.h

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ extern "C" CONFUSE_EMACS
1414
everything when you're typing */
1515
#endif
1616

17+
#include <Python.h>
1718
#include "ndarraytypes.h"
1819

1920
/* Includes the "function" C-API -- these are all stored in a
@@ -50,25 +51,33 @@ extern "C" CONFUSE_EMACS
5051

5152
#define PyArray_CheckScalar(m) (PyArray_IsScalar(m, Generic) || \
5253
PyArray_IsZeroDim(m))
53-
54+
#if PY_MAJOR_VERSION >= 3
55+
#define PyArray_IsPythonNumber(obj) \
56+
(PyFloat_Check(obj) || PyComplex_Check(obj) || \
57+
PyLong_Check(obj) || PyBool_Check(obj))
58+
#define PyArray_IsIntegerScalar(obj) (PyLong_Check(obj) \
59+
|| PyArray_IsScalar((obj), Integer))
60+
#define PyArray_IsPythonScalar(obj) \
61+
(PyArray_IsPythonNumber(obj) || PyBytes_Check(obj) || \
62+
PyUnicode_Check(obj))
63+
#else
5464
#define PyArray_IsPythonNumber(obj) \
5565
(PyInt_Check(obj) || PyFloat_Check(obj) || PyComplex_Check(obj) || \
5666
PyLong_Check(obj) || PyBool_Check(obj))
57-
67+
#define PyArray_IsIntegerScalar(obj) (PyInt_Check(obj) \
68+
|| PyLong_Check(obj) \
69+
|| PyArray_IsScalar((obj), Integer))
5870
#define PyArray_IsPythonScalar(obj) \
5971
(PyArray_IsPythonNumber(obj) || PyString_Check(obj) || \
6072
PyUnicode_Check(obj))
73+
#endif
6174

6275
#define PyArray_IsAnyScalar(obj) \
6376
(PyArray_IsScalar(obj, Generic) || PyArray_IsPythonScalar(obj))
6477

6578
#define PyArray_CheckAnyScalar(obj) (PyArray_IsPythonScalar(obj) || \
6679
PyArray_CheckScalar(obj))
6780

68-
#define PyArray_IsIntegerScalar(obj) (PyInt_Check(obj) \
69-
|| PyLong_Check(obj) \
70-
|| PyArray_IsScalar((obj), Integer))
71-
7281

7382
#define PyArray_GETCONTIGUOUS(m) (PyArray_ISCONTIGUOUS(m) ? \
7483
Py_INCREF(m), (m) : \

numpy/core/src/multiarray/multiarray_tests.c.src

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,22 @@
22
#include <Python.h>
33
#include "numpy/arrayobject.h"
44

5+
/* test PyArray_IsPythonScalar, before including private py3 compat header */
6+
static PyObject *
7+
IsPythonScalar(PyObject * dummy, PyObject *args)
8+
{
9+
PyObject *arg = NULL;
10+
if (!PyArg_ParseTuple(args, "O", &arg)) {
11+
return NULL;
12+
}
13+
if (PyArray_IsPythonScalar(arg)) {
14+
Py_RETURN_TRUE;
15+
}
16+
else {
17+
Py_RETURN_FALSE;
18+
}
19+
}
20+
521
#include "npy_pycompat.h"
622

723
/*
@@ -860,6 +876,9 @@ test_nditer_too_large(PyObject *NPY_UNUSED(self), PyObject *args) {
860876

861877

862878
static PyMethodDef Multiarray_TestsMethods[] = {
879+
{"IsPythonScalar",
880+
IsPythonScalar,
881+
METH_VARARGS, NULL},
863882
{"test_neighborhood_iterator",
864883
test_neighborhood_iterator,
865884
METH_VARARGS, NULL},

numpy/core/tests/test_multiarray.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2162,6 +2162,16 @@ def __numpy_ufunc__(self, ufunc, method, i, inputs, **kw):
21622162
assert_(isinstance(obj2, SomeClass2))
21632163

21642164

2165+
class TestCAPI(TestCase):
2166+
def test_IsPythonScalar(self):
2167+
from numpy.core.multiarray_tests import IsPythonScalar
2168+
assert_(IsPythonScalar(b'foobar'))
2169+
assert_(IsPythonScalar(1))
2170+
assert_(IsPythonScalar(2**80))
2171+
assert_(IsPythonScalar(2.))
2172+
assert_(IsPythonScalar("a"))
2173+
2174+
21652175
class TestSubscripting(TestCase):
21662176
def test_test_zero_rank(self):
21672177
x = array([1, 2, 3])

0 commit comments

Comments
 (0)
0