Repository: numpy/numpy

Issue:
Test error for scipy 0.15.0.dev-5d197ed
The first bad commit is d8fd28389adb491e24b7cdc25cd1b20f539310c3, the isolated error is

```
Traceback (most recent call last):
  File "/home/charris/scipy-test-fail.py", line 53, in test_trim
    assert_equal(trimx._mask.ravel(), expected)
  File "/home/charris/.local/lib/python2.7/site-packages/numpy/ma/testutils.py", line 123, in assert_equal
    return assert_array_equal(actual, desired, err_msg)
  File "/home/charris/.local/lib/python2.7/site-packages/numpy/ma/testutils.py", line 196, in assert_array_equal
    header='Arrays are not equal')
  File "/home/charris/.local/lib/python2.7/site-packages/numpy/ma/testutils.py", line 189, in assert_array_compare
    verbose=verbose, header=header)
  File "/home/charris/.local/lib/python2.7/site-packages/numpy/testing/utils.py", line 651, in assert_array_compare
    raise AssertionError(msg)
AssertionError: 
Arrays are not equal

(mismatch 9.09090909091%)
 x: array([ True,  True,  True,  True,  True,  True,  True,  True,  True,
        True,  True,  True,  True,  True,  True,  True,  True,  True,
        True,  True, False, False, False, False, False, False, False,...
 y: array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0,
       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,...

----------------------------------------------------------------------
Ran 1 test in 0.005s
```



Discussion:
Looks like the problem is returning a view rather than a copy.

Or more precisely, not making a copy and returning the original array. Probably the easiest fix is to revert this change, at least for numpy 1.9.


Resolving patch:
diff --git a/numpy/ma/core.py b/numpy/ma/core.py
--- a/numpy/ma/core.py
+++ b/numpy/ma/core.py
@@ -7018,8 +7018,6 @@ def asarray(a, dtype=None, order=None):
     <class 'numpy.ma.core.MaskedArray'>
 
     """
-    if dtype is None and type(a) is MaskedArray:
-        return a
     return masked_array(a, dtype=dtype, copy=False, keep_mask=True, subok=False)
 
 def asanyarray(a, dtype=None):
@@ -7065,8 +7063,6 @@ def asanyarray(a, dtype=None):
     <class 'numpy.ma.core.MaskedArray'>
 
     """
-    if dtype is None and isinstance(a, MaskedArray):
-        return a
     return masked_array(a, dtype=dtype, copy=False, keep_mask=True, subok=True)
 
 


Test patch:
diff --git a/numpy/ma/tests/test_core.py b/numpy/ma/tests/test_core.py
--- a/numpy/ma/tests/test_core.py
+++ b/numpy/ma/tests/test_core.py
@@ -186,24 +186,11 @@ def test_asarray(self):
         (x, y, a10, m1, m2, xm, ym, z, zm, xf) = self.d
         xm.fill_value = -9999
         xm._hardmask = True
-        xmm = asarray(xm, xm.dtype)
+        xmm = asarray(xm)
         assert_equal(xmm._data, xm._data)
         assert_equal(xmm._mask, xm._mask)
         assert_equal(xmm.fill_value, xm.fill_value)
         assert_equal(xmm._hardmask, xm._hardmask)
-        # address gh-4043
-        self.assertTrue(xm is asarray(xm))
-
-    def test_asanyarray(self):
-        class M(MaskedArray):
-            pass
-        xm = M([])
-        self.assertTrue(xm is not asarray(xm))
-        # address gh-4043
-        self.assertTrue(xm is asanyarray(xm))
-        test = asanyarray(xm, np.int64)
-        self.assertTrue(isinstance(test, M))
-        assert_equal(test.dtype, np.int64)
 
     def test_fix_invalid(self):
         # Checks fix_invalid.