From 4ff8d0aa471c8f2c463e50e24b38e71a94eb7c36 Mon Sep 17 00:00:00 2001 From: Eric Wieser Date: Fri, 30 Jun 2017 22:29:25 +0100 Subject: 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 --- numpy/ma/core.py | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) (limited to 'numpy/ma/core.py') 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__. """ -- cgit v1.2.1