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

arigo at codespeak.net arigo at codespeak.net
Sat Nov 11 19:24:41 CET 2006


Author: arigo
Date: Sat Nov 11 19:24:40 2006
New Revision: 34495

Modified:
   pypy/dist/pypy/rlib/rctypesobject.py
   pypy/dist/pypy/rlib/test/test_rctypesobject.py
Log:
rc_char_p.


Modified: pypy/dist/pypy/rlib/rctypesobject.py
==============================================================================
--- pypy/dist/pypy/rlib/rctypesobject.py	(original)
+++ pypy/dist/pypy/rlib/rctypesobject.py	Sat Nov 11 19:24:40 2006
@@ -122,6 +122,51 @@
 rc_char = Primitive(lltype.Char)
 
 
+class _RCTypesStringData(object):
+    ARRAYTYPE    = lltype.Array(lltype.Char, hints={'nolength': True})
+    FIRSTITEMOFS = llmemory.ArrayItemsOffset(ARRAYTYPE)
+    ITEMOFS      = llmemory.sizeof(lltype.Char)
+    
+    def __init__(self, string):
+        rawsize = self.FIRSTITEMOFS + self.ITEMOFS * (len(string) + 1)
+        self.addr = llmemory.raw_malloc(rawsize)
+        a = self.addr + self.FIRSTITEMOFS
+        for i in range(len(string)):
+            a.char[0] = string[i]
+            a += self.ITEMOFS
+        a.char[0] = '\x00'
+    def __del__(self):
+        llmemory.raw_free(self.addr)
+
+class RCTypesCharP(RCTypesObject):
+    LLTYPE = lltype.Ptr(_RCTypesStringData.ARRAYTYPE)
+
+    def strlen(self):
+        ptr = self.ll_ref(RCTypesCharP.CDATATYPE)
+        a = ptr[0]
+        n = 0
+        while a[n] != '\x00':
+            n += 1
+        return n
+
+    def get_value(self):
+        length = self.strlen()
+        ptr = self.ll_ref(RCTypesCharP.CDATATYPE)
+        a = ptr[0]
+        lst = ['\x00'] * length
+        for i in range(length):
+            lst[i] = a[i]
+        return ''.join(lst)
+
+    def set_value(self, string):
+        data = _RCTypesStringData(string)
+        ptr = self.ll_ref(RCTypesCharP.CDATATYPE)
+        ptr[0] = llmemory.cast_adr_to_ptr(data.addr, RCTypesCharP.LLTYPE)
+        self._keepalive_stringdata = data
+
+rc_char_p = RCTypesCharP
+
+
 def RPointer(contentscls):
     """Build and return a new RCTypesPointer class."""
     try:
@@ -240,6 +285,8 @@
     """Build and return a new RCTypesVarArray class.
     Note that this is *not* a subclass of RCTypesObject, so you cannot
     take a pointer to it, use it as a field of a structure, etc.
+    You can take a pointer to one of its elements (e.g. the first),
+    though, and that pointer will keep the whole array alive.
     """
     try:
         return itemcls._vararraycls

Modified: pypy/dist/pypy/rlib/test/test_rctypesobject.py
==============================================================================
--- pypy/dist/pypy/rlib/test/test_rctypesobject.py	(original)
+++ pypy/dist/pypy/rlib/test/test_rctypesobject.py	Sat Nov 11 19:24:40 2006
@@ -101,6 +101,21 @@
         res = self.do(func)
         assert res == 8
 
+    def test_char_p(self):
+        def func():
+            p = rc_char_p.allocate()
+            s = ''
+            for i in range(65, 91):
+                s += chr(i)
+            p.set_value(s)
+            del s
+            s = p.get_value()
+            for i in range(26):
+                assert ord(s[i]) == 65 + i
+            return len(s)
+        res = self.do(func)
+        assert res == 26
+
 
 class TestLLInterpreted(TestBasic):
     POLICY = AnnotatorPolicy()



More information about the Pypy-commit mailing list