[pypy-svn] r78374 - in pypy/branch/fast-forward/pypy/objspace/std: . test

afa at codespeak.net afa at codespeak.net
Wed Oct 27 19:53:31 CEST 2010


Author: afa
Date: Wed Oct 27 19:53:29 2010
New Revision: 78374

Modified:
   pypy/branch/fast-forward/pypy/objspace/std/bytearrayobject.py
   pypy/branch/fast-forward/pypy/objspace/std/test/test_bytes.py
Log:
Slice assignment cannot change the size in RPython.
unfortunately, this is only true when translated...


Modified: pypy/branch/fast-forward/pypy/objspace/std/bytearrayobject.py
==============================================================================
--- pypy/branch/fast-forward/pypy/objspace/std/bytearrayobject.py	(original)
+++ pypy/branch/fast-forward/pypy/objspace/std/bytearrayobject.py	Wed Oct 27 19:53:29 2010
@@ -397,9 +397,31 @@
     if step != 1:
         raise OperationError(space.w_NotImplementedError,
                              space.wrap("fixme: only step=1 for the moment"))
+    _setitem_helper(w_bytearray, start, stop, slicelength,
+                    space.str_w(w_other))
+
+def _setitem_helper(w_bytearray, start, stop, slicelength, data):
     assert start >= 0
     assert stop >= 0
-    w_bytearray.data[start:stop] = [c for c in space.str_w(w_other)]
+    step = 1
+    len2 = len(data)
+    delta = slicelength - len2
+    if delta < 0:
+        delta = -delta
+        newsize = len(w_bytearray.data) + delta
+        w_bytearray.data += ['\0'] * delta
+        lim = start + len2
+        i = newsize - 1
+        while i >= lim:
+            w_bytearray.data[i] = w_bytearray.data[i-delta]
+            i -= 1
+    elif start >= 0:
+        del w_bytearray.data[start:start+delta]
+    else:
+        assert delta == 0
+    for i in range(len2):
+        w_bytearray.data[start] = data[i]
+        start += step
 
 # __________________________________________________________
 # Buffer interface

Modified: pypy/branch/fast-forward/pypy/objspace/std/test/test_bytes.py
==============================================================================
--- pypy/branch/fast-forward/pypy/objspace/std/test/test_bytes.py	(original)
+++ pypy/branch/fast-forward/pypy/objspace/std/test/test_bytes.py	Wed Oct 27 19:53:29 2010
@@ -175,6 +175,8 @@
         assert b == 'ABC...defghi'
         b[3:6] = '()'
         assert b == 'ABC()defghi'
+        b[6:6] = '<<'
+        assert b == 'ABC()d<<efghi'
 
     def test_buffer(self):
         b = bytearray('abcdefghi')



More information about the Pypy-commit mailing list