diff options
author | Eric Wieser <wieser.eric@gmail.com> | 2017-06-30 22:52:13 +0100 |
---|---|---|
committer | Eric Wieser <wieser.eric@gmail.com> | 2017-06-30 22:52:13 +0100 |
commit | 834996792fcb0738a325d76f23154ef52f7e74c8 (patch) | |
tree | 173d6709f0166af0d2071a129a61090c48ec3f9a /numpy/ma/core.py | |
parent | 4ff8d0aa471c8f2c463e50e24b38e71a94eb7c36 (diff) | |
download | numpy-834996792fcb0738a325d76f23154ef52f7e74c8.tar.gz |
BUG: np.ma.masked is mutable
Diffstat (limited to 'numpy/ma/core.py')
-rw-r--r-- | numpy/ma/core.py | 19 |
1 files changed, 18 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 |