[pypy-commit] pypy PyBuffer: Rename as_binary()/as_binary_rw() to as_readbuf()/as_writebuf()

rlamy pypy.commits at gmail.com
Tue Apr 25 14:49:51 EDT 2017


Author: Ronan Lamy <ronan.lamy at gmail.com>
Branch: PyBuffer
Changeset: r91129:357193a36126
Date: 2017-04-25 19:49 +0100
http://bitbucket.org/pypy/pypy/changeset/357193a36126/

Log:	Rename as_binary()/as_binary_rw() to as_readbuf()/as_writebuf()

diff --git a/pypy/interpreter/baseobjspace.py b/pypy/interpreter/baseobjspace.py
--- a/pypy/interpreter/baseobjspace.py
+++ b/pypy/interpreter/baseobjspace.py
@@ -9,9 +9,9 @@
 from rpython.rlib.signature import signature
 from rpython.rlib.rarithmetic import r_uint, SHRT_MIN, SHRT_MAX, \
     INT_MIN, INT_MAX, UINT_MAX, USHRT_MAX
+from rpython.rlib.buffer import StringBuffer
 
-from pypy.interpreter.buffer import (
-    BufferInterfaceNotFound, StringBuffer)
+from pypy.interpreter.buffer import BufferInterfaceNotFound
 from pypy.interpreter.executioncontext import (ExecutionContext, ActionFlag,
     make_finalizer_queue)
 from pypy.interpreter.error import OperationError, new_exception_class, oefmt
@@ -1475,14 +1475,14 @@
     def readbuf_w(self, w_obj):
         # Old buffer interface, returns a readonly buffer (PyObject_AsReadBuffer)
         try:
-            return w_obj.buffer_w(self, self.BUF_SIMPLE).as_binary()
+            return w_obj.buffer_w(self, self.BUF_SIMPLE).as_readbuf()
         except BufferInterfaceNotFound:
             self._getarg_error("bytes-like object", w_obj)
 
     def writebuf_w(self, w_obj):
         # Old buffer interface, returns a writeable buffer (PyObject_AsWriteBuffer)
         try:
-            return w_obj.buffer_w(self, self.BUF_WRITABLE).as_binary_rw()
+            return w_obj.buffer_w(self, self.BUF_WRITABLE).as_writebuf()
         except (BufferInterfaceNotFound, OperationError):
             self._getarg_error("read-write bytes-like object", w_obj)
 
@@ -1516,7 +1516,7 @@
                 # NB. CPython forbids surrogates here
                 return StringBuffer(w_obj.text_w(self))
             try:
-                return w_obj.buffer_w(self, self.BUF_SIMPLE).as_binary()
+                return w_obj.buffer_w(self, self.BUF_SIMPLE).as_readbuf()
             except BufferInterfaceNotFound:
                 self._getarg_error("bytes or buffer", w_obj)
         elif code == 's#':
diff --git a/pypy/interpreter/buffer.py b/pypy/interpreter/buffer.py
--- a/pypy/interpreter/buffer.py
+++ b/pypy/interpreter/buffer.py
@@ -36,11 +36,11 @@
     def get_raw_address(self):
         raise ValueError("no raw buffer")
 
-    def as_binary(self):
+    def as_readbuf(self):
         # Inefficient. May be overridden.
         return StringBuffer(self.as_str())
 
-    def as_binary_rw(self):
+    def as_writebuf(self):
         """Return a writable Buffer sharing the same data as `self`."""
         raise BufferInterfaceNotFound
 
@@ -209,10 +209,10 @@
     def get_raw_address(self):
         return self.data.get_raw_address()
 
-    def as_binary(self):
+    def as_readbuf(self):
         return self.data
 
-    def as_binary_rw(self):
+    def as_writebuf(self):
         assert not self.data.readonly
         return self.data
 
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
@@ -66,7 +66,7 @@
                                                       self.buffer.getlength())
         if step not in (0, 1):
             raise oefmt(space.w_NotImplementedError, "")
