[pypy-commit] pypy faster-rstruct-2: write tests for ByteBuffer, make it a subclass of GCBuffer and reduce a bit of code duplication with ByteArrayBuffer
antocuni
pypy.commits at gmail.com
Mon May 15 11:10:01 EDT 2017
Author: Antonio Cuni <anto.cuni at gmail.com>
Branch: faster-rstruct-2
Changeset: r91302:261fbab29544
Date: 2017-05-15 17:08 +0200
http://bitbucket.org/pypy/pypy/changeset/261fbab29544/
Log: write tests for ByteBuffer, make it a subclass of GCBuffer and
reduce a bit of code duplication with ByteArrayBuffer
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
@@ -6,10 +6,9 @@
from rpython.rlib.debug import check_list_of_chars, check_nonneg
from rpython.rtyper.lltypesystem import rffi
from rpython.rlib.rgc import (resizable_list_supporting_raw_ptr,
- nonmoving_raw_ptr_for_resizable_list,
- ll_for_resizable_list)
+ nonmoving_raw_ptr_for_resizable_list)
from rpython.rlib import jit
-from rpython.rlib.buffer import GCBuffer
+from rpython.rlib.buffer import GCBuffer, get_gc_data_for_list_of_chars
from pypy.interpreter.baseobjspace import W_Root
from pypy.interpreter.error import OperationError, oefmt
from pypy.interpreter.gateway import WrappedDefault, interp2app, unwrap_spec
@@ -1303,12 +1302,7 @@
return p
def _get_gc_data(self):
- from rpython.rtyper.lltypesystem import lltype, llmemory
- ll_data = ll_for_resizable_list(self.ba._data)
- ll_items = ll_data.items
- LIST = lltype.typeOf(ll_data).TO # rlist.LIST_OF(lltype.Char)
- base_ofs = llmemory.itemoffsetof(LIST.items.TO, 0)
- return ll_items, base_ofs
+ return get_gc_data_for_list_of_chars(self.ba._data)
@specialize.argtype(1)
diff --git a/rpython/rlib/buffer.py b/rpython/rlib/buffer.py
--- a/rpython/rlib/buffer.py
+++ b/rpython/rlib/buffer.py
@@ -8,7 +8,8 @@
from rpython.rlib.objectmodel import specialize
from rpython.rlib import jit
from rpython.rlib.rgc import (resizable_list_supporting_raw_ptr,
- nonmoving_raw_ptr_for_resizable_list)
+ nonmoving_raw_ptr_for_resizable_list,
+ ll_for_resizable_list)
from rpython.rlib.signature import signature
from rpython.rlib import types
@@ -166,8 +167,15 @@
scale_factor, base_ofs)
+def get_gc_data_for_list_of_chars(data):
+ ll_data = ll_for_resizable_list(data)
+ ll_items = ll_data.items
+ LIST = lltype.typeOf(ll_data).TO # rlist.LIST_OF(lltype.Char)
+ base_ofs = llmemory.itemoffsetof(LIST.items.TO, 0)
+ return ll_items, base_ofs
-class ByteBuffer(Buffer):
+
+class ByteBuffer(GCBuffer):
_immutable_ = True
def __init__(self, n):
@@ -186,6 +194,9 @@
def get_raw_address(self):
return nonmoving_raw_ptr_for_resizable_list(self.data)
+ def _get_gc_data(self):
+ return get_gc_data_for_list_of_chars(self.data)
+
class StringBuffer(GCBuffer):
_attrs_ = ['readonly', 'value']
diff --git a/rpython/rlib/test/test_buffer.py b/rpython/rlib/test/test_buffer.py
--- a/rpython/rlib/test/test_buffer.py
+++ b/rpython/rlib/test/test_buffer.py
@@ -1,7 +1,8 @@
import struct
from rpython.rtyper.lltypesystem import lltype, rffi
from rpython.rlib.rarithmetic import r_singlefloat
-from rpython.rlib.buffer import StringBuffer, SubBuffer, Buffer, RawBuffer
+from rpython.rlib.buffer import (StringBuffer, SubBuffer, Buffer, RawBuffer,
+ ByteBuffer)
from rpython.annotator.annrpython import RPythonAnnotator
from rpython.annotator.model import SomeInteger
from rpython.rtyper.test.tool import BaseRtypingTest
@@ -101,7 +102,6 @@
x = self.read(lltype.SingleFloat, buf, size)
assert x == r_singlefloat(45.6)
-
class TestTypedReadDirect(BaseTypedReadTest):
def read(self, TYPE, data, offset):
@@ -157,3 +157,28 @@
fn = self.cache[TYPE]
x = fn(data, offset)
return lltype.cast_primitive(TYPE, x)
+
+
+class TestByteBuffer(object):
+
+ def test_basic(self):
+ buf = ByteBuffer(4)
+ assert buf.getlength() == 4
+ assert buf.getitem(2) == '\x00'
+ buf.setitem(0, 'A')
+ buf.setitem(3, 'Z')
+ assert buf.as_str() == 'A\x00\x00Z'
+
+ def test_typed_write(self):
+ buf = ByteBuffer(4)
+ buf.typed_write(rffi.USHORT, 0, 0x1234)
+ buf.typed_write(rffi.USHORT, 2, 0x5678)
+ expected = struct.pack('HH', 0x1234, 0x5678)
+ assert buf.as_str() == expected
+
+ def test_typed_read(self):
+ data = struct.pack('HH', 0x1234, 0x5678)
+ buf = ByteBuffer(4)
+ buf.setslice(0, data)
+ assert buf.typed_read(rffi.USHORT, 0) == 0x1234
+ assert buf.typed_read(rffi.USHORT, 2) == 0x5678
More information about the pypy-commit
mailing list