Skip to content

Commit d6f7524

Browse files
committed
BUG: Fix fieldless comparison broadcasting
Fixes #13438
1 parent 7c3adfb commit d6f7524

4 files changed

Lines changed: 24 additions & 8 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
PyDataType_ISUNSIZED(descr) now returns False for structured datatypes
2+
----------------------------------------------------------------------
3+
Previously this returned True for any datatype of itemsize 0, but now this
4+
returns false for the non-flexible datatype with itemsize 0, ``np.dtype([])``.
5+

‎doc/source/reference/c-api/array.rst‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -997,8 +997,9 @@ argument must be a :c:type:`PyObject *<PyObject>` that can be directly interpret
997997
called on flexible dtypes. Types that are attached to an array will always
998998
be sized, hence the array form of this macro not existing.
999999
1000-
..versionchanged:: 1.18.0
1001-
For structured datatypes with no fields this function now return False.
1000+
.. versionchanged:: 1.18
1001+
1002+
For structured datatypes with no fields this function now returns False.
10021003
10031004
.. c:function:: PyTypeNum_ISUSERDEF(num)
10041005

‎numpy/core/src/multiarray/arrayobject.c‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1200,11 +1200,21 @@ _void_compare(PyArrayObject *self, PyArrayObject *other, int cmp_op)
12001200
}
12011201
}
12021202
if (res == NULL && !PyErr_Occurred()) {
1203-
/* these dtypes had no fields */
1204-
res = PyArray_NewLikeArray(self, NPY_ANYORDER,
1205-
PyArray_DescrFromType(NPY_BOOL), 1);
1203+
/* these dtypes had no fields. Use a MultiIter to broadcast them
1204+
* to an output array, and fill with True (for EQ)*/
1205+
PyArrayMultiIterObject *mit = (PyArrayMultiIterObject *)
1206+
PyArray_MultiIterNew(2, self, other);
1207+
if (mit == NULL) {
1208+
return NULL;
1209+
}
1210+
1211+
res = PyArray_NewFromDescr(&PyArray_Type,
1212+
PyArray_DescrFromType(NPY_BOOL),
1213+
mit->nd, mit->dimensions,
1214+
NULL, NULL, 0, NULL);
1215+
Py_DECREF(mit);
12061216
if (res) {
1207-
PyArray_FILLWBYTE((PyArrayObject*)res,
1217+
PyArray_FILLWBYTE((PyArrayObject *)res,
12081218
cmp_op == Py_EQ ? 1 : 0);
12091219
}
12101220
}

‎numpy/core/tests/test_dtype.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -441,8 +441,8 @@ def test_fieldless_views(self):
441441
assert_equal(np.zeros(2, dtype=[]) == np.zeros(2, dtype=[]),
442442
np.ones(2, dtype=bool))
443443

444-
assert_equal(np.zeros(2, dtype=[]) == a,
445-
np.ones(2, dtype=bool))
444+
assert_equal(np.zeros((1, 2), dtype=[]) == a,
445+
np.ones((1, 2), dtype=bool))
446446

447447

448448
class TestSubarray(object):

0 commit comments

Comments
 (0)