diff --git a/numpy/ma/core.py b/numpy/ma/core.py index 8f2dbedeb61d..68c5d55951e3 100644 --- a/numpy/ma/core.py +++ b/numpy/ma/core.py @@ -930,6 +930,9 @@ def __call__ (self, a, b, *args, **kwargs): with np.errstate(): np.seterr(divide='ignore', invalid='ignore') result = self.f(da, db, *args, **kwargs) + # check it worked + if result is NotImplemented: + return NotImplemented # Case 1. : scalar if not result.ndim: if m: @@ -999,6 +1002,9 @@ def outer (self, a, b): return masked (da, db) = (getdata(a), getdata(b)) d = self.f.outer(da, db) + # check it worked + if d is NotImplemented: + return NotImplemented if m is not nomask: np.copyto(d, da, where=m) if d.shape: @@ -1065,6 +1071,9 @@ def __call__(self, a, b, *args, **kwargs): with np.errstate(): np.seterr(divide='ignore', invalid='ignore') result = self.f(da, db, *args, **kwargs) + # check it worked + if result is NotImplemented: + return NotImplemented # Get the mask as a combination of ma, mb and invalid m = ~umath.isfinite(result) m |= ma diff --git a/numpy/ma/tests/test_core.py b/numpy/ma/tests/test_core.py index 6c7baa48ec75..a4c0766e1a44 100644 --- a/numpy/ma/tests/test_core.py +++ b/numpy/ma/tests/test_core.py @@ -1662,6 +1662,27 @@ def test_ndarray_mask(self): assert_equal(test.mask, control.mask) self.assertTrue(not isinstance(test.mask, MaskedArray)) + def test_treatment_of_NotImplemented(self): + "Check any NotImplemented returned by umath. is passed on" + a = masked_array([1., 2.], mask=[1, 0]) + # basic tests for _MaskedBinaryOperation + assert_(a.__mul__('abc') is NotImplemented) + assert_(multiply.outer(a, 'abc') is NotImplemented) + # and for _DomainedBinaryOperation + assert_(a.__div__('abc') is NotImplemented) + + # also check explicitly that rmul of another class can be accessed + class MyClass(str): + def __mul__(self, other): + return "My mul" + + def __rmul__(self, other): + return "My rmul" + + me = MyClass() + assert_(me * a == "My mul") + assert_(a * me == "My rmul") + #------------------------------------------------------------------------------ class TestMaskedArrayInPlaceArithmetics(TestCase):