diff options
author | Nathaniel J. Smith <njs@pobox.com> | 2015-05-07 20:10:53 -0700 |
---|---|---|
committer | Charles Harris <charlesr.harris@gmail.com> | 2015-06-13 12:32:54 -0600 |
commit | 4b1f508a57549d8031a23160b40c7f87f47892ed (patch) | |
tree | 1f1843b308d8431017573918e858b572325a738d | |
parent | 88937f8f1d61d889e6923de7e15b63e40fa9a8ee (diff) | |
download | numpy-4b1f508a57549d8031a23160b40c7f87f47892ed.tar.gz |
MAINT: move the special case for string comparison before the regular 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.
-rw-r--r-- | numpy/core/src/multiarray/arrayobject.c | 37 |
1 files changed, 19 insertions, 18 deletions
diff --git a/numpy/core/src/multiarray/arrayobject.c b/numpy/core/src/multiarray/arrayobject.c index 1a5c30832..ac9e1a2d2 100644 --- a/numpy/core/src/multiarray/arrayobject.c +++ b/numpy/core/src/multiarray/arrayobject.c @@ -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) && @@ -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; } |