[pypy-svn] r78131 - in pypy/branch/fast-forward/pypy/objspace/std: . test
afa at codespeak.net
afa at codespeak.net
Wed Oct 20 15:14:17 CEST 2010
Author: afa
Date: Wed Oct 20 15:14:16 2010
New Revision: 78131
Modified:
pypy/branch/fast-forward/pypy/objspace/std/bytearrayobject.py
pypy/branch/fast-forward/pypy/objspace/std/test/test_bytes.py
Log:
buffer interface for bytearray objects
Modified: pypy/branch/fast-forward/pypy/objspace/std/bytearrayobject.py
==============================================================================
--- pypy/branch/fast-forward/pypy/objspace/std/bytearrayobject.py (original)
+++ pypy/branch/fast-forward/pypy/objspace/std/bytearrayobject.py Wed Oct 20 15:14:16 2010
@@ -11,6 +11,7 @@
from pypy.objspace.std.sliceobject import W_SliceObject, normalize_simple_slice
from pypy.objspace.std import slicetype
from pypy.interpreter import gateway
+from pypy.interpreter.buffer import RWBuffer
class W_BytearrayObject(W_Object):
from pypy.objspace.std.bytearraytype import bytearray_typedef as typedef
@@ -400,5 +401,25 @@
assert stop >= 0
w_bytearray.data[start:stop] = [c for c in space.str_w(w_other)]
+# __________________________________________________________
+# Buffer interface
+
+class BytearrayBuffer(RWBuffer):
+ def __init__(self, data):
+ self.data = data
+
+ 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 buffer__Bytearray(space, self):
+ b = BytearrayBuffer(self.data)
+ return space.wrap(b)
+
from pypy.objspace.std import bytearraytype
register_all(vars(), bytearraytype)
Modified: pypy/branch/fast-forward/pypy/objspace/std/test/test_bytes.py
==============================================================================
--- pypy/branch/fast-forward/pypy/objspace/std/test/test_bytes.py (original)
+++ pypy/branch/fast-forward/pypy/objspace/std/test/test_bytes.py Wed Oct 20 15:14:16 2010
@@ -175,3 +175,12 @@
assert b == 'ABC...defghi'
b[3:6] = '()'
assert b == 'ABC()defghi'
+
+ def test_buffer(self):
+ b = bytearray('abcdefghi')
+ buf = buffer(b)
+ assert buf[2] == 'c'
+ buf[3] = 'D'
+ assert b == 'abcDefghi'
+ buf[4:6] = 'EF'
+ assert b == 'abcDEFghi'
More information about the Pypy-commit
mailing list