[pypy-svn] r32402 - in pypy/dist/pypy: interpreter objspace/cpy

arigo at codespeak.net arigo at codespeak.net
Sun Sep 17 11:28:43 CEST 2006


Author: arigo
Date: Sun Sep 17 11:28:41 2006
New Revision: 32402

Modified:
   pypy/dist/pypy/interpreter/baseobjspace.py
   pypy/dist/pypy/objspace/cpy/objspace.py
Log:
A helper for mixedmodule implementors, to decode index and slice access
for sequence-like objects.



Modified: pypy/dist/pypy/interpreter/baseobjspace.py
==============================================================================
--- pypy/dist/pypy/interpreter/baseobjspace.py	(original)
+++ pypy/dist/pypy/interpreter/baseobjspace.py	Sun Sep 17 11:28:41 2006
@@ -697,6 +697,32 @@
         args = Arguments(self, list(posargs_w))
         return self.call_args(w_func, args)
 
+    def decode_index(self, w_index_or_slice, seqlength):
+        """Helper for custom sequence implementations
+             -> (index, 0, 0) or
+                (start, stop, step)
+        """
+        if self.is_true(self.isinstance(w_index_or_slice, self.w_slice)):
+            w_indices = self.call_method(w_index_or_slice, "indices",
+                                         self.wrap(seqlength))
+            w_start, w_stop, w_step = self.unpackiterable(w_indices, 3)
+            start = self.int_w(w_start)
+            stop  = self.int_w(w_stop)
+            step  = self.int_w(w_step)
+            if step == 0:
+                raise OperationError(self.w_ValueError,
+                                     self.wrap("slice step cannot be zero"))
+        else:
+            start = self.int_w(w_index_or_slice)
+            if start < 0:
+                start += seqlength
+            if not (0 <= start < seqlength):
+                raise OperationError(self.w_IndexError,
+                                     self.wrap("index out of range"))
+            stop  = 0
+            step  = 0
+        return start, stop, step
+
 class AppExecCache(SpaceCache):
     def build(cache, source):
         """ NOT_RPYTHON """

Modified: pypy/dist/pypy/objspace/cpy/objspace.py
==============================================================================
--- pypy/dist/pypy/objspace/cpy/objspace.py	(original)
+++ pypy/dist/pypy/objspace/cpy/objspace.py	Sun Sep 17 11:28:41 2006
@@ -28,6 +28,7 @@
     w_unicode        = W_Object(unicode)
     w_type           = W_Object(type)
     w_instance       = W_Object(types.InstanceType)
+    w_slice          = W_Object(slice)
 
     w_hex            = W_Object(hex) # no C API function to do that :-(
     w_oct            = W_Object(oct)



More information about the Pypy-commit mailing list