diff options
Diffstat (limited to 'numpy/ma')
-rw-r--r-- | numpy/ma/core.py | 19 | ||||
-rw-r--r-- | numpy/ma/tests/test_core.py | 9 |
2 files changed, 27 insertions, 1 deletions
diff --git a/numpy/ma/core.py b/numpy/ma/core.py index 94613ef2f..9ae225728 100644 --- a/numpy/ma/core.py +++ b/numpy/ma/core.py @@ -6193,7 +6193,13 @@ class MaskedConstant(MaskedArray): # Note that it can be tricky sometimes w/ type comparison data = np.array(0.) mask = np.array(True) - self.__singleton = MaskedArray(data, mask=mask).view(self) + + # prevent any modifications + data.flags.writeable = False + mask.flags.writeable = False + + masked = MaskedArray(data, mask=mask) + self.__singleton = masked.view(self) return self.__singleton @@ -6211,6 +6217,11 @@ class MaskedConstant(MaskedArray): self.__class__ = MaskedArray self.__array_finalize__(obj) + def __array_prepare__(self, obj, context=None): + return self.view(MaskedArray).__array_prepare__(obj, context) + + def __array_wrap__(self, obj, context=None): + return self.view(MaskedArray).__array_wrap__(obj, context) def __str__(self): return str(masked_print_option._display) @@ -6227,6 +6238,12 @@ class MaskedConstant(MaskedArray): """ return (self.__class__, ()) + # inplace operations have no effect. We have to override them to avoid + # trying to modify the readonly data and mask arrays + def __inop(self, other): + return self + __iadd__ = __isub__ = __imul__ = __ifloordiv__ = __itruediv__ = __ipow__ = __inop + masked = masked_singleton = MaskedConstant() masked_array = MaskedArray diff --git a/numpy/ma/tests/test_core.py b/numpy/ma/tests/test_core.py index bead29d31..bf83461d7 100644 --- a/numpy/ma/tests/test_core.py +++ b/numpy/ma/tests/test_core.py @@ -4766,6 +4766,15 @@ class TestMaskedConstant(TestCase): assert_(not isinstance(np.ma.masked[...], MC)) assert_(not isinstance(np.ma.masked.copy(), MC)) + def test_immutable(self): + assert_raises(ValueError, operator.setitem, np.ma.masked.data, (), 1) + assert_raises(ValueError, operator.setitem, np.ma.masked.mask, (), False) + + view = np.ma.masked.view(np.ma.MaskedArray) + assert_raises(ValueError, operator.setitem, view, (), 1) + assert_raises(ValueError, operator.setitem, view.data, (), 1) + assert_raises(ValueError, operator.setitem, view.mask, (), False) + def test_masked_array(): a = np.ma.array([0, 1, 2, 3], mask=[0, 0, 1, 0]) |