diff options
author | Pauli Virtanen <pav@iki.fi> | 2010-10-11 00:40:13 +0200 |
---|---|---|
committer | Pauli Virtanen <pav@iki.fi> | 2010-10-11 00:40:13 +0200 |
commit | 11ee694744f2552d77652ed929fdc2b4ccca6843 (patch) | |
tree | 64b9389e2a7db4765beef7898fb4040cc667f352 /numpy | |
parent | d7ff9074fcde66225478d6721cf22b2db32dc2fd (diff) | |
download | numpy-11ee694744f2552d77652ed929fdc2b4ccca6843.tar.gz |
BUG: core: richcompare must indicate undefined comparison via Py_NotImplemented (fixes #1491)
The Python documentation indicates that the result from comparisons
should be Py_NotImplemented when undefined, and there is no reason why
not follow that also in array_richcompare.
Diffstat (limited to 'numpy')
-rw-r--r-- | numpy/core/src/multiarray/arrayobject.c | 28 | ||||
-rw-r--r-- | numpy/core/tests/test_regression.py | 8 |
2 files changed, 21 insertions, 15 deletions
diff --git a/numpy/core/src/multiarray/arrayobject.c b/numpy/core/src/multiarray/arrayobject.c index ab1e824be..587e45d26 100644 --- a/numpy/core/src/multiarray/arrayobject.c +++ b/numpy/core/src/multiarray/arrayobject.c @@ -907,16 +907,15 @@ array_richcompare(PyArrayObject *self, PyObject *other, int cmp_op) array_other = PyArray_FromObject(other, typenum, 0, 0); /* - * If not successful, then return False. This fixes code - * that used to allow equality comparisons between arrays - * and other objects which would give a result of False. + * If not successful, indicate that the items cannot be compared + * this way. */ if ((array_other == NULL) || (array_other == Py_None)) { Py_XDECREF(array_other); PyErr_Clear(); - Py_INCREF(Py_False); - return Py_False; + Py_INCREF(Py_NotImplemented); + return Py_NotImplemented; } } else { @@ -952,14 +951,14 @@ array_richcompare(PyArrayObject *self, PyObject *other, int cmp_op) } /* * If the comparison results in NULL, then the - * two array objects can not be compared together so - * return zero + * two array objects can not be compared together; + * indicate that */ Py_DECREF(array_other); if (result == NULL) { PyErr_Clear(); - Py_INCREF(Py_False); - return Py_False; + Py_INCREF(Py_NotImplemented); + return Py_NotImplemented; } break; case Py_NE: @@ -976,14 +975,13 @@ array_richcompare(PyArrayObject *self, PyObject *other, int cmp_op) array_other = PyArray_FromObject(other, typenum, 0, 0); /* * If not successful, then objects cannot be - * compared and cannot be equal, therefore, - * return True; + * compared this way */ if ((array_other == NULL) || (array_other == Py_None)) { Py_XDECREF(array_other); PyErr_Clear(); - Py_INCREF(Py_True); - return Py_True; + Py_INCREF(Py_NotImplemented); + return Py_NotImplemented; } } else { @@ -1021,8 +1019,8 @@ array_richcompare(PyArrayObject *self, PyObject *other, int cmp_op) Py_DECREF(array_other); if (result == NULL) { PyErr_Clear(); - Py_INCREF(Py_True); - return Py_True; + Py_INCREF(Py_NotImplemented); + return Py_NotImplemented; } break; case Py_GT: diff --git a/numpy/core/tests/test_regression.py b/numpy/core/tests/test_regression.py index 52548ca1a..12fe089e6 100644 --- a/numpy/core/tests/test_regression.py +++ b/numpy/core/tests/test_regression.py @@ -1426,5 +1426,13 @@ class TestRegression(TestCase): rep = repr(ra) # should not cause a segmentation fault assert_raises(ValueError, setattr, ra.dtype, 'names', ('f1', 'f1')) + def test_eq_string_and_object_array(self): + # From e-mail thread "__eq__ with str and object" (Keith Goodman) + a1 = np.array(['a', 'b'], dtype=object) + a2 = np.array(['a', 'c']) + assert_array_equal(a1 == a2, [True, False]) + assert_array_equal(a2 == a1, [True, False]) + + if __name__ == "__main__": run_module_suite() |