[pypy-svn] pypy default: (antocuni, david) Fix array slice assignment with self

bivab commits-noreply at bitbucket.org
Thu Jan 20 16:02:07 CET 2011


Author: David Schneider <david.schneider at picle.org>
Branch: 
Changeset: r41043:1591039519fd
Date: 2011-01-20 15:57 +0100
http://bitbucket.org/pypy/pypy/changeset/1591039519fd/

Log:	(antocuni, david) Fix array slice assignment with self

diff --git a/pypy/module/array/test/test_array.py b/pypy/module/array/test/test_array.py
--- a/pypy/module/array/test/test_array.py
+++ b/pypy/module/array/test/test_array.py
@@ -853,3 +853,8 @@
         import _rawffi
         data = _rawffi.charp2string(bi[0])
         assert data[0:3] == 'Hi!'
+
+    def test_array_reverse_slice_assign_self(self):
+        a = self.array('b', range(4))
+        a[::-1] = a
+        assert a == self.array('b', [3, 2, 1, 0])

diff --git a/pypy/module/array/interp_array.py b/pypy/module/array/interp_array.py
--- a/pypy/module/array/interp_array.py
+++ b/pypy/module/array/interp_array.py
@@ -362,10 +362,19 @@
             self.setlen(0)
             self.fromsequence(w_lst)
         else:
-            j = 0
-            for i in range(start, stop, step):
-                self.buffer[i] = w_item.buffer[j]
-                j += 1
+            if self is w_item:
+                with lltype.scoped_alloc(mytype.arraytype, self.allocated) as new_buffer:
+                    for i in range(self.len):
+                        new_buffer[i] = w_item.buffer[i]
+                    j = 0
+                    for i in range(start, stop, step):
+                        self.buffer[i] = new_buffer[j]
+                        j += 1
+            else:
+                j = 0
+                for i in range(start, stop, step):
+                    self.buffer[i] = w_item.buffer[j]
+                    j += 1
 
     def setslice__Array_ANY_ANY_ANY(space, self, w_i, w_j, w_x):
         space.setitem(self, space.newslice(w_i, w_j, space.w_None), w_x)


More information about the Pypy-commit mailing list