Author: Ronan Lamy <[email protected]>
Branch: PyBuffer
Changeset: r91039:3efce1d06791
Date: 2017-04-11 17:32 +0100
http://bitbucket.org/pypy/pypy/changeset/3efce1d06791/
Log: Add .getbytes() and .setbytes() to Buffer to replace deleted
getslice() and setslice()
diff --git a/pypy/interpreter/buffer.py b/pypy/interpreter/buffer.py
--- a/pypy/interpreter/buffer.py
+++ b/pypy/interpreter/buffer.py
@@ -1,6 +1,7 @@
from rpython.rlib.rgc import nonmoving_raw_ptr_for_resizable_list
from rpython.rlib.signature import signature
from rpython.rlib import types
+from rpython.rlib.rstruct.error import StructError
from pypy.interpreter.error import oefmt
@@ -18,6 +19,15 @@
"Returns an interp-level string with the whole content of the buffer."
return ''.join(self._copy_buffer())
+ def getbytes(self, start, stop, step, size):
+ # May be overridden. No bounds checks.
+ return ''.join([self.getitem(i) for i in range(start, stop, step)])
+
+ def setbytes(self, start, string):
+ # May be overridden. No bounds checks.
+ for i in range(len(string)):
+ self.setitem(start + i, string[i])
+
def get_raw_address(self):
raise ValueError("no raw buffer")
@@ -46,7 +56,7 @@
def _copy_buffer(self):
if self.getndim() == 0:
itemsize = self.getitemsize()
- return [self.getslice(0, itemsize, 1, itemsize)]
+ return [self.getbytes(0, itemsize, 1, itemsize)]
data = []
self._copy_rec(0, data, 0)
return data
@@ -72,7 +82,7 @@
bytesize = self.getlength()
copiedbytes = 0
for i in range(step):
- bytes = self.getslice(off, off+itemsize, 1, itemsize)
+ bytes = self.getbytes(off, off+itemsize, 1, itemsize)
data.append(bytes)
copiedbytes += len(bytes)
off += strides[0]
@@ -125,7 +135,7 @@
"memoryview: invalid type for format '%s'",
self.getformat())
byteval = fmtiter.result.build()
- self.setslice(offset, byteval)
+ self.setbytes(offset, byteval)
class SimpleBuffer(Buffer):
@@ -142,6 +152,13 @@
def as_str(self):
return self.data.as_str()
+ def getbytes(self, start, stop, step, size):
+ assert step == 1
+ return self.data[start:stop]
+
+ def setbytes(self, offset, s):
+ self.data.setslice(offset, s)
+
def get_raw_address(self):
return self.data.get_raw_address()
diff --git a/pypy/module/cpyext/slotdefs.py b/pypy/module/cpyext/slotdefs.py
--- a/pypy/module/cpyext/slotdefs.py
+++ b/pypy/module/cpyext/slotdefs.py
@@ -380,8 +380,13 @@
def getlength(self):
return self.size
- def getitem(self, index):
- return self.ptr[index]
+ def getbytes(self, start, stop, step, size):
+ return ''.join([self.ptr[i] for i in range(start, stop, step)])
+
+ def setbytes(self, start, string):
+ # absolutely no safety checks, what could go wrong?
+ for i in range(len(string)):
+ self.ptr[start + i] = string[i]
def get_raw_address(self):
return rffi.cast(rffi.CCHARP, self.ptr)
@@ -401,10 +406,6 @@
def getndim(self):
return self.ndim
- def setitem(self, index, char):
- # absolutely no safety checks, what could go wrong?
- self.ptr[index] = char
-
class FQ(rgc.FinalizerQueue):
Class = CPyBuffer
def finalizer_trigger(self):
diff --git a/pypy/objspace/std/bytearrayobject.py
b/pypy/objspace/std/bytearrayobject.py
--- a/pypy/objspace/std/bytearrayobject.py
+++ b/pypy/objspace/std/bytearrayobject.py
@@ -427,7 +427,8 @@
def descr_insert(self, space, w_idx, w_other):
where = space.int_w(w_idx)
- index = get_positive_index(where, len(self.getdata()))
+ data = self.getdata()
+ index = get_positive_index(where, len(data))
val = space.byte_w(w_other)
data.insert(index, val)
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
@@ -4,7 +4,6 @@
import operator
from rpython.rlib.objectmodel import compute_hash
-from rpython.rlib.rstruct.error import StructError
from pypy.interpreter.baseobjspace import W_Root
from pypy.interpreter.buffer import Buffer, SubBuffer
from pypy.interpreter.error import OperationError, oefmt
@@ -254,7 +253,7 @@
if value.getlength() != slicelength * itemsize:
raise oefmt(space.w_ValueError,
"cannot modify size of memoryview object")
- self.buf.setslice(start * itemsize, value.as_str())
+ self.buf.setbytes(start * itemsize, value.as_str())
else:
if self.getndim() != 1:
raise oefmt(space.w_NotImplementedError,
@@ -274,7 +273,7 @@
src_shape0 = slicelength
src_stride0 = src.getstrides()[0]
for i in range(src_shape0):
- data.append(src.getslice(off,off+itemsize,1,itemsize))
+ data.append(src.getbytes(off,off+itemsize,1,itemsize))
off += src_stride0
off = 0
dst_stride0 = self.getstrides()[0] * step
@@ -655,6 +654,19 @@
def getlength(self):
return self.shape[0] * self.getitemsize()
+ def getbytes(self, start, stop, step, size):
+ if start == stop:
+ return '' # otherwise, adding self.offset might make them
+ # out of bounds
+ return self.buf.getbytes(self.offset + start, self.offset + stop,
+ step, size)
+
+ def setbytes(self, start, string):
+ if len(string) == 0:
+ return # otherwise, adding self.offset might make 'start'
+ # out of bounds
+ self.buf.setbytes(self.offset + start, string)
+
def get_raw_address(self):
from rpython.rtyper.lltypesystem import rffi
ptr = self.buf.get_raw_address()
@@ -689,6 +701,12 @@
def as_str_and_offset_maybe(self):
return self.parent.as_str_and_offset_maybe()
+ def getbytes(self, start, stop, step, size):
+ return self.parent.getbytes(start, stop, step, size)
+
+ def setbytes(self, start, string):
+ self.parent.setbytes(start, string)
+
def get_raw_address(self):
return self.parent.get_raw_address()
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit