Author: Ronan Lamy <[email protected]>
Branch: PyBuffer
Changeset: r91044:23f969f08eb4
Date: 2017-04-12 00:12 +0100
http://bitbucket.org/pypy/pypy/changeset/23f969f08eb4/

Log:    Move tolist() implementation from W_MemoryObject to Buffer

diff --git a/pypy/interpreter/buffer.py b/pypy/interpreter/buffer.py
--- a/pypy/interpreter/buffer.py
+++ b/pypy/interpreter/buffer.py
@@ -105,8 +105,8 @@
 
 
     def w_getitem(self, space, idx):
+        from pypy.module.struct.formatiterator import UnpackFormatIterator
         offset = self.get_offset(space, 0, idx)
-        from pypy.module.struct.formatiterator import UnpackFormatIterator
         itemsize = self.getitemsize()
         if itemsize == 1:
             ch = self.as_binary()[offset]
@@ -137,6 +137,54 @@
             byteval = fmtiter.result.build()
             self.setbytes(offset, byteval)
 
+    def w_tolist(self, space):
+        dim = self.getndim()
+        fmt = self.getformat()
+        if dim == 0:
+            raise NotImplementedError
+        elif dim == 1:
+            n = self.getshape()[0]
+            values_w = [self.w_getitem(space, i) for i in range(n)]
+            return space.newlist(values_w)
+        else:
+            return self._tolist_rec(space, self.as_binary(), 0, 0, fmt)
+
+    def _tolist(self, space, buf, bytecount, itemsize, fmt, strides=None):
+        from pypy.module.struct.formatiterator import UnpackFormatIterator
+        # TODO: this probably isn't very fast
+        count = bytecount // itemsize
+        fmtiter = UnpackFormatIterator(space, buf)
+        # patch the length, necessary buffer might have offset
+        # which leads to wrong length calculation if e.g. the
+        # memoryview is reversed
+        fmtiter.length = bytecount
+        fmtiter.strides = strides
+        fmtiter.interpret(fmt * count)
+        return space.newlist(fmtiter.result_w)
+
+    def _tolist_rec(self, space, buf, start, idim, fmt):
+        strides = self.getstrides()
+        shape = self.getshape()
+        #
+        dim = idim + 1
+        stride = strides[idim]
+        itemsize = self.getitemsize()
+        dimshape = shape[idim]
+        #
+        if dim >= self.getndim():
+            bytecount = (stride * dimshape)
+            return self._tolist(space, buf, bytecount, itemsize, fmt, [stride])
+        items = [None] * dimshape
+
+        orig_buf = buf
+        for i in range(dimshape):
+            buf = SubBuffer(orig_buf, start, stride)
+            item = self._tolist_rec(space, buf, start, idim + 1, fmt)
+            items[i] = item
+            start += stride
+
+        return space.newlist(items)
+
 
 class SimpleBuffer(Buffer):
     _attrs_ = ['readonly', 'data']
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
@@ -96,55 +96,7 @@
 
     def descr_tolist(self, space):
         self._check_released(space)
-
-        buf = self.buf
-        dim = self.getndim()
-        fmt = self.getformat()
-        if dim == 0:
-            raise NotImplementedError
-        elif dim == 1:
-            itemsize = self.getitemsize()
-            n = self.getshape()[0]
-            values_w = [buf.w_getitem(space, i) for i in range(n)]
-            return space.newlist(values_w)
-        else:
-            return self._tolist_rec(space, buf.as_binary(), 0, 0, fmt)
-
-    def _tolist(self, space, buf, bytecount, itemsize, fmt, strides=None):
-        # TODO: this probably isn't very fast
-        count = bytecount // itemsize
-        fmtiter = UnpackFormatIterator(space, buf)
-        # patch the length, necessary buffer might have offset
-        # which leads to wrong length calculation if e.g. the
-        # memoryview is reversed
-        fmtiter.length = bytecount
-        fmtiter.strides = strides
-        fmtiter.interpret(fmt * count)
-        return space.newlist(fmtiter.result_w)
-
-    def _tolist_rec(self, space, buf, start, idim, fmt):
-        strides = self.getstrides()
-        shape = self.getshape()
-        #
-        dim = idim+1
-        stride = strides[idim]
-        itemsize = self.getitemsize()
-        dimshape = shape[idim]
-        #
-        if dim >= self.getndim():
-            bytecount = (stride * dimshape)
-            return self._tolist(space, buf, bytecount, itemsize, fmt, [stride])
-        items = [None] * dimshape
-
-        orig_buf = buf
-        for i in range(dimshape):
-            buf = SubBuffer(orig_buf, start, stride)
-            item = self._tolist_rec(space, buf, start, idim+1, fmt)
-            items[i] = item
-            start += stride
-
-        return space.newlist(items)
-
+        return self.buf.w_tolist(space)
 
     def _start_from_tuple(self, space, w_tuple):
         from pypy.objspace.std.tupleobject import W_AbstractTupleObject
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to