Seems like a bug to me. Certain branches in _array_richcompare return False to fail rather than Py_NotImplemented, which means the string-understanding comparison fallbacks don't run. Attached is a (simple) patch that resolves this bug, and doesn't seem to cause any of the unit tests to fail. Does this make sense to someone with a better understanding of the rich comparison code than I?

Mike

On 05/25/2010 12:54 PM, Keith Goodman wrote:
a1 = np.array(['a', 'b'], dtype=object)
>>  a2 = np.array(['a', 'b'])
>>
>>  a1 == a2
    array([ True,  True], dtype=bool)  # Looks good
>>  a2 == a1
    False  # Should I have expected this?



--
Michael Droettboom
Science Software Branch
Space Telescope Science Institute
Baltimore, Maryland, USA

Index: numpy/core/src/multiarray/arrayobject.c
===================================================================
--- numpy/core/src/multiarray/arrayobject.c	(revision 8430)
+++ numpy/core/src/multiarray/arrayobject.c	(working copy)
@@ -958,8 +958,8 @@
         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:
@@ -1021,8 +1021,8 @@
         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:
@@ -1217,7 +1217,7 @@
         }
         else if ((strides.ptr == NULL) &&
                  (buffer.len < (offset + (((intp)itemsize)*
-                                          PyArray_MultiplyList(dims.ptr, 
+                                          PyArray_MultiplyList(dims.ptr,
                                                                dims.len))))) {
             PyErr_SetString(PyExc_TypeError,
                             "buffer is too small for "      \
Index: numpy/core/tests/test_regression.py
===================================================================
--- numpy/core/tests/test_regression.py	(revision 8430)
+++ numpy/core/tests/test_regression.py	(working copy)
@@ -1302,5 +1302,13 @@
         # Ticket #1345: the following should not cause a crash
         np.fromstring(asbytes('aa, aa, 1.0'), sep=',')
 
+    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()
_______________________________________________
NumPy-Discussion mailing list
[email protected]
http://mail.scipy.org/mailman/listinfo/numpy-discussion

Reply via email to