-        value = space.buffer_w(w_newstring, space.BUF_CONTIG_RO).as_binary()
+        value = space.buffer_w(w_newstring, space.BUF_CONTIG_RO).as_readbuf()
         if value.getlength() != size:
             raise oefmt(space.w_ValueError,
                         "cannot modify size of memoryview object")
@@ -80,20 +80,20 @@
         if space.isinstance_w(w_other, space.w_unicode):
             return space.w_NotImplemented
         try:
-            other_pybuf = space.buffer_w(w_other, space.BUF_SIMPLE)
+            other_buf = space.readbuf_w(w_other)
         except OperationError as e:
             if e.async(space):
                 raise
             return space.w_NotImplemented
         my_buf = self.buffer
         my_len = len(my_buf)
-        other_len = other_pybuf.getlength()
+        other_len = len(other_buf)
         if other_len != my_len:
             if mode == 'E':
                 return space.w_False
             if mode == 'N':
                 return space.w_True
-        cmp = _memcmp(my_buf, other_pybuf.as_binary(), min(my_len, other_len))
+        cmp = _memcmp(my_buf, other_buf, min(my_len, other_len))
         if cmp == 0:
             if my_len < other_len:
                 cmp = -1
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
@@ -921,10 +921,10 @@
     def get_raw_address(self):
         return self.data.get_raw_address()
 
-    def as_binary(self):
+    def as_readbuf(self):
         return self.data
 
-    def as_binary_rw(self):
+    def as_writebuf(self):
         assert not self.readonly
         return self.data
 
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
@@ -390,10 +390,10 @@
         for i in range(len(string)):
             self.ptr[start + i] = string[i]
 
-    def as_binary(self):
+    def as_readbuf(self):
         return CBuffer(self)
 
-    def as_binary_rw(self):
+    def as_writebuf(self):
         assert not self.readonly
         return CBuffer(self)
 
diff --git a/pypy/module/cpyext/test/test_memoryobject.py b/pypy/module/cpyext/test/test_memoryobject.py
--- a/pypy/module/cpyext/test/test_memoryobject.py
+++ b/pypy/module/cpyext/test/test_memoryobject.py
@@ -1,9 +1,11 @@
 import pytest
 
 from rpython.rtyper.lltypesystem import rffi
+from rpython.rlib.buffer import StringBuffer
+
 from pypy.module.cpyext.test.test_api import BaseApiTest
 from pypy.module.cpyext.test.test_cpyext import AppTestCpythonExtensionBase
-from pypy.interpreter.buffer import SimpleView, StringBuffer
+from pypy.interpreter.buffer import SimpleView
 from pypy.module.cpyext.pyobject import from_ref
 from pypy.module.cpyext.memoryobject import PyMemoryViewObject
 
diff --git a/pypy/module/struct/interp_struct.py b/pypy/module/struct/interp_struct.py
--- a/pypy/module/struct/interp_struct.py
+++ b/pypy/module/struct/interp_struct.py
@@ -127,7 +127,7 @@
     """Unpack the buffer, containing packed C structure data, according to
 fmt, starting at offset. Requires len(buffer[offset:]) >= calcsize(fmt)."""
     size = _calcsize(space, format)
-    buf = space.buffer_w(w_buffer, space.BUF_SIMPLE).as_binary()
+    buf = space.readbuf_w(w_buffer, space.BUF_SIMPLE)
     if offset < 0:
         offset += buf.getlength()
     if offset < 0 or (buf.getlength() - offset) < size:
@@ -140,7 +140,7 @@
 
 class W_UnpackIter(W_Root):
     def __init__(self, space, w_struct, w_buffer):
