Author: Richard Plangger <[email protected]>
Branch: py3.5-memoryview
Changeset: r86532:c771970fb98a
Date: 2016-08-25 12:38 +0200
http://bitbucket.org/pypy/pypy/changeset/c771970fb98a/

Log:    progress on implementing tuple indexing (almost done with the logic)

diff --git a/pypy/module/micronumpy/descriptor.py 
b/pypy/module/micronumpy/descriptor.py
--- a/pypy/module/micronumpy/descriptor.py
+++ b/pypy/module/micronumpy/descriptor.py
@@ -1048,7 +1048,6 @@
             return dtype
         if name[0] in 'VSUca' or name[0] in '<>=|' and name[1] in 'VSUca':
             return variable_dtype(space, name)
-        import pdb; pdb.set_trace()
         raise oefmt(space.w_TypeError, 'data type "%s" not understood', name)
     elif space.isinstance_w(w_dtype, space.w_list):
         return _set_metadata_and_copy( space, w_metadata,
diff --git a/pypy/module/micronumpy/test/dummy_module.py 
b/pypy/module/micronumpy/test/dummy_module.py
--- a/pypy/module/micronumpy/test/dummy_module.py
+++ b/pypy/module/micronumpy/test/dummy_module.py
@@ -20,6 +20,8 @@
 for t in types:
     globals()[t] = dtype(t).type
 
+# removed 'string' and 'unicode' from that list to handle an error in
+# make_new_dtype (micronumpy/descriptor.py)
 types = ['bool', 'int', 'float', 'complex', 'str', 'object']
 for t in types:
     globals()[t + '_'] = dtype(t).type
diff --git a/pypy/objspace/std/memoryobject.py 
b/pypy/objspace/std/memoryobject.py
--- a/pypy/objspace/std/memoryobject.py
+++ b/pypy/objspace/std/memoryobject.py
@@ -79,9 +79,51 @@
         fmtiter.interpret(self.format * self.getlength())
         return space.newlist(fmtiter.result_w)
 
+    def _start_from_tuple(self, space, w_tuple):
+        start = 0
+
+        view = self.buf
+        length = space.len(w_tuple)
+        for i, w_obj in enumerate(w_tuple.getitems_unroll()):
+            value = w_obj.int_w(space)
+            start = self.lookup_dimension(start, dim, index)
+        return start
+
+    def lookup_dimension(self, space, start, dim, index):
+        return start
+
+    def _getitem_tuple_indexed(self, w_index):
+        view = self.buf
+
+        fmt = view.getformat() # TODO adjust format?
+
+        length = space.len_w(w_index)
+        ndim = view.getndim()
+        if length < ndim:
+            raise OperationError(self.w_NotImplementedError, \
+                    self.wrap("sub-views are not implemented"))
+
+        if length > ndim:
+            raise oefmt(self.w_NotImplementedError, \
+                    "cannot index %d-dimension view with %d-element tuple",
+                    length, ndim)
+
+        start = self._start_from_tuple(space, w_index)
+
+        buf = SubBuffer(self.buf, start, self.itemsize)
+        fmtiter = UnpackFormatIterator(space, buf)
+        fmtiter.interpret(fmt)
+        return fmtiter.result_w[0]
+
+
     def descr_getitem(self, space, w_index):
         self._check_released(space)
-        start, stop, step, size = space.decode_index4(w_index, 
self.getlength())
+
+        if self.isinstance_w(w_index, self.w_tuple):
+            return self._getitem_tuple_indexed(space, w_index)
+
+        start, stop, step, size = space.decode_index4_or_tuple_index(w_index, \
+                                                              self.getlength())
         itemsize = self.buf.getitemsize()
         if itemsize > 1:
             start *= itemsize
diff --git a/pypy/objspace/std/test/test_memoryobject.py 
b/pypy/objspace/std/test/test_memoryobject.py
--- a/pypy/objspace/std/test/test_memoryobject.py
+++ b/pypy/objspace/std/test/test_memoryobject.py
@@ -173,5 +173,5 @@
 
     def test_tuple_indexing(self):
         from numpy import ndarray
-        content = ndarray(list(range(12)))
+        content = ndarray(list(range(12))).reshape(3,4)
         assert memoryview(content)[0,0] == 0
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to