diff options
author | Eric Wieser <wieser.eric@gmail.com> | 2017-06-30 22:05:06 +0100 |
---|---|---|
committer | Eric Wieser <wieser.eric@gmail.com> | 2017-06-30 22:28:07 +0100 |
commit | a5322429c56fb2f906e49e4ae5b87533269d8150 (patch) | |
tree | 8e4b5c5564ec1d53b3ba10ec16889c02d5c3955f /numpy/ma | |
parent | 7254888b9ffe1f26ce59600ad31b7d4c9135c13d (diff) | |
download | numpy-a5322429c56fb2f906e49e4ae5b87533269d8150.tar.gz |
BUG: np.ma.masked does not preserve identity through pickle
It's still possible to create duplicate MaskedConstants through `.copy()`
Diffstat (limited to 'numpy/ma')
-rw-r--r-- | numpy/ma/core.py | 6 | ||||
-rw-r--r-- | numpy/ma/tests/test_core.py | 10 |
2 files changed, 15 insertions, 1 deletions
diff --git a/numpy/ma/core.py b/numpy/ma/core.py index 2c62019cd..5cf859b75 100644 --- a/numpy/ma/core.py +++ b/numpy/ma/core.py @@ -6190,8 +6190,12 @@ class MaskedConstant(MaskedArray): _mask = mask = np.array(True) _baseclass = ndarray + __singleton = None + def __new__(self): - return self._data.view(self) + if self.__singleton is None: + self.__singleton = self._data.view(self) + return self.__singleton def __array_finalize__(self, obj): return diff --git a/numpy/ma/tests/test_core.py b/numpy/ma/tests/test_core.py index 0a4840d41..345666317 100644 --- a/numpy/ma/tests/test_core.py +++ b/numpy/ma/tests/test_core.py @@ -4738,6 +4738,16 @@ class TestMaskedConstant(TestCase): assert_equal(repr(np.ma.masked), 'masked') assert_not_equal(repr(np.ma.masked.copy()), 'masked') + def test_pickle(self): + from io import BytesIO + import pickle + + with BytesIO() as f: + pickle.dump(np.ma.masked, f) + f.seek(0) + res = pickle.load(f) + assert_(res is np.ma.masked) + def test_masked_array(): a = np.ma.array([0, 1, 2, 3], mask=[0, 0, 1, 0]) |