-        buf = space.buffer_w(w_buffer, space.BUF_SIMPLE).as_binary()
+        buf = space.readbuf_w(w_buffer, space.BUF_SIMPLE)
         if w_struct.size <= 0:
             raise oefmt(get_error(space),
                 "cannot iteratively unpack with a struct of length %d",
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
@@ -19,7 +19,7 @@
 from pypy.interpreter.typedef import TypeDef
 from pypy.interpreter.buffer import SimpleView
 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 StringMethods
 from pypy.objspace.std.stringmethods import _descr_getslice_slowpath
 from pypy.objspace.std.bytesobject import W_BytesObject
 from pypy.objspace.std.util import get_positive_index
@@ -267,7 +267,7 @@
             return space.newbool(self.getdata() == w_other.getdata())
 
         try:
-            buffer = _get_buffer(space, w_other)
+            buffer = space.readbuf_w(w_other)
         except OperationError as e:
             if e.match(space, space.w_TypeError):
                 return space.w_NotImplemented
@@ -287,7 +287,7 @@
             return space.newbool(self.getdata() != w_other.getdata())
 
         try:
-            buffer = _get_buffer(space, w_other)
+            buffer = space.readbuf_w(w_other)
         except OperationError as e:
             if e.match(space, space.w_TypeError):
                 return space.w_NotImplemented
@@ -315,7 +315,7 @@
             cmp = _memcmp(value, other, min(len(value), len(other)))
         else:
             try:
-                buffer = _get_buffer(space, w_other)
+                buffer = space.readbuf_w(w_other)
             except OperationError as e:
                 if e.match(space, space.w_TypeError):
                     return False, 0, 0
@@ -357,7 +357,7 @@
         if isinstance(w_other, W_BytesObject):
             self._inplace_add(self._op_val(space, w_other))
         else:
-            self._inplace_add(_get_buffer(space, w_other))
+            self._inplace_add(space.readbuf_w(w_other))
         return self
 
     @specialize.argtype(1)
@@ -459,7 +459,7 @@
             return self._add(self._op_val(space, w_other))
 
         try:
-            buffer = _get_buffer(space, w_other)
+            buffer = space.readbuf_w(w_other)
         except OperationError as e:
             if e.match(space, space.w_TypeError):
                 return space.w_NotImplemented
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
@@ -208,7 +208,7 @@
             dst_strides = self.getstrides()
             dim = 0
             dst = SubBuffer(
-                self.view.as_binary_rw(),
+                self.view.as_writebuf(),
                 start * itemsize, slicelength * itemsize)
             src_stride0 = dst_strides[dim]
 
@@ -492,7 +492,7 @@
     def descr_hex(self, space):
         from pypy.objspace.std.bytearrayobject import _array_to_hexstring
         self._check_released(space)
-        return _array_to_hexstring(space, self.view.as_binary(), 0, 1, self.getlength())
+        return _array_to_hexstring(space, self.view.as_readbuf(), 0, 1, self.getlength())
 
 def is_byte_format(char):
     return char == 'b' or char == 'B' or char == 'c'
@@ -603,11 +603,11 @@
     def get_raw_address(self):
         return self.parent.get_raw_address()
 
-    def as_binary(self):
-        return self.parent.as_binary()
+    def as_readbuf(self):
+        return self.parent.as_readbuf()
 
-    def as_binary_rw(self):
-        return self.parent.as_binary_rw()
+    def as_writebuf(self):
+        return self.parent.as_writebuf()
 
 class BufferView1D(IndirectView):
     _immutable_ = True
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
@@ -485,7 +485,7 @@
 
             pos = value.find(sub)
         else:
-            sub = _get_buffer(space, w_sub)
+            sub = space.readbuf_w(w_sub)
             sublen = sub.getlength()
             if sublen == 0:
                 raise oefmt(space.w_ValueError, "empty separator")
@@ -515,7 +515,7 @@
 
             pos = value.rfind(sub)
         else:
-            sub = _get_buffer(space, w_sub)
+            sub = space.readbuf_w(w_sub)
             sublen = sub.getlength()
             if sublen == 0:
                 raise oefmt(space.w_ValueError, "empty separator")
@@ -806,6 +806,3 @@
 @specialize.argtype(0)
 def _descr_getslice_slowpath(selfvalue, start, step, sl):
     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).as_binary()


More information about the pypy-commit mailing list