diff options
| author | Eric Wieser <wieser.eric@gmail.com> | 2017-12-28 11:17:52 +0000 |
|---|---|---|
| committer | Eric Wieser <wieser.eric@gmail.com> | 2019-05-11 15:03:24 -0700 |
| commit | 599dbade766ad3fecc82afa308803a2e0082c149 (patch) | |
| tree | e8076014281d30c3562834252050f9df5c5f1d26 /numpy | |
| parent | 0f19dae081e6678902826b195e0d3857c5b4c2b3 (diff) | |
| download | numpy-599dbade766ad3fecc82afa308803a2e0082c149.tar.gz | |
API: Make MaskedArray.mask return a view, rather than the underlying mask
This prevents consumers from reshaping the mask in place, which breaks things
As a result, `x.mask is x.mask` returns `False`, but this was already true of `x.data is x.data`.
May also be related to gh-10270
Diffstat (limited to 'numpy')
| -rw-r--r-- | numpy/lib/tests/test_function_base.py | 2 | ||||
| -rw-r--r-- | numpy/ma/core.py | 4 | ||||
| -rw-r--r-- | numpy/ma/tests/test_core.py | 11 | ||||
| -rw-r--r-- | numpy/ma/tests/test_old_ma.py | 14 |
4 files changed, 20 insertions, 11 deletions
diff --git a/numpy/lib/tests/test_function_base.py b/numpy/lib/tests/test_function_base.py index 6d32c365a..0702c3856 100644 --- a/numpy/lib/tests/test_function_base.py +++ b/numpy/lib/tests/test_function_base.py @@ -945,7 +945,7 @@ class TestGradient(object): assert_equal(type(out), type(x)) # And make sure that the output and input don't have aliased mask # arrays - assert_(x.mask is not out.mask) + assert_(x._mask is not out._mask) # Also check that edge_order=2 doesn't alter the original mask x2 = np.ma.arange(5) x2[2] = np.ma.masked diff --git a/numpy/ma/core.py b/numpy/ma/core.py index 90aff6ec8..69fae3512 100644 --- a/numpy/ma/core.py +++ b/numpy/ma/core.py @@ -3449,7 +3449,9 @@ class MaskedArray(ndarray): # We could try to force a reshape, but that wouldn't work in some # cases. - return self._mask + # Return a view so that the dtype and shape cannot be changed in place + # This still preserves nomask by identity + return self._mask.view() @mask.setter def mask(self, value): diff --git a/numpy/ma/tests/test_core.py b/numpy/ma/tests/test_core.py index 1f80ba26d..fb3f1a810 100644 --- a/numpy/ma/tests/test_core.py +++ b/numpy/ma/tests/test_core.py @@ -374,12 +374,12 @@ class TestMaskedArray(object): y2a = array(x1, mask=m, copy=1) assert_(y2a._data.__array_interface__ != x1.__array_interface__) - #assert_( y2a.mask is not m) + #assert_( y2a._mask is not m) assert_(y2a._mask.__array_interface__ != m.__array_interface__) assert_(y2a[2] is masked) y2a[2] = 9 assert_(y2a[2] is not masked) - #assert_( y2a.mask is not m) + #assert_( y2a._mask is not m) assert_(y2a._mask.__array_interface__ != m.__array_interface__) assert_(allequal(y2a.mask, 0)) @@ -5203,3 +5203,10 @@ def test_fieldless_void(): mx = np.ma.array(x, mask=x) assert_equal(mx.dtype, x.dtype) assert_equal(mx.shape, x.shape) + + +def test_mask_shape_assignment_does_not_break_masked(): + a = np.ma.masked + b = np.ma.array(1, mask=a.mask) + b.shape = (1,) + assert_equal(a.mask.shape, ()) diff --git a/numpy/ma/tests/test_old_ma.py b/numpy/ma/tests/test_old_ma.py index 07bfb613f..1c523768d 100644 --- a/numpy/ma/tests/test_old_ma.py +++ b/numpy/ma/tests/test_old_ma.py @@ -270,7 +270,7 @@ class TestMa(object): y1 = array(x1, mask=m) assert_(y1._data is not x1) assert_(allequal(x1, y1._data)) - assert_(y1.mask is m) + assert_(y1._mask is m) y1a = array(y1, copy=0) # For copy=False, one might expect that the array would just @@ -280,19 +280,19 @@ class TestMa(object): y1._mask.__array_interface__) y2 = array(x1, mask=m3, copy=0) - assert_(y2.mask is m3) + assert_(y2._mask is m3) assert_(y2[2] is masked) y2[2] = 9 assert_(y2[2] is not masked) - assert_(y2.mask is m3) + assert_(y2._mask is m3) assert_(allequal(y2.mask, 0)) y2a = array(x1, mask=m, copy=1) - assert_(y2a.mask is not m) + assert_(y2a._mask is not m) assert_(y2a[2] is masked) y2a[2] = 9 assert_(y2a[2] is not masked) - assert_(y2a.mask is not m) + assert_(y2a._mask is not m) assert_(allequal(y2a.mask, 0)) y3 = array(x1 * 1.0, mask=m) @@ -318,14 +318,14 @@ class TestMa(object): assert_(x[3] is masked) assert_(x[4] is masked) x[[1, 4]] = [10, 40] - assert_(x.mask is m) + assert_(x._mask is m) assert_(x[3] is masked) assert_(x[4] is not masked) assert_(eq(x, [0, 10, 2, -1, 40])) x = array(d, mask=m2, copy=True) x.put([0, 1, 2], [-1, 100, 200]) - assert_(x.mask is not m2) + assert_(x._mask is not m2) assert_(x[3] is masked) assert_(x[4] is masked) assert_(eq(x, [-1, 100, 200, 0, 0])) |
