Author: Richard Plangger <planri...@gmail.com> Branch: py3.5-ssl Changeset: r88819:a62844213890 Date: 2016-12-02 13:46 +0100 http://bitbucket.org/pypy/pypy/changeset/a62844213890/
Log: test that checks multidim. memoryview tolist + reversed diff --git a/pypy/module/struct/formatiterator.py b/pypy/module/struct/formatiterator.py --- a/pypy/module/struct/formatiterator.py +++ b/pypy/module/struct/formatiterator.py @@ -135,9 +135,8 @@ def read(self, count): if self.strides: - end = self.pos + count * self.strides[0] - else: - end = self.pos + count + count = self.strides[0] + end = self.pos + count if end > self.length: raise StructError("unpack str size too short for format") s = self.buf.getslice(self.pos, end, 1, count) 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 @@ -167,18 +167,20 @@ raise NotImplementedError elif dim == 1: itemsize = self.getitemsize() - return self._tolist(space, buf, self.getlength() // itemsize, fmt) + return self._tolist(space, buf, self.getlength(), itemsize, fmt, + self.getstrides()) else: return self._tolist_rec(space, buf, 0, 0, fmt) - def _tolist(self, space, buf, count, 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 = self.getlength() - fmtiter.strides = self.getstrides() + fmtiter.length = bytecount + fmtiter.strides = strides fmtiter.interpret(fmt * count) return space.newlist(fmtiter.result_w) @@ -193,12 +195,13 @@ # if dim >= self.getndim(): bytecount = (stride * dimshape) - count = bytecount // itemsize - return self._tolist(space, buf, count, fmt) + return self._tolist(space, buf, bytecount, itemsize, fmt, [stride]) items = [None] * dimshape for i in range(dimshape): - item = self._tolist_rec(space, SubBuffer(buf, start, stride), start, idim+1, fmt) + import pdb; pdb.set_trace() + buf = SubBuffer(buf, start, stride) + item = self._tolist_rec(space, buf, start, idim+1, fmt) items[i] = item start += stride 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 @@ -1,4 +1,5 @@ import py +import pytest import struct from pypy.interpreter.baseobjspace import W_Root from pypy.interpreter.gateway import interp2app @@ -424,3 +425,18 @@ assert view[::-1][-2] == 1 assert list(reversed(view)) == revlist assert list(reversed(view)) == view[::-1].tolist() + +class AppTestMemoryViewReversed(object): + spaceconfig = dict(usemodules=['array']) + def test_reversed_non_bytes(self): + import array + items = [1,2,3,9,7,5] + formats = ['h'] + for fmt in formats: + bytes = array.array(fmt, items) + view = memoryview(bytes) + bview = view.cast('b') + rview = bview.cast(fmt, shape=(2,3)) + assert rview.tolist() == [[1,2,3],[9,7,5]] + assert rview[::-1].tolist() == [[3,2,1], [5,7,9]] + raises(NotImplementedError, list, reversed(rview)) _______________________________________________ pypy-commit mailing list pypy-commit@python.org https://mail.python.org/mailman/listinfo/pypy-commit