[pypy-svn] r31339 - in pypy/dist/pypy: annotation rpython rpython/test

arigo at codespeak.net arigo at codespeak.net
Wed Aug 16 12:51:03 CEST 2006


Author: arigo
Date: Wed Aug 16 12:51:01 2006
New Revision: 31339

Modified:
   pypy/dist/pypy/annotation/binaryop.py
   pypy/dist/pypy/annotation/model.py
   pypy/dist/pypy/rpython/rtuple.py
   pypy/dist/pypy/rpython/test/test_rtuple.py
Log:
RPython support for slicing tuples (constant indices only).


Modified: pypy/dist/pypy/annotation/binaryop.py
==============================================================================
--- pypy/dist/pypy/annotation/binaryop.py	(original)
+++ pypy/dist/pypy/annotation/binaryop.py	Wed Aug 16 12:51:01 2006
@@ -456,6 +456,15 @@
             return unionof(*tup1.items)
     getitem.can_only_throw = [IndexError]
 
+class __extend__(pairtype(SomeTuple, SomeSlice)):
+
+    def getitem((tup, slic)):
+        if not slic.is_immutable_constant():
+            raise Exception("not supported: "
+                            "tuple slicing with non-constant indexes")
+        return SomeTuple(tup.items[slic.const])
+    getitem.can_only_throw = []
+
 
 class __extend__(pairtype(SomeList, SomeInteger)):
     

Modified: pypy/dist/pypy/annotation/model.py
==============================================================================
--- pypy/dist/pypy/annotation/model.py	(original)
+++ pypy/dist/pypy/annotation/model.py	Wed Aug 16 12:51:01 2006
@@ -231,6 +231,10 @@
         self.start = start
         self.stop = stop
         self.step = step
+        if (start.is_immutable_constant() and
+            stop .is_immutable_constant() and
+            step .is_immutable_constant()):
+            self.const = slice(start.const, stop.const, step.const)
 
     def can_be_none(self):
         return False

Modified: pypy/dist/pypy/rpython/rtuple.py
==============================================================================
--- pypy/dist/pypy/rpython/rtuple.py	(original)
+++ pypy/dist/pypy/rpython/rtuple.py	Wed Aug 16 12:51:01 2006
@@ -6,6 +6,7 @@
 from pypy.rpython.rmodel import Repr, IntegerRepr, inputconst
 from pypy.rpython.rmodel import IteratorRepr
 from pypy.rpython.rmodel import externalvsinternal
+from pypy.rpython.rslice import AbstractSliceRepr
 from pypy.rpython.lltypesystem.lltype import Void, Signed 
 from pypy.rpython.rarithmetic import intmask
 
@@ -153,6 +154,21 @@
         index = v_index.value
         return r_tup.getitem(hop.llops, v_tuple, index)
 
+class __extend__(pairtype(AbstractTupleRepr, AbstractSliceRepr)):
+
+    def rtype_getitem((r_tup, r_slice), hop):
+        s_slice = hop.args_s[1]
+        if not s_slice.is_immutable_constant():
+            raise TyperError("non-constant tuple slicing index")
+        v_tup = hop.inputarg(r_tup, arg=0)
+
+        indices = range(len(r_tup.items_r))[s_slice.const]
+        assert len(indices) == len(hop.r_result.items_r)
+
+        items_v = [r_tup.getitem_internal(hop.llops, v_tup, i)
+                   for i in indices]
+        return hop.r_result.newtuple(hop.llops, hop.r_result, items_v)
+
 class __extend__(pairtype(AbstractTupleRepr, Repr)): 
     def rtype_contains((r_tup, r_item), hop):
         s_tup = hop.args_s[0]

Modified: pypy/dist/pypy/rpython/test/test_rtuple.py
==============================================================================
--- pypy/dist/pypy/rpython/test/test_rtuple.py	(original)
+++ pypy/dist/pypy/rpython/test/test_rtuple.py	Wed Aug 16 12:51:01 2006
@@ -249,6 +249,21 @@
             assert res[0] == res[1] == res[2] == []
         self.interpret(f, [])
 
+    def test_slice(self):
+        def g(n):
+            t = (1.5, "hello", n)
+            return t[1:] + t[:-1] + t[12:] + t[0:2]
+        def f(n):
+            res = g(n)
+            assert len(res) == 6
+            assert res[0] == "hello"
+            assert res[1] == n
+            assert res[2] == 1.5
+            assert res[3] == "hello"
+            assert res[4] == 1.5
+            assert res[5] == "hello"
+        self.interpret(f, [9])
+
 class TestLLtype(BaseTestRtuple, LLRtypeMixin):
     pass
 



More information about the Pypy-commit mailing list