From 1234688ae809870007b73b4a8816711a60f8e62c Mon Sep 17 00:00:00 2001 From: "Yury V. Zaytsev" Date: Tue, 16 Jul 2013 11:29:22 +0200 Subject: [PATCH 1/3] BUG: Fix PyType_FastSubclass() check for numpy.int_ Signed-off-by: Yury V. Zaytsev --- numpy/core/src/multiarray/multiarraymodule.c | 33 ++++++++++++++------ 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/numpy/core/src/multiarray/multiarraymodule.c b/numpy/core/src/multiarray/multiarraymodule.c index 7ec7ab639b68..f8e434e675a8 100644 --- a/numpy/core/src/multiarray/multiarraymodule.c +++ b/numpy/core/src/multiarray/multiarraymodule.c @@ -3783,6 +3783,19 @@ setup_scalartypes(PyObject *NPY_UNUSED(dict)) } \ Py##child##ArrType_Type.tp_hash = Py##parent1##_Type.tp_hash; +/* + * In Py3K, int is no longer a fixed-width integer type, so don't + * inherit numpy.int_ from it. + */ +#if defined(NPY_PY3K) +#define INHERIT_INT(child, parent2) \ + SINGLE_INHERIT(child, parent2); +#else +#define INHERIT_INT(child, parent2) \ + Py##child##ArrType_Type.tp_flags |= Py_TPFLAGS_INT_SUBCLASS; \ + DUAL_INHERIT(child, Int, parent2); +#endif + #if defined(NPY_PY3K) #define DUAL_INHERIT_COMPARE(child, parent1, parent2) #else @@ -3811,18 +3824,17 @@ setup_scalartypes(PyObject *NPY_UNUSED(dict)) SINGLE_INHERIT(Bool, Generic); SINGLE_INHERIT(Byte, SignedInteger); SINGLE_INHERIT(Short, SignedInteger); -#if NPY_SIZEOF_INT == NPY_SIZEOF_LONG && !defined(NPY_PY3K) - DUAL_INHERIT(Int, Int, SignedInteger); + +#if NPY_SIZEOF_INT == NPY_SIZEOF_LONG + INHERIT_INT(Int, SignedInteger); #else SINGLE_INHERIT(Int, SignedInteger); #endif -#if !defined(NPY_PY3K) - DUAL_INHERIT(Long, Int, SignedInteger); -#else - SINGLE_INHERIT(Long, SignedInteger); -#endif -#if NPY_SIZEOF_LONGLONG == NPY_SIZEOF_LONG && !defined(NPY_PY3K) - DUAL_INHERIT(LongLong, Int, SignedInteger); + + INHERIT_INT(Long, SignedInteger); + +#if NPY_SIZEOF_LONGLONG == NPY_SIZEOF_LONG + INHERIT_INT(LongLong, SignedInteger); #else SINGLE_INHERIT(LongLong, SignedInteger); #endif @@ -3864,6 +3876,9 @@ setup_scalartypes(PyObject *NPY_UNUSED(dict)) #undef SINGLE_INHERIT #undef DUAL_INHERIT +#undef INHERIT_INT +#undef DUAL_INHERIT2 +#undef DUAL_INHERIT_COMPARE /* * Clean up string and unicode array types so they act more like From 83a3daf0c6d1d55c10a70bf2a90be954af8d747e Mon Sep 17 00:00:00 2001 From: "Yury V. Zaytsev" Date: Tue, 16 Jul 2013 17:12:55 +0200 Subject: [PATCH 2/3] BUG: Document that numpy.int_ doesn't inherit from int on Py3k Signed-off-by: Yury V. Zaytsev --- doc/source/reference/arrays.scalars.rst | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/doc/source/reference/arrays.scalars.rst b/doc/source/reference/arrays.scalars.rst index 0a0d8045c7cb..f229efb07d4b 100644 --- a/doc/source/reference/arrays.scalars.rst +++ b/doc/source/reference/arrays.scalars.rst @@ -61,14 +61,14 @@ are also provided. The C-like names are associated with character codes, which are shown in the table. Use of the character codes, however, is discouraged. -Five of the scalar types are essentially equivalent to fundamental +Some of the scalar types are essentially equivalent to fundamental Python types and therefore inherit from them as well as from the generic array scalar type: ==================== ==================== Array scalar type Related Python type ==================== ==================== -:class:`int_` :class:`IntType` +:class:`int_` :class:`IntType` (Python 2 only) :class:`float_` :class:`FloatType` :class:`complex_` :class:`ComplexType` :class:`str_` :class:`StringType` @@ -88,6 +88,11 @@ Python Boolean scalar. than Python's default implementation of :class:`bool` as a sub-class of int. +.. warning:: + + The :class:`int_` type does **not** inherit from the + :class:`int` built-in under Python 3, because type :class:`int` is no + longer a fixed-width integer type. .. tip:: The default data type in Numpy is :class:`float_`. From 41e6f0617e198fe45427d3f63b58d940f0cdd3f8 Mon Sep 17 00:00:00 2001 From: "Yury V. Zaytsev" Date: Wed, 17 Jul 2013 16:48:57 +0200 Subject: [PATCH 3/3] TST: Add regression test to check numpy.int_ inheritance on Py2/Py3 Signed-off-by: Yury V. Zaytsev --- .../src/multiarray/multiarray_tests.c.src | 29 +++++++++++++++++++ numpy/core/tests/test_multiarray.py | 16 ++++++++++ 2 files changed, 45 insertions(+) diff --git a/numpy/core/src/multiarray/multiarray_tests.c.src b/numpy/core/src/multiarray/multiarray_tests.c.src index f22b7462dee5..0aea85f66347 100644 --- a/numpy/core/src/multiarray/multiarray_tests.c.src +++ b/numpy/core/src/multiarray/multiarray_tests.c.src @@ -557,6 +557,30 @@ fail: return NULL; } + +#if !defined(NPY_PY3K) +static PyObject * +int_subclass(PyObject *dummy, PyObject *args) +{ + + PyObject *result = NULL; + PyObject *scalar_object = NULL; + + if (!PyArg_UnpackTuple(args, "test_int_subclass", 1, 1, &scalar_object)) + return NULL; + + if (PyInt_Check(scalar_object)) + result = Py_True; + else + result = Py_False; + + Py_INCREF(result); + + return result; + +} +#endif + static PyMethodDef Multiarray_TestsMethods[] = { {"test_neighborhood_iterator", test_neighborhood_iterator, @@ -573,6 +597,11 @@ static PyMethodDef Multiarray_TestsMethods[] = { {"test_inplace_increment", inplace_increment, METH_VARARGS, NULL}, +#if !defined(NPY_PY3K) + {"test_int_subclass", + int_subclass, + METH_VARARGS, NULL}, +#endif {NULL, NULL, 0, NULL} /* Sentinel */ }; diff --git a/numpy/core/tests/test_multiarray.py b/numpy/core/tests/test_multiarray.py index 6cd24a76fe96..c298b4f1db05 100644 --- a/numpy/core/tests/test_multiarray.py +++ b/numpy/core/tests/test_multiarray.py @@ -95,6 +95,22 @@ def test_dtypeattr(self): assert_equal(self.one.dtype.str[1], 'i') assert_equal(self.three.dtype.str[1], 'f') + def test_int_subclassing(self): + # Regression test for https://github.com/numpy/numpy/pull/3526 + + numpy_int = np.int_(0) + + if sys.version_info[0] >= 3: + # On Py3k int_ should not inherit from int, because it's not fixed-width anymore + assert_equal(isinstance(numpy_int, int), False) + else: + # Otherwise, it should inherit from int... + assert_equal(isinstance(numpy_int, int), True) + + # ... and fast-path checks on C-API level should also work + from numpy.core.multiarray_tests import test_int_subclass + assert_equal(test_int_subclass(numpy_int), True) + def test_stridesattr(self): x = self.one def make_array(size, offset, strides):