Skip to content

Commit

Permalink
MAINT: move the special case for string comparison before the regular…
Browse files Browse the repository at this point in the history
… case

The ndarray richcompare function has a special case for handling
string dtypes (which currently cannot be handled by
ufuncs). Traditionally this was handled by the ufuncs returning
NotImplemented, and then falling through to a special case. By moving
the special case to the top of the richcompare function, it becomes
unnecessary for the ufuncs to return NotImplemented in this case.
  • Loading branch information
njsmith authored and charris committed Jun 13, 2015
1 parent 88937f8 commit 4b1f508
Showing 1 changed file with 19 additions and 18 deletions.
37 changes: 19 additions & 18 deletions numpy/core/src/multiarray/arrayobject.c
Expand Up @@ -1295,6 +1295,25 @@ array_richcompare(PyArrayObject *self, PyObject *other, int cmp_op)
PyObject *obj_self = (PyObject *)self;
PyObject *result = NULL;

/* Special case for string arrays (which don't and currently can't have
* ufunc loops defined, so there's no point in trying).
*/
if (PyArray_ISSTRING(self)) {
array_other = (PyArrayObject *)PyArray_FromObject(other,
NPY_NOTYPE, 0, 0);
if (array_other == NULL) {
PyErr_Clear();
/* Never mind, carry on, see what happens */
}
else if (!PyArray_ISSTRING(array_other)) {
Py_DECREF(array_other);
/* Never mind, carry on, see what happens */
}
else {
return _strings_richcompare(self, array_other, cmp_op, 0);
}
}

switch (cmp_op) {
case Py_LT:
if (needs_right_binop_forward(obj_self, other, "__gt__", 0) &&
Expand Down Expand Up @@ -1484,24 +1503,6 @@ array_richcompare(PyArrayObject *self, PyObject *other, int cmp_op)
result = Py_NotImplemented;
Py_INCREF(result);
}
if (result == Py_NotImplemented) {
/* Try to handle string comparisons */
if (PyArray_TYPE(self) == NPY_OBJECT) {
return result;
}
array_other = (PyArrayObject *)PyArray_FromObject(other,
NPY_NOTYPE, 0, 0);
if (array_other == NULL) {
PyErr_Clear();
return result;
}
if (PyArray_ISSTRING(self) && PyArray_ISSTRING(array_other)) {
Py_DECREF(result);
result = _strings_richcompare(self, (PyArrayObject *)
array_other, cmp_op, 0);
}
Py_DECREF(array_other);
}
return result;
}

Expand Down

0 comments on commit 4b1f508

Please sign in to comment.