[Numpy-svn] r5791 - in trunk/numpy/ma: . tests

numpy-svn@scip... numpy-svn@scip...
Fri Sep 5 18:38:47 CDT 2008


Author: pierregm
Date: 2008-09-05 18:38:44 -0500 (Fri, 05 Sep 2008)
New Revision: 5791

Modified:
   trunk/numpy/ma/core.py
   trunk/numpy/ma/tests/test_core.py
Log:
* fixed deepcopy of masked arrays (bug #906)

Modified: trunk/numpy/ma/core.py
===================================================================
--- trunk/numpy/ma/core.py	2008-09-05 16:51:06 UTC (rev 5790)
+++ trunk/numpy/ma/core.py	2008-09-05 23:38:44 UTC (rev 5791)
@@ -3123,6 +3123,14 @@
         return (_mareconstruct,
                 (self.__class__, self._baseclass, (0,), 'b', ),
                 self.__getstate__())
+    #
+    def __deepcopy__(self, memo={}):
+        from copy import deepcopy
+        copied = MaskedArray.__new__(type(self), self, copy=True)
+        memo[id(self)] = copied
+        for (k,v) in self.__dict__.iteritems():
+            copied.__dict__[k] = deepcopy(v, memo)
+        return copied
 
 
 def _mareconstruct(subtype, baseclass, baseshape, basetype,):

Modified: trunk/numpy/ma/tests/test_core.py
===================================================================
--- trunk/numpy/ma/tests/test_core.py	2008-09-05 16:51:06 UTC (rev 5790)
+++ trunk/numpy/ma/tests/test_core.py	2008-09-05 23:38:44 UTC (rev 5791)
@@ -330,6 +330,24 @@
         assert_not_equal(y._mask.ctypes.data, x._mask.ctypes.data)
 
 
+    def test_deepcopy(self):
+        from copy import deepcopy
+        a = array([0,1,2], mask=[False,True,False])
+        copied = deepcopy(a)
+        assert_equal(copied.mask, a.mask)
+        assert_not_equal(id(a._mask), id(copied._mask))
+        #
+        copied[1] = 1
+        assert_equal(copied.mask,[0,0,0])
+        assert_equal(a.mask, [0,1,0])
+        #
+        copied = deepcopy(a)
+        assert_equal(copied.mask, a.mask)
+        copied.mask[1] = False
+        assert_equal(copied.mask,[0,0,0])
+        assert_equal(a.mask, [0,1,0])
+
+
     def test_pickling(self):
         "Tests pickling"
         import cPickle



More information about the Numpy-svn mailing list