[pypy-svn] r58011 - in pypy/branch/tuple-nonresizable-395/pypy: interpreter objspace/std

fijal at codespeak.net fijal at codespeak.net
Tue Sep 9 18:57:09 CEST 2008


Author: fijal
Date: Tue Sep  9 18:57:08 2008
New Revision: 58011

Modified:
   pypy/branch/tuple-nonresizable-395/pypy/interpreter/baseobjspace.py
   pypy/branch/tuple-nonresizable-395/pypy/objspace/std/listobject.py
Log:
Some IN-PROGRESS checkin, playing around


Modified: pypy/branch/tuple-nonresizable-395/pypy/interpreter/baseobjspace.py
==============================================================================
--- pypy/branch/tuple-nonresizable-395/pypy/interpreter/baseobjspace.py	(original)
+++ pypy/branch/tuple-nonresizable-395/pypy/interpreter/baseobjspace.py	Tue Sep  9 18:57:08 2008
@@ -8,6 +8,7 @@
 from pypy.tool.cache import Cache
 from pypy.tool.uid import HUGEVAL_BYTES
 from pypy.rlib.objectmodel import we_are_translated
+from pypy.rlib.debug import make_sure_not_resized
 import os, sys
 
 __all__ = ['ObjSpace', 'OperationError', 'Wrappable', 'W_Root']
@@ -657,6 +658,12 @@
                                    (i, plural))
         return items
 
+    def immutableiterable(self, w_iterable):
+        """ More or less the same as unpackiterable, but does not return
+        a copy. Please don't modify the result
+        """
+        return make_sure_not_resized(self.unpackiterable(w_iterable)[:])
+
     def unpacktuple(self, w_tuple, expected_length=-1):
         """Same as unpackiterable(), but only for tuples.
         Only use for bootstrapping or performance reasons."""

Modified: pypy/branch/tuple-nonresizable-395/pypy/objspace/std/listobject.py
==============================================================================
--- pypy/branch/tuple-nonresizable-395/pypy/objspace/std/listobject.py	(original)
+++ pypy/branch/tuple-nonresizable-395/pypy/objspace/std/listobject.py	Tue Sep  9 18:57:08 2008
@@ -257,9 +257,13 @@
     l = w_list2.wrappeditems
     return _setitem_slice_helper(space, w_list, w_slice, l, len(l))
 
+def setitem__List_Slice_Tuple(space, w_tuple, w_slice, w_iterable):
+    l = w_tuple.wrappeditems
+    return _setitem_slice_helper2(space, w_tuple, w_slice, l, len(l))
+
 def setitem__List_Slice_ANY(space, w_list, w_slice, w_iterable):
-    l = space.unpackiterable(w_iterable)
-    return _setitem_slice_helper(space, w_list, w_slice, l, len(l))
+    l = space.immutableiterable(w_iterable)
+    return _setitem_slice_helper2(space, w_list, w_slice, l, len(l))
 
 def _setitem_slice_helper(space, w_list, w_slice, sequence2, len2):
     oldsize = len(w_list.wrappeditems)
@@ -306,6 +310,57 @@
         start += step
     return space.w_None
 
+
+def _setitem_slice_helper2(space, w_list, w_slice, sequence2, len2):
+    make_sure_not_resized(sequence2)
+    oldsize = len(w_list.wrappeditems)
+    start, stop, step, slicelength = w_slice.indices4(space, oldsize)
+    assert slicelength >= 0
+    items = w_list.wrappeditems
+
+    if step == 1:  # Support list resizing for non-extended slices
+        delta = len2 - slicelength
+        if delta >= 0:
+            newsize = oldsize + delta
+            # XXX support this in rlist!
+            items += [None] * delta
+            lim = start+len2
+            i = newsize - 1
+            while i >= lim:
+                items[i] = items[i-delta]
+                i -= 1
+        else:
+            # shrinking requires the careful memory management of _del_slice()
+            _del_slice(w_list, start, start-delta)
+    elif len2 != slicelength:  # No resize for extended slices
+        raise OperationError(space.w_ValueError, space.wrap("attempt to "
+              "assign sequence of size %d to extended slice of size %d" %
+              (len2,slicelength)))
+
+    if sequence2 is items:
+        if step > 0:
+            # Always copy starting from the right to avoid
+            # having to make a shallow copy in the case where
+            # the source and destination lists are the same list.
+            i = len2 - 1
+            start += i*step
+            while i >= 0:
+                items[start] = sequence2[i]
+                start -= step
+                i -= 1
+            return space.w_None
+        else:
+            # Make a shallow copy to more easily handle the reversal case
+            sequence2 = list(sequence2)
+    for i in range(len2):
+        items[start] = sequence2[i]
+        start += step
+    return space.w_None
+#_setitem_slice_helper.func_name = name
+
+#_setitem_slice_helper = _new_slice_helper('_setitem_slice_helper')
+#_setitem_slice_helper2 = _new_slice_helper('_setitem_slice_helper2')
+
 app = gateway.applevel("""
     def listrepr(currently_in_repr, l):
         'The app-level part of repr().'



More information about the Pypy-commit mailing list