Skip to content

Commit 6497151

Browse files
committed
Merge pull request numpy#3905 from charris/backport-1.8-3900
Backport 1.8 3900
2 parents 687ae39 + 7072af9 commit 6497151

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

numpy/ma/core.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -930,6 +930,9 @@ def __call__ (self, a, b, *args, **kwargs):
930930
with np.errstate():
931931
np.seterr(divide='ignore', invalid='ignore')
932932
result = self.f(da, db, *args, **kwargs)
933+
# check it worked
934+
if result is NotImplemented:
935+
return NotImplemented
933936
# Case 1. : scalar
934937
if not result.ndim:
935938
if m:
@@ -999,6 +1002,9 @@ def outer (self, a, b):
9991002
return masked
10001003
(da, db) = (getdata(a), getdata(b))
10011004
d = self.f.outer(da, db)
1005+
# check it worked
1006+
if d is NotImplemented:
1007+
return NotImplemented
10021008
if m is not nomask:
10031009
np.copyto(d, da, where=m)
10041010
if d.shape:
@@ -1065,6 +1071,9 @@ def __call__(self, a, b, *args, **kwargs):
10651071
with np.errstate():
10661072
np.seterr(divide='ignore', invalid='ignore')
10671073
result = self.f(da, db, *args, **kwargs)
1074+
# check it worked
1075+
if result is NotImplemented:
1076+
return NotImplemented
10681077
# Get the mask as a combination of ma, mb and invalid
10691078
m = ~umath.isfinite(result)
10701079
m |= ma

numpy/ma/tests/test_core.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1662,6 +1662,27 @@ def test_ndarray_mask(self):
16621662
assert_equal(test.mask, control.mask)
16631663
self.assertTrue(not isinstance(test.mask, MaskedArray))
16641664

1665+
def test_treatment_of_NotImplemented(self):
1666+
"Check any NotImplemented returned by umath.<ufunc> is passed on"
1667+
a = masked_array([1., 2.], mask=[1, 0])
1668+
# basic tests for _MaskedBinaryOperation
1669+
assert_(a.__mul__('abc') is NotImplemented)
1670+
assert_(multiply.outer(a, 'abc') is NotImplemented)
1671+
# and for _DomainedBinaryOperation
1672+
assert_(a.__div__('abc') is NotImplemented)
1673+
1674+
# also check explicitly that rmul of another class can be accessed
1675+
class MyClass(str):
1676+
def __mul__(self, other):
1677+
return "My mul"
1678+
1679+
def __rmul__(self, other):
1680+
return "My rmul"
1681+
1682+
me = MyClass()
1683+
assert_(me * a == "My mul")
1684+
assert_(a * me == "My rmul")
1685+
16651686
#------------------------------------------------------------------------------
16661687

16671688
class TestMaskedArrayInPlaceArithmetics(TestCase):

0 commit comments

Comments
 (0)