diff options
| author | Eric Wieser <wieser.eric@gmail.com> | 2017-06-30 22:29:25 +0100 |
|---|---|---|
| committer | Eric Wieser <wieser.eric@gmail.com> | 2017-06-30 22:29:25 +0100 |
| commit | 4ff8d0aa471c8f2c463e50e24b38e71a94eb7c36 (patch) | |
| tree | 5075d48bd5ee448733e739e76ed065f3d45a00cf /numpy/ma/core.py | |
| parent | a5322429c56fb2f906e49e4ae5b87533269d8150 (diff) | |
| download | numpy-4ff8d0aa471c8f2c463e50e24b38e71a94eb7c36.tar.gz | |
BUG: Prevent copies of np.ma.MaskedConstant from being created by .view.
This seems to solve the problem everywhere. It's not clear if this works on PyPy.
Fixes gh-9328
Diffstat (limited to 'numpy/ma/core.py')
| -rw-r--r-- | numpy/ma/core.py | 33 |
1 files changed, 18 insertions, 15 deletions
diff --git a/numpy/ma/core.py b/numpy/ma/core.py index 5cf859b75..94613ef2f 100644 --- a/numpy/ma/core.py +++ b/numpy/ma/core.py @@ -6184,27 +6184,33 @@ isMA = isMaskedArray # backward compatibility class MaskedConstant(MaskedArray): - # We define the masked singleton as a float for higher precedence. - # Note that it can be tricky sometimes w/ type comparison - _data = data = np.array(0.) - _mask = mask = np.array(True) - _baseclass = ndarray __singleton = None def __new__(self): if self.__singleton is None: - self.__singleton = self._data.view(self) + # We define the masked singleton as a float for higher precedence. + # 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) + return self.__singleton def __array_finalize__(self, obj): - return - - def __array_prepare__(self, obj, context=None): - return self.view(MaskedArray).__array_prepare__(obj, context) + if self.__singleton is None: + # this handles the `.view` in __new__, which we want to copy across + # properties normally + return super(MaskedConstant, self).__array_finalize__(obj) + elif self is self.__singleton: + # not clear how this can happen, play it safe + pass + else: + # everywhere else, we want to downcast to MaskedArray, to prevent a + # duplicate maskedconstant. + self.__class__ = MaskedArray + self.__array_finalize__(obj) - def __array_wrap__(self, obj, context=None): - return self.view(MaskedArray).__array_wrap__(obj, context) def __str__(self): return str(masked_print_option._display) @@ -6216,9 +6222,6 @@ class MaskedConstant(MaskedArray): # something is wrong, make it obvious return object.__repr__(self) - def flatten(self): - return masked_array([self._data], dtype=float, mask=[True]) - def __reduce__(self): """Override of MaskedArray's __reduce__. """ |
