[pypy-commit] pypy PyBuffer: Stop inheriting Buffer from BinaryBuffer
rlamy
pypy.commits at gmail.com
Wed Mar 29 15:28:25 EDT 2017
Author: Ronan Lamy <ronan.lamy at gmail.com>
Branch: PyBuffer
Changeset: r90864:710267dd4284
Date: 2017-03-29 20:27 +0100
http://bitbucket.org/pypy/pypy/changeset/710267dd4284/
Log: Stop inheriting Buffer from BinaryBuffer
diff --git a/pypy/interpreter/buffer.py b/pypy/interpreter/buffer.py
--- a/pypy/interpreter/buffer.py
+++ b/pypy/interpreter/buffer.py
@@ -20,14 +20,6 @@
"Returns an interp-level string with the whole content of the buffer."
raise NotImplementedError
- def as_str_and_offset_maybe(self):
- """
- If the buffer is backed by a string, return a pair (string, offset),
- where offset is the offset inside the string where the buffer start.
- Else, return (None, 0).
- """
- return None, 0
-
def getitem(self, index):
"Returns the index'th character in the buffer."
raise NotImplementedError # Must be overriden. No bounds checks.
@@ -39,16 +31,10 @@
# May be overridden. No bounds checks.
return ''.join([self.getitem(i) for i in range(start, stop, step)])
- def __getslice__(self, start, stop):
- return self.getslice(start, stop, 1, stop - start)
-
def setitem(self, index, char):
"Write a character into the buffer."
raise NotImplementedError # Must be overriden. No bounds checks.
- def __setitem__(self, i, char):
- return self.setitem(i, char)
-
def setslice(self, start, string):
# May be overridden. No bounds checks.
for i in range(len(string)):
@@ -93,9 +79,6 @@
def as_str(self):
return self.data.as_str()
- def as_str_and_offset_maybe(self):
- return self.data.as_str_and_offset_maybe()
-
def getitem(self, index):
return self.data.getitem(index)
@@ -124,43 +107,59 @@
return [1]
-class BinaryBuffer(Buffer):
+class BinaryBuffer(object):
"""Base class for buffers of bytes"""
_attrs_ = ['readonly']
_immutable_ = True
+ def getlength(self):
+ """Returns the size in bytes (even if getitemsize() > 1)."""
+ raise NotImplementedError
+
+ def __len__(self):
+ res = self.getlength()
+ assert res >= 0
+ return res
+
def as_str(self):
"Returns an interp-level string with the whole content of the buffer."
# May be overridden.
return self.getslice(0, self.getlength(), 1, self.getlength())
+ def as_str_and_offset_maybe(self):
+ """
+ If the buffer is backed by a string, return a pair (string, offset),
+ where offset is the offset inside the string where the buffer start.
+ Else, return (None, 0).
+ """
+ return None, 0
+
+ def getitem(self, index):
+ "Returns the index'th character in the buffer."
+ raise NotImplementedError # Must be overriden. No bounds checks.
+
+ def __getitem__(self, i):
+ return self.getitem(i)
+
def getslice(self, start, stop, step, size):
# May be overridden. No bounds checks.
return ''.join([self.getitem(i) for i in range(start, stop, step)])
+ def __getslice__(self, start, stop):
+ return self.getslice(start, stop, 1, stop - start)
+
+ def setitem(self, index, char):
+ "Write a character into the buffer."
+ raise NotImplementedError # Must be overriden. No bounds checks.
+
+ def __setitem__(self, i, char):
+ return self.setitem(i, char)
def setslice(self, start, string):
# May be overridden. No bounds checks.
for i in range(len(string)):
self.setitem(start + i, string[i])
-
- def getformat(self):
- return 'B'
-
- def getitemsize(self):
- return 1
-
- def getndim(self):
- return 1
-
- def getshape(self):
- return [self.getlength()]
-
- def getstrides(self):
- return [1]
-
-
class ByteBuffer(BinaryBuffer):
_immutable_ = True
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
@@ -403,8 +403,6 @@
fromfile() method).
"""
buf = space.getarg_w('y*', w_s)
- if buf.getitemsize() != 1:
- raise oefmt(space.w_TypeError, "a bytes-like object is required")
s = buf.as_str()
self._frombytes(space, s)
diff --git a/pypy/module/mmap/interp_mmap.py b/pypy/module/mmap/interp_mmap.py
--- a/pypy/module/mmap/interp_mmap.py
+++ b/pypy/module/mmap/interp_mmap.py
@@ -2,7 +2,7 @@
from pypy.interpreter.baseobjspace import W_Root
from pypy.interpreter.typedef import TypeDef, GetSetProperty, make_weakref_descr
from pypy.interpreter.gateway import interp2app, unwrap_spec, WrappedDefault
-from pypy.interpreter.buffer import BinaryBuffer
+from pypy.interpreter.buffer import BinaryBuffer, SimpleBuffer
from rpython.rlib import rmmap, rarithmetic, objectmodel
from rpython.rlib.rmmap import RValueError, RTypeError, RMMapError
from rpython.rlib.rstring import StringBuilder
@@ -24,7 +24,7 @@
write_required = bool(flags & space.BUF_WRITABLE)
if write_required and readonly:
raise oefmt(space.w_BufferError, "Object is not writable.")
- return MMapBuffer(self.space, self.mmap, readonly)
+ return SimpleBuffer(MMapBuffer(self.space, self.mmap, readonly))
def writebuf_w(self, space):
self.check_writeable()
diff --git a/pypy/objspace/fake/objspace.py b/pypy/objspace/fake/objspace.py
--- a/pypy/objspace/fake/objspace.py
+++ b/pypy/objspace/fake/objspace.py
@@ -11,7 +11,7 @@
from pypy.tool.option import make_config
from pypy.interpreter import argument, gateway
from pypy.interpreter.baseobjspace import W_Root, ObjSpace, SpaceCache
-from pypy.interpreter.buffer import StringBuffer
+from pypy.interpreter.buffer import StringBuffer, SimpleBuffer
from pypy.interpreter.typedef import TypeDef, GetSetProperty
from pypy.objspace.std.sliceobject import W_SliceObject
@@ -42,7 +42,7 @@
is_root(w_subtype)
def buffer_w(self, space, flags):
- return StringBuffer("foobar")
+ return SimpleBuffer(StringBuffer("foobar"))
def text_w(self, space):
return NonConstant("foobar")
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
@@ -146,7 +146,7 @@
raise NotImplementedError
elif dim == 1:
itemsize = self.getitemsize()
- return self._tolist(space, buf, self.getlength(), itemsize, fmt,
+ return self._tolist(space, buf.as_binary(), self.getlength(), itemsize, fmt,
self.getstrides())
else:
return self._tolist_rec(space, buf.as_binary(), 0, 0, fmt)
@@ -179,7 +179,7 @@
orig_buf = buf
for i in range(dimshape):
- buf = SubBuffer(orig_buf.as_binary(), start, stride)
+ buf = SubBuffer(orig_buf, start, stride)
item = self._tolist_rec(space, buf, start, idim+1, fmt)
items[i] = item
start += stride
diff --git a/pypy/objspace/std/stringmethods.py b/pypy/objspace/std/stringmethods.py
--- a/pypy/objspace/std/stringmethods.py
+++ b/pypy/objspace/std/stringmethods.py
@@ -808,4 +808,4 @@
return [selfvalue[start + i*step] for i in range(sl)]
def _get_buffer(space, w_obj):
- return space.buffer_w(w_obj, space.BUF_SIMPLE)
+ return space.buffer_w(w_obj, space.BUF_SIMPLE).as_binary()
More information about the pypy-commit
mailing list