[pypy-svn] r25802 - pypy/dist/pypy/rpython

arigo at codespeak.net arigo at codespeak.net
Thu Apr 13 17:14:35 CEST 2006


Author: arigo
Date: Thu Apr 13 17:14:34 2006
New Revision: 25802

Added:
   pypy/dist/pypy/rpython/rslice.py
Log:
Antonio: did you forget to add this file, or were just tricked
by CPython continuing to import rslice.pyc after the .py file
was deleted?

BTW two ootypesystem tests fail, looking for 'getitem' when
there is only 'getitem_nonneg' on List.


Added: pypy/dist/pypy/rpython/rslice.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/rpython/rslice.py	Thu Apr 13 17:14:34 2006
@@ -0,0 +1,56 @@
+from pypy.rpython.lltypesystem.lltype import Signed, Void
+from pypy.objspace.flow.model import Constant
+from pypy.annotation import model as annmodel
+
+
+def select_slice_repr(self):
+    # Select which one of the three prebuilt reprs to use.
+    # Return a name.
+    if not self.step.is_constant() or self.step.const not in (None, 1):
+        raise TyperError("only supports slices with step 1")
+    if (self.start.is_constant() and self.start.const in (None, 0) and
+        self.stop.is_constant() and self.stop.const == -1):
+        return "minusone_slice_repr"    # [:-1]
+    if isinstance(self.start, annmodel.SomeInteger):
+        if not self.start.nonneg:
+            raise TyperError("slice start must be proved non-negative")
+    if isinstance(self.stop, annmodel.SomeInteger):
+        if not self.stop.nonneg:
+            raise TyperError("slice stop must be proved non-negative")
+    if self.stop.is_constant() and self.stop.const is None:
+        return "startonly_slice_repr"
+    else:
+        return "startstop_slice_repr"
+
+class __extend__(annmodel.SomeSlice):
+    def rtyper_makerepr(self, rtyper):
+        return getattr(rtyper.type_system.rslice, select_slice_repr(self))
+        
+    def rtyper_makekey(self):
+        return self.__class__, select_slice_repr(self)
+
+
+def rtype_newslice(hop):
+    sig = []
+    for s in hop.args_s:
+        if s.is_constant() and s.const is None:
+            sig.append(Void)
+        else:
+            sig.append(Signed)
+    v_start, v_stop, v_step = hop.inputargs(*sig)
+    assert isinstance(v_step, Constant) and v_step.value in (None, 1)
+    if isinstance(v_start, Constant) and v_start.value is None:
+        v_start = hop.inputconst(Signed, 0)
+    if (isinstance(v_start, Constant) and v_start.value == 0 and
+        isinstance(v_stop, Constant) and v_stop.value == -1):
+        # [:-1] slice
+        return hop.inputconst(Void, slice(None,-1))
+    if isinstance(v_stop, Constant) and v_stop.value is None:
+        # start-only slice
+        # NB. cannot just return v_start in case it is a constant
+        return hop.genop('same_as', [v_start],
+            resulttype=hop.rtyper.type_system.rslice.startonly_slice_repr)
+    else:
+        # start-stop slice
+        return hop.gendirectcall(hop.rtyper.type_system.rslice.ll_newslice,
+                                 v_start, v_stop)



More information about the Pypy-commit mailing list