Commits

Ilya Osadchiy committed 58762c3

Returning False for "arr == None". Preparations for "zeors(3) == zeros(4)" to
return False

  • Participants
  • Parent commits 8e5dc0c
  • Branches numpy-comparison

Comments (0)

Files changed (2)

File pypy/module/micronumpy/interp_numarray.py

     descr_pow = _binop_impl("power")
     descr_mod = _binop_impl("mod")
 
-    descr_eq = _binop_impl("equal")
-    descr_ne = _binop_impl("not_equal")
+    def _eq_ne_impl(ufunc_name, fallback_res):
+        assert isinstance(fallback_res, bool)
+        def impl(self, space, w_other):
+            # Unlike ufunc, operator may return simple bool
+            if space.is_w(w_other, space.w_None):
+                # Special case
+                return space.wrap(fallback_res)
+            try:
+                return getattr(interp_ufuncs.get(space), ufunc_name).call(space, [self, w_other])
+            except OperationError, e:
+                if e.match(space, space.w_ValueError):
+                    # For the case when arrays of incompatible size are compared
+                    return space.wrap(fallback_res)
+                raise
+        return func_with_new_name(impl, "binop_%s_impl" % ufunc_name)
+
+    descr_eq = _eq_ne_impl("equal", False)
+    descr_ne = _eq_ne_impl("not_equal", True)
     descr_lt = _binop_impl("less")
     descr_le = _binop_impl("less_equal")
     descr_gt = _binop_impl("greater")

File pypy/module/micronumpy/test/test_numarray.py

             for i in xrange(5):
                 assert c[i] == func(b[i], 3)
 
+    def test_eq_ne(self):
+        from numpy import array
+        a = array(range(5))
+        assert (a == None) is False
+        assert (a != None) is True
+        # TODO: uncomment after size check is implemented
+        # b = array(range(2))
+        # assert (a == b) is False
+        # assert (a != b) is True
 
 class AppTestSupport(object):
     def setup_class(cls):