Author: Justin Peel <notmuchtot...@gmail.com>
Branch: 
Changeset: r45890:0055fef080ed
Date: 2011-07-22 18:10 -0600
http://bitbucket.org/pypy/pypy/changeset/0055fef080ed/

Log:    numpy: added copy and made set slice work when setting to part of
        the same array

diff --git a/pypy/module/micronumpy/interp_numarray.py 
b/pypy/module/micronumpy/interp_numarray.py
--- a/pypy/module/micronumpy/interp_numarray.py
+++ b/pypy/module/micronumpy/interp_numarray.py
@@ -205,6 +205,9 @@
     def get_concrete(self):
         raise NotImplementedError
 
+    def descr_copy(self, space):
+        return new_numarray(space, self)
+
     def descr_get_shape(self, space):
         return space.newtuple([self.descr_len(space)])
 
@@ -241,7 +244,16 @@
             # Single index
             self.get_concrete().setitem(start, space.float_w(w_value))
         else:
-            self.get_concrete().setslice(space, start, stop, step, 
+            concrete = self.get_concrete()
+            if isinstance(w_value, BaseArray):
+                # for now we just copy if setting part of an array from 
+                # part of itself. can be improved.
+                if concrete.get_root_storage() is \
+                                    w_value.get_concrete().get_root_storage():
+                    w_value = new_numarray(space, w_value)
+            else:
+                w_value = convert_to_array(space, w_value)
+            concrete.setslice(space, start, stop, step, 
                                                slice_length, w_value)
 
     def descr_mean(self, space):
@@ -440,11 +452,13 @@
             self.parent = parent
         self.size = slice_length
 
+    def get_root_storage(self):
+        self.parent.storage
+
     def find_size(self):
         return self.size
 
     def setslice(self, space, start, stop, step, slice_length, arr):
-        arr = convert_to_array(space, arr)
         start = self.calc_index(start)
         if stop != -1:
             stop = self.calc_index(stop)
@@ -471,6 +485,9 @@
     def get_concrete(self):
         return self
 
+    def get_root_storage(self):
+        return self.storage
+
     def find_size(self):
         return self.size
 
@@ -488,8 +505,6 @@
         self.storage[item] = value
 
     def setslice(self, space, start, stop, step, slice_length, arr):
-        if not isinstance(arr, BaseArray):
-            arr = convert_to_array(space, arr)
         if step > 0:
             self._sliceloop1(start, stop, step, arr, self)
         else:
@@ -525,6 +540,7 @@
     'numarray',
     __new__ = interp2app(descr_new_numarray),
 
+    copy = interp2app(BaseArray.descr_copy),
     shape = GetSetProperty(BaseArray.descr_get_shape),
 
     __len__ = interp2app(BaseArray.descr_len),
diff --git a/pypy/module/micronumpy/test/test_numarray.py 
b/pypy/module/micronumpy/test/test_numarray.py
--- a/pypy/module/micronumpy/test/test_numarray.py
+++ b/pypy/module/micronumpy/test/test_numarray.py
@@ -38,6 +38,13 @@
         a[2] = 4
         assert a[2] == 4
 
+    def test_copy(self):
+        from numpy import array
+        a = array(range(5))
+        b = a.copy()
+        for i in xrange(5):
+            assert b[i] == a[i]
+
     def test_iterator_init(self):
         from numpy import array
         a = array(range(5))
@@ -99,6 +106,9 @@
         a[1:4:2] = b
         assert a[1] == 0.
         assert a[3] == 1.
+        b[::-1] = b
+        assert b[0] == 1.
+        assert b[1] == 0.
 
     def test_setslice_of_slice_array(self):
         from numpy import array, zeros
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to