From 84831ca7b7926bf1c73e1702201e7591c55588a3 Mon Sep 17 00:00:00 2001 From: Sebastian Berg Date: Wed, 10 Jul 2013 16:49:59 +0200 Subject: FIX: Make object comparison ufuncs not include identity check The issue here is that PyObject_RichCompareBool does an identity check before doing any other checks. This is good, and is the way for example list comparisons are handled. However in numpy comparisons are elementwise, so that the identitycheck should not be expected. The example for this is as follows: obj_array = np.arange(3) a = np.array([obj_array, 0], dtype=object) np.equal(a, a) If an identity check is done, this returns [True, True]. But obj_array == obj_array itself cannot be cast to a bool. While this is slightly slower, not doing the identity check seems more logical in the light of elementwise operations. Closes gh-2117. --- numpy/core/src/umath/loops.c.src | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'numpy/core/src') diff --git a/numpy/core/src/umath/loops.c.src b/numpy/core/src/umath/loops.c.src index ee7e7652d..c1e2dc1c5 100644 --- a/numpy/core/src/umath/loops.c.src +++ b/numpy/core/src/umath/loops.c.src @@ -2568,9 +2568,18 @@ OBJECT_@kind@(char **args, npy_intp *dimensions, npy_intp *steps, void *NPY_UNUS BINARY_LOOP { PyObject *in1 = *(PyObject **)ip1; PyObject *in2 = *(PyObject **)ip2; - int ret = PyObject_RichCompareBool( + /* + * Do not use RichCompareBool because it includes an identity check. + * But this is wrong for elementwise behaviour, since i.e. it says + * that NaN can be equal to NaN, and an array is equal to itself. + */ + PyObject *ret_obj = PyObject_RichCompare( in1 ? in1 : Py_None, in2 ? in2 : Py_None, Py_@OP@); + if (ret_obj == NULL) { + return; + } + int ret = PyObject_IsTrue(ret_obj); if (ret == -1) { return; } -- cgit v1.2.1