Author: Ronan Lamy <ronan.l...@gmail.com> Branch: py3.5 Changeset: r91638:9068d4152e3f Date: 2017-06-23 17:13 +0100 http://bitbucket.org/pypy/pypy/changeset/9068d4152e3f/
Log: Make slices of array memoryviews usable as writable buffers if contiguous. diff --git a/pypy/module/array/interp_array.py b/pypy/module/array/interp_array.py --- a/pypy/module/array/interp_array.py +++ b/pypy/module/array/interp_array.py @@ -1,5 +1,5 @@ from rpython.rlib import jit, rgc -from rpython.rlib.buffer import RawBuffer +from rpython.rlib.buffer import RawBuffer, SubBuffer from rpython.rlib.objectmodel import keepalive_until_here from rpython.rlib.rarithmetic import ovfcheck, widen from rpython.rlib.unroll import unrolling_iterable @@ -930,6 +930,14 @@ assert not self.readonly return self.data + def new_slice(self, start, step, slicelength): + if step == 1: + n = self.itemsize + newbuf = SubBuffer(self.data, start * n, slicelength * n) + return ArrayView(newbuf, self.fmt, self.itemsize, self.readonly) + else: + return BufferView.new_slice(self, start, step, slicelength) + unpack_driver = jit.JitDriver(name='unpack_array', greens=['selfclass', 'tp'], diff --git a/pypy/module/array/test/test_array.py b/pypy/module/array/test/test_array.py --- a/pypy/module/array/test/test_array.py +++ b/pypy/module/array/test/test_array.py @@ -1062,6 +1062,13 @@ def test_fresh_array_buffer_bytes(self): assert bytes(memoryview(self.array('i'))) == b'' + def test_mview_slice_aswritebuf(self): + import struct + a = self.array('B', b'abcdef') + view = memoryview(a)[1:5] + struct.pack_into('>H', view, 1, 0x1234) + assert a.tobytes() == b'ab\x12\x34ef' + class AppTestArrayReconstructor: spaceconfig = dict(usemodules=('array', 'struct')) _______________________________________________ pypy-commit mailing list pypy-commit@python.org https://mail.python.org/mailman/listinfo/pypy-commit