[pypy-commit] pypy PyBuffer-backport: Backport rpython.rlib.buffer changes from py3.5

rlamy pypy.commits at gmail.com
Tue May 2 14:46:59 EDT 2017


Author: Ronan Lamy <ronan.lamy at gmail.com>
Branch: PyBuffer-backport
Changeset: r91171:4ec9314365c8
Date: 2017-05-02 18:02 +0100
http://bitbucket.org/pypy/pypy/changeset/4ec9314365c8/

Log:	Backport rpython.rlib.buffer changes from py3.5

diff --git a/rpython/rlib/buffer.py b/rpython/rlib/buffer.py
--- a/rpython/rlib/buffer.py
+++ b/rpython/rlib/buffer.py
@@ -1,13 +1,13 @@
 """
 Buffer protocol support.
 """
-from rpython.rlib import jit
-from rpython.rlib.rgc import (resizable_list_supporting_raw_ptr,
-        nonmoving_raw_ptr_for_resizable_list)
-
+from rpython.rlib.rgc import (
+    nonmoving_raw_ptr_for_resizable_list, resizable_list_supporting_raw_ptr)
+from rpython.rlib.signature import signature
+from rpython.rlib import types
 
 class Buffer(object):
-    """Abstract base class for buffers."""
+    """Base class for buffers of bytes"""
     _attrs_ = ['readonly']
     _immutable_ = True
 
@@ -27,8 +27,8 @@
 
     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.
+        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
@@ -44,6 +44,7 @@
         # May be overridden.  No bounds checks.
         return ''.join([self.getitem(i) for i in range(start, stop, step)])
 
+    @signature(types.any(), types.int(), types.int(), returns=types.str())
     def __getslice__(self, start, stop):
         return self.getslice(start, stop, 1, stop - start)
 
@@ -59,26 +60,24 @@
         for i in range(len(string)):
             self.setitem(start + i, string[i])
 
+class ByteBuffer(Buffer):
+    _immutable_ = True
+
+    def __init__(self, n):
+        self.data = resizable_list_supporting_raw_ptr(['\0'] * n)
+        self.readonly = False
+
+    def getlength(self):
+        return len(self.data)
+
+    def getitem(self, index):
+        return self.data[index]
+
+    def setitem(self, index, char):
+        self.data[index] = char
+
     def get_raw_address(self):
-        raise ValueError("no raw buffer")
-
-    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]
-
-    def releasebuffer(self):
-        pass
+        return nonmoving_raw_ptr_for_resizable_list(self.data)
 
 class StringBuffer(Buffer):
     _attrs_ = ['readonly', 'value']
@@ -119,6 +118,8 @@
     _attrs_ = ['buffer', 'offset', 'size', 'readonly']
     _immutable_ = True
 
+    @signature(types.any(), types.instance(Buffer), 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
@@ -149,7 +150,7 @@
     def as_str_and_offset_maybe(self):
         string, offset = self.buffer.as_str_and_offset_maybe()
         if string is not None:
-            return string, offset+self.offset
+            return string, offset + self.offset
         return None, 0
 
     def getitem(self, index):
diff --git a/rpython/rlib/rstring.py b/rpython/rlib/rstring.py
--- a/rpython/rlib/rstring.py
+++ b/rpython/rlib/rstring.py
@@ -8,7 +8,6 @@
 from rpython.rlib import jit
 from rpython.rlib.objectmodel import newlist_hint, resizelist_hint, specialize, not_rpython
 from rpython.rlib.rarithmetic import ovfcheck, LONG_BIT as BLOOM_WIDTH
-from rpython.rlib.buffer import Buffer
 from rpython.rlib.unicodedata import unicodedb_5_2_0 as unicodedb
 from rpython.rtyper.extregistry import ExtRegistryEntry
 from rpython.tool.pairtype import pairtype


More information about the pypy-commit mailing list