[pypy-svn] r51357 - in pypy/dist/pypy/rlib: . test

fijal at codespeak.net fijal at codespeak.net
Sat Feb 9 15:41:13 CET 2008


Author: fijal
Date: Sat Feb  9 15:41:11 2008
New Revision: 51357

Modified:
   pypy/dist/pypy/rlib/rbuffer.py
   pypy/dist/pypy/rlib/test/test_rbuffer.py
Log:
setitem/getitem


Modified: pypy/dist/pypy/rlib/rbuffer.py
==============================================================================
--- pypy/dist/pypy/rlib/rbuffer.py	(original)
+++ pypy/dist/pypy/rlib/rbuffer.py	Sat Feb  9 15:41:11 2008
@@ -5,17 +5,34 @@
 """
 
 from pypy.rpython.lltypesystem import lltype, rffi
+from pypy.rlib.rarithmetic import r_uint
 
 class RBuffer:
     ll_buffer = lltype.nullptr(rffi.CCHARP.TO)
     
-    def __init__(self, size):
-        self.ll_buffer = lltype.malloc(rffi.CCHARP.TO, size, flavor='raw')
+    def __init__(self, size, address=r_uint(0)):
+        if address == 0:
+            self.ll_buffer = lltype.malloc(rffi.CCHARP.TO, size,
+                                           zero=True, flavor='raw')
+        else:
+            self.ll_buffer = rffi.cast(rffi.CCHARP, address)
         self.size = size
 
     def address(self):
-        return rffi.cast(rffi.INT, self.ll_buffer)
+        return rffi.cast(rffi.UINT, self.ll_buffer)
+
+    def getitem(self, item):
+        # XXX think how to avoid multiple layers of boundary checks
+        if item >= self.size or item < 0:
+            raise IndexError(item)
+        return self.ll_buffer[item]
+
+    def setitem(self, item, value):
+        if item >= self.size or item < 0:
+            raise IndexError(item)
+        self.ll_buffer[item] = value
 
     def free(self):
         if self.ll_buffer:
             lltype.free(self.ll_buffer, flavor='raw')
+            self.ll_buffer = lltype.nullptr(rffi.CCHARP.TO)

Modified: pypy/dist/pypy/rlib/test/test_rbuffer.py
==============================================================================
--- pypy/dist/pypy/rlib/test/test_rbuffer.py	(original)
+++ pypy/dist/pypy/rlib/test/test_rbuffer.py	Sat Feb  9 15:41:11 2008
@@ -7,4 +7,10 @@
         buf = RBuffer(3)
         assert buf.address()
         buf.free()
-    
+
+    def test_getsetitem(self):
+        buf = RBuffer(10)
+        assert buf.getitem(3) == '\x00'
+        buf.setitem(4, '\x01')
+        assert buf.getitem(4) == '\x01'
+        buf.free()



More information about the Pypy-commit mailing list