[pypy-commit] pypy PyBuffer: Add a base class for C-style buffers: BinaryBuffer
rlamy
pypy.commits at gmail.com
Sat Mar 25 22:24:27 EDT 2017
Author: Ronan Lamy <ronan.lamy at gmail.com>
Branch: PyBuffer
Changeset: r90811:08a3f30f31dc
Date: 2017-03-26 03:23 +0100
http://bitbucket.org/pypy/pypy/changeset/08a3f30f31dc/
Log: Add a base class for C-style buffers: BinaryBuffer
diff --git a/pypy/interpreter/buffer.py b/pypy/interpreter/buffer.py
--- a/pypy/interpreter/buffer.py
+++ b/pypy/interpreter/buffer.py
@@ -1,4 +1,6 @@
from rpython.rlib.rgc import nonmoving_raw_ptr_for_resizable_list
+from rpython.rlib.signature import signature
+from rpython.rlib import types
class Buffer(object):
"""Abstract base class for buffers."""
@@ -16,8 +18,7 @@
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())
+ raise NotImplementedError
def as_str_and_offset_maybe(self):
"""
@@ -57,6 +58,34 @@
raise ValueError("no raw buffer")
def getformat(self):
+ raise NotImplementedError
+
+ def getitemsize(self):
+ raise NotImplementedError
+
+ def getndim(self):
+ raise NotImplementedError
+
+ def getshape(self):
+ raise NotImplementedError
+
+ def getstrides(self):
+ raise NotImplementedError
+
+ def releasebuffer(self):
+ pass
+
+class BinaryBuffer(Buffer):
+ """Base class for buffers of bytes"""
+ _attrs_ = ['readonly']
+ _immutable_ = True
+
+ 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 getformat(self):
return 'B'
def getitemsize(self):
@@ -71,10 +100,9 @@
def getstrides(self):
return [1]
- def releasebuffer(self):
- pass
-class ByteBuffer(Buffer):
+
+class ByteBuffer(BinaryBuffer):
_immutable_ = True
def __init__(self, len):
@@ -93,7 +121,7 @@
def get_raw_address(self):
return nonmoving_raw_ptr_for_resizable_list(self.data)
-class StringBuffer(Buffer):
+class StringBuffer(BinaryBuffer):
_attrs_ = ['readonly', 'value']
_immutable_ = True
@@ -128,10 +156,11 @@
# may still raise ValueError on some GCs
return rffi.get_raw_address_of_string(self.value)
-class SubBuffer(Buffer):
+class SubBuffer(BinaryBuffer):
_attrs_ = ['buffer', 'offset', 'size', 'readonly']
_immutable_ = True
+ #@signature(types.any(), types.instance(BinaryBuffer), types.int(), types.int(), returns=types.none())
def __init__(self, buffer, offset, size):
self.readonly = buffer.readonly
if isinstance(buffer, SubBuffer): # don't nest them
diff --git a/pypy/module/_cffi_backend/cbuffer.py b/pypy/module/_cffi_backend/cbuffer.py
--- a/pypy/module/_cffi_backend/cbuffer.py
+++ b/pypy/module/_cffi_backend/cbuffer.py
@@ -5,13 +5,13 @@
from pypy.module._cffi_backend import cdataobj, ctypeptr, ctypearray
from pypy.module._cffi_backend import ctypestruct
-from pypy.interpreter.buffer import Buffer
+from pypy.interpreter.buffer import BinaryBuffer
from rpython.rtyper.annlowlevel import llstr
from rpython.rtyper.lltypesystem import rffi
from rpython.rtyper.lltypesystem.rstr import copy_string_to_raw
-class LLBuffer(Buffer):
+class LLBuffer(BinaryBuffer):
_immutable_ = True
def __init__(self, raw_cdata, size):
@@ -34,7 +34,7 @@
def getslice(self, start, stop, step, size):
if step == 1:
return rffi.charpsize2str(rffi.ptradd(self.raw_cdata, start), size)
- return Buffer.getslice(self, start, stop, step, size)
+ return BinaryBuffer.getslice(self, start, stop, step, size)
def setslice(self, start, string):
raw_cdata = rffi.ptradd(self.raw_cdata, start)
diff --git a/pypy/module/_io/interp_bufferedio.py b/pypy/module/_io/interp_bufferedio.py
--- a/pypy/module/_io/interp_bufferedio.py
+++ b/pypy/module/_io/interp_bufferedio.py
@@ -4,7 +4,7 @@
from pypy.interpreter.typedef import (
TypeDef, GetSetProperty, generic_new_descr, interp_attrproperty_w)
from pypy.interpreter.gateway import interp2app, unwrap_spec, WrappedDefault
-from pypy.interpreter.buffer import Buffer, SubBuffer
+from pypy.interpreter.buffer import BinaryBuffer, SubBuffer
from rpython.rlib.rgc import (
nonmoving_raw_ptr_for_resizable_list, resizable_list_supporting_raw_ptr)
from rpython.rlib.rstring import StringBuilder
@@ -155,7 +155,7 @@
readinto1 = interp2app(W_BufferedIOBase.readinto1_w),
)
-class RawBuffer(Buffer):
+class RawBuffer(BinaryBuffer):
_immutable_ = True
def __init__(self, n):
diff --git a/pypy/module/_io/interp_bytesio.py b/pypy/module/_io/interp_bytesio.py
--- a/pypy/module/_io/interp_bytesio.py
+++ b/pypy/module/_io/interp_bytesio.py
@@ -2,7 +2,7 @@
from pypy.interpreter.typedef import (
TypeDef, generic_new_descr, GetSetProperty)
from pypy.interpreter.gateway import interp2app, unwrap_spec
-from pypy.interpreter.buffer import Buffer
+from pypy.interpreter.buffer import BinaryBuffer
from rpython.rlib.rStringIO import RStringIO
from rpython.rlib.rarithmetic import r_longlong
from rpython.rlib.objectmodel import import_from_mixin
@@ -12,7 +12,7 @@
import sys
-class BytesIOBuffer(Buffer):
+class BytesIOBuffer(BinaryBuffer):
_immutable_ = True
def __init__(self, w_bytesio):
diff --git a/pypy/module/_rawffi/buffer.py b/pypy/module/_rawffi/buffer.py
--- a/pypy/module/_rawffi/buffer.py
+++ b/pypy/module/_rawffi/buffer.py
@@ -1,11 +1,11 @@
from rpython.rtyper.lltypesystem import rffi
-from pypy.interpreter.buffer import Buffer
+from pypy.interpreter.buffer import BinaryBuffer
# XXX not the most efficient implementation
-class RawFFIBuffer(Buffer):
+class RawFFIBuffer(BinaryBuffer):
_immutable_ = True
def __init__(self, datainstance):
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 Buffer
+from pypy.interpreter.buffer import BinaryBuffer
from rpython.rlib import rmmap, rarithmetic, objectmodel
from rpython.rlib.rmmap import RValueError, RTypeError, RMMapError
from rpython.rlib.rstring import StringBuilder
@@ -311,7 +311,7 @@
return OperationError(space.w_SystemError, space.newtext('%s' % e))
-class MMapBuffer(Buffer):
+class MMapBuffer(BinaryBuffer):
_immutable_ = True
def __init__(self, space, mmap, readonly):
@@ -331,7 +331,7 @@
if step == 1:
return self.mmap.getslice(start, size)
else:
- return Buffer.getslice(self, start, stop, step, size)
+ return BinaryBuffer.getslice(self, start, stop, step, size)
def setitem(self, index, char):
self.check_valid_writeable()
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
@@ -17,7 +17,7 @@
getbytevalue, makebytesdata_w, newbytesdata_w)
from pypy.interpreter.gateway import WrappedDefault, interp2app, unwrap_spec
from pypy.interpreter.typedef import TypeDef
-from pypy.interpreter.buffer import Buffer
+from pypy.interpreter.buffer import BinaryBuffer
from pypy.objspace.std.sliceobject import W_SliceObject, unwrap_start_stop
from pypy.objspace.std.stringmethods import StringMethods, _get_buffer
from pypy.objspace.std.stringmethods import _descr_getslice_slowpath
@@ -1276,7 +1276,7 @@
start += step
-class BytearrayBuffer(Buffer):
+class BytearrayBuffer(BinaryBuffer):
_immutable_ = True
readonly = False
@@ -1306,7 +1306,7 @@
if start != 0 or stop != len(data):
data = data[start:stop]
return "".join(data)
- return Buffer.getslice(self, start, stop, step, size)
+ return BinaryBuffer.getslice(self, start, stop, step, size)
def setslice(self, start, string):
# No bounds checks.
More information about the pypy-commit
mailing list