From 851692ced06c0073cded98ca52e56d4424b18975 Mon Sep 17 00:00:00 2001 From: Eric Wieser Date: Wed, 5 Feb 2020 17:07:31 +0000 Subject: MAINT: Simplify arraydescr_richcompare This saves a handful of lines, and is arguably easier to read. There is no need to call `PyArray_DescrCheck`, since that's done almost immediately inside `_convert_from_any`. --- numpy/core/src/multiarray/descriptor.c | 81 +++++++++++----------------------- 1 file changed, 25 insertions(+), 56 deletions(-) (limited to 'numpy') diff --git a/numpy/core/src/multiarray/descriptor.c b/numpy/core/src/multiarray/descriptor.c index ec91da3ff..bfbf67ff9 100644 --- a/numpy/core/src/multiarray/descriptor.c +++ b/numpy/core/src/multiarray/descriptor.c @@ -3149,72 +3149,41 @@ arraydescr_str(PyArray_Descr *dtype) static PyObject * arraydescr_richcompare(PyArray_Descr *self, PyObject *other, int cmp_op) { - PyArray_Descr *new = NULL; - PyObject *result = Py_NotImplemented; - if (!PyArray_DescrCheck(other)) { - new = _convert_from_any(other, 0); - if (new == NULL) { - return NULL; - } - } - else { - new = (PyArray_Descr *)other; - Py_INCREF(new); + PyArray_Descr *new = _convert_from_any(other, 0); + if (new == NULL) { + return NULL; } + + npy_bool ret; switch (cmp_op) { case Py_LT: - if (!PyArray_EquivTypes(self, new) && PyArray_CanCastTo(self, new)) { - result = Py_True; - } - else { - result = Py_False; - } - break; + ret = !PyArray_EquivTypes(self, new) && PyArray_CanCastTo(self, new); + Py_DECREF(new); + return PyBool_FromLong(ret); case Py_LE: - if (PyArray_CanCastTo(self, new)) { - result = Py_True; - } - else { - result = Py_False; - } - break; + ret = PyArray_CanCastTo(self, new); + Py_DECREF(new); + return PyBool_FromLong(ret); case Py_EQ: - if (PyArray_EquivTypes(self, new)) { - result = Py_True; - } - else { - result = Py_False; - } - break; + ret = PyArray_EquivTypes(self, new); + Py_DECREF(new); + return PyBool_FromLong(ret); case Py_NE: - if (PyArray_EquivTypes(self, new)) - result = Py_False; - else - result = Py_True; - break; + ret = !PyArray_EquivTypes(self, new); + Py_DECREF(new); + return PyBool_FromLong(ret); case Py_GT: - if (!PyArray_EquivTypes(self, new) && PyArray_CanCastTo(new, self)) { - result = Py_True; - } - else { - result = Py_False; - } - break; + ret = !PyArray_EquivTypes(self, new) && PyArray_CanCastTo(new, self); + Py_DECREF(new); + return PyBool_FromLong(ret); case Py_GE: - if (PyArray_CanCastTo(new, self)) { - result = Py_True; - } - else { - result = Py_False; - } - break; + ret = PyArray_CanCastTo(new, self); + Py_DECREF(new); + return PyBool_FromLong(ret); default: - result = Py_NotImplemented; + Py_DECREF(new); + Py_RETURN_NOTIMPLEMENTED; } - - Py_XDECREF(new); - Py_INCREF(result); - return result; } static int -- cgit v1.2.1