aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatti Picus <matti.picus@gmail.com>2022-11-17 23:07:51 +0200
committerMatti Picus <matti.picus@gmail.com>2022-11-17 23:07:51 +0200
commit3a6a4154062c4dc03ddaa1245fef4cceb2e7dfe7 (patch)
tree527387e806f37dec1d2955d3a5903691c090ff6a
parentfix setting a slice in a memoryview with non-unit strides (issue 3857) (diff)
downloadpypy-3a6a4154062c4dc03ddaa1245fef4cceb2e7dfe7.tar.gz
pypy-3a6a4154062c4dc03ddaa1245fef4cceb2e7dfe7.tar.bz2
pypy-3a6a4154062c4dc03ddaa1245fef4cceb2e7dfe7.zip
fix for itemsize != 1
-rw-r--r--pypy/interpreter/buffer.py8
-rw-r--r--pypy/objspace/std/test/test_memoryobject.py10
2 files changed, 15 insertions, 3 deletions
diff --git a/pypy/interpreter/buffer.py b/pypy/interpreter/buffer.py
index f67152e36e..f2e7398b86 100644
--- a/pypy/interpreter/buffer.py
+++ b/pypy/interpreter/buffer.py
@@ -336,9 +336,13 @@ class BufferSlice(BufferView):
# We cannot use self.parent.setbytes, we need to roll our own
# XXX check that length is not too long
last_stride = self.getstrides()[0]
- offset = self.start * last_stride
+ itemsize = self.getitemsize()
+ assert itemsize >= 0
+ offset = self.start * itemsize
for i in range(length):
- self.parent.setbytes(offset + i * last_stride, string[i])
+ os = offset + i * last_stride
+ start = i * itemsize
+ self.parent.setbytes(os, string[start:(start+itemsize)])
else:
offset = self.start * self.parent.getstrides()[0]
self.parent.setbytes(offset + start, string)
diff --git a/pypy/objspace/std/test/test_memoryobject.py b/pypy/objspace/std/test/test_memoryobject.py
index c25975c61f..70ab4aa70d 100644
--- a/pypy/objspace/std/test/test_memoryobject.py
+++ b/pypy/objspace/std/test/test_memoryobject.py
@@ -76,7 +76,15 @@ class AppTestMemoryView(object):
assert data == b'zbzdzf'
data[3:] = b'yyy'
assert s == b'zzy'
-
+
+ def test_assign_all_harder(self):
+ import array
+ v = memoryview(array.array('i', list(range(10))))
+ s = v[-1::-2]
+ s[:] = array.array('i', [100, 200, 300, 400, 500])
+ assert list(v) == [0, 500, 2, 400, 4, 300, 6, 200, 8, 100], list(v)
+ v[7:] = array.array('i', [-10, -10, -10])
+ assert list(s) == [-10, -10, 300, 400, 500], list(s)
def test_memoryview_attrs(self):
b = b"a"*100