Skip to content

Commit

Permalink
BUG: Fix fieldless comparison broadcasting
Browse files Browse the repository at this point in the history
  • Loading branch information
ahaldane committed Sep 5, 2019
1 parent 7c3adfb commit d6f7524
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
5 changes: 5 additions & 0 deletions doc/release/upcoming_changes/14393.c_api.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
PyDataType_ISUNSIZED(descr) now returns False for structured datatypes
----------------------------------------------------------------------
Previously this returned True for any datatype of itemsize 0, but now this
returns false for the non-flexible datatype with itemsize 0, ``np.dtype([])``.

5 changes: 3 additions & 2 deletions doc/source/reference/c-api/array.rst
Original file line number Diff line number Diff line change
Expand Up @@ -997,8 +997,9 @@ argument must be a :c:type:`PyObject *<PyObject>` that can be directly interpret
called on flexible dtypes. Types that are attached to an array will always
be sized, hence the array form of this macro not existing.
..versionchanged:: 1.18.0
For structured datatypes with no fields this function now return False.
.. versionchanged:: 1.18
For structured datatypes with no fields this function now returns False.
.. c:function:: PyTypeNum_ISUSERDEF(num)
Expand Down
18 changes: 14 additions & 4 deletions numpy/core/src/multiarray/arrayobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1200,11 +1200,21 @@ _void_compare(PyArrayObject *self, PyArrayObject *other, int cmp_op)
}
}
if (res == NULL && !PyErr_Occurred()) {
/* these dtypes had no fields */
res = PyArray_NewLikeArray(self, NPY_ANYORDER,
PyArray_DescrFromType(NPY_BOOL), 1);
/* these dtypes had no fields. Use a MultiIter to broadcast them
* to an output array, and fill with True (for EQ)*/
PyArrayMultiIterObject *mit = (PyArrayMultiIterObject *)
PyArray_MultiIterNew(2, self, other);
if (mit == NULL) {
return NULL;
}

res = PyArray_NewFromDescr(&PyArray_Type,
PyArray_DescrFromType(NPY_BOOL),
mit->nd, mit->dimensions,
NULL, NULL, 0, NULL);
Py_DECREF(mit);
if (res) {
PyArray_FILLWBYTE((PyArrayObject*)res,
PyArray_FILLWBYTE((PyArrayObject *)res,
cmp_op == Py_EQ ? 1 : 0);
}
}
Expand Down
4 changes: 2 additions & 2 deletions numpy/core/tests/test_dtype.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,8 +441,8 @@ def test_fieldless_views(self):
assert_equal(np.zeros(2, dtype=[]) == np.zeros(2, dtype=[]),
np.ones(2, dtype=bool))

assert_equal(np.zeros(2, dtype=[]) == a,
np.ones(2, dtype=bool))
assert_equal(np.zeros((1, 2), dtype=[]) == a,
np.ones((1, 2), dtype=bool))


class TestSubarray(object):
Expand Down

0 comments on commit d6f7524

Please sign in to comment.