Commits

Ilya Osadchiy  committed cfbf398

numpy: receiving slice object as argument of descr_getitem

  • Participants
  • Parent commits c373c5f

Comments (0)

Files changed (1)

File pypy/module/micronumpy/interp_numarray.py

     def descr_len(self, space):
         return self.get_concrete().descr_len(space)
 
-    @unwrap_spec(item=int)
-    def descr_getitem(self, space, item):
-        return self.get_concrete().descr_getitem(space, item)
+#    unwrap_spec(item=int)
+    def descr_getitem(self, space, w_idx):
+        # TODO: indexation by tuples
+        start, stop, step, slice_length = space.decode_index4(w_idx, self.find_size())
+        if step == 0:
+            # Single index
+            return space.wrap(self.get_concrete().getitem(start))
+        else:
+            # Slice
+            signature = Signature()
+            res = SingleDimSlice(start, stop, step, slice_length, self, self.signature.transition(signature))
+            return space.wrap(res)
+            
 
     @unwrap_spec(item=int, value=float)
     def descr_setitem(self, space, item, value):
         self.invalidated()
         return self.get_concrete().descr_setitem(space, item, value)
 
-    @unwrap_spec(sta=int, sto=int)
-    def descr_getslice(self, space, sta, sto):
-        signature = Signature()
-        res = SingleDimSlice(sta, sto, self, self.signature.transition(signature))
-        return res
+#    @unwrap_spec(sta=int, sto=int)
+#    def descr_getslice(self, space, sta, sto):
+#        signature = Signature()
+#        res = SingleDimSlice(sta, sto, self, self.signature.transition(signature))
+#        return res
 
 class FloatWrapper(BaseArray):
     """
     def eval(self, i):
         return self.parent.eval(self.calc_index(i))
 
-    @unwrap_spec(item=int)
-    def descr_getitem(self, space, item):
-        return self.parent.descr_getitem(space, self.calc_index(item))
+#    @unwrap_spec(item=int)
+    def getitem(self, item):
+        return self.parent.getitem(self.calc_index(item))
 
     @unwrap_spec(item=int, value=float)
     def descr_setitem(self, space, item, value):
         return self.parent.descr_setitem(space, self.calc_index(item), value)
+
+    def descr_len(self, space):
+        return space.wrap(self.find_size())
         
 #    def calc_index(self, item):
 #        raise NotImplementedError
 
 class SingleDimSlice(ViewArray):
-    _immutable_fields_ = ["start", "stop", "step"]
+    _immutable_fields_ = ["start", "stop", "step", "size"]
 
-    def __init__(self, start, stop, parent, signature):
+    def __init__(self, start, stop, step, slice_length, parent, signature):
         ViewArray.__init__(self, parent, signature)
-        self.start = start #sl.start
-        l = parent.find_size()
-        if stop > l:
-            self.stop = l
-        else:
-            self.stop = stop #sl.stop
-        self.step = 1 #sl.step
+        self.start = start
+        self.stop = stop
+        self.step = step
+        self.size = slice_length
 
     def find_size(self):
-        return (self.stop - self.start) # FIXME divide by step
+        return self.size
 
     def calc_index(self, item):
         return (self.start + item * self.step)
     def descr_len(self, space):
         return space.wrap(self.size)
 
-    @unwrap_spec(item=int)
-    def descr_getitem(self, space, item):
-        item = self.getindex(space, item)
-        return space.wrap(self.storage[item])
+#    @unwrap_spec(item=int)
+    def getitem(self, item):
+#FIXME        item = self.getindex(space, item)
+        return self.storage[item]
 
     @unwrap_spec(item=int, value=float)
     def descr_setitem(self, space, item, value):
     __len__ = interp2app(BaseArray.descr_len),
     __getitem__ = interp2app(BaseArray.descr_getitem),
     __setitem__ = interp2app(BaseArray.descr_setitem),
-    __getslice__ = interp2app(BaseArray.descr_getslice),
+#    __getslice__ = interp2app(BaseArray.descr_getslice),
 
     __add__ = interp2app(BaseArray.descr_add),
     __sub__ = interp2app(BaseArray.descr_sub),