[pypy-svn] r25880 - in pypy/dist/pypy/rpython/rctypes: . test

arigo at codespeak.net arigo at codespeak.net
Mon Apr 17 13:40:20 CEST 2006


Author: arigo
Date: Mon Apr 17 13:40:19 2006
New Revision: 25880

Added:
   pypy/dist/pypy/rpython/rctypes/rstringbuf.py   (contents, props changed)
Modified:
   pypy/dist/pypy/rpython/rctypes/astringbuf.py
   pypy/dist/pypy/rpython/rctypes/rmodel.py
   pypy/dist/pypy/rpython/rctypes/test/test_rstringbuf.py
Log:
RTyping string buffers.


Modified: pypy/dist/pypy/rpython/rctypes/astringbuf.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/astringbuf.py	(original)
+++ pypy/dist/pypy/rpython/rctypes/astringbuf.py	Mon Apr 17 13:40:19 2006
@@ -19,6 +19,24 @@
     return annmodel.SomeCTypesObject(StringBufferType,
             annmodel.SomeCTypesObject.OWNSMEMORY)
 
+def stringbuf_specialize_call(hop):
+    from pypy.rpython.lltypesystem import lltype
+    [v_length] = hop.inputargs(lltype.Signed)
+    r_stringbuf = hop.r_result
+    return hop.genop("malloc_varsize", [
+        hop.inputconst(lltype.Void, r_stringbuf.lowleveltype.TO),
+        v_length,
+        ], resulttype=r_stringbuf.lowleveltype,
+    )
+
 extregistry.register_value(create_string_buffer,
     compute_result_annotation=stringbuf_compute_result_annotation,
+    specialize_call=stringbuf_specialize_call,
     )
+
+def stringbuf_get_repr(rtyper, s_stringbuf):
+    from pypy.rpython.rctypes import rstringbuf
+    return rstringbuf.StringBufRepr(rtyper, s_stringbuf, rstringbuf.STRBUFTYPE)
+
+extregistry.register_type(StringBufferType,
+    get_repr=stringbuf_get_repr)

Modified: pypy/dist/pypy/rpython/rctypes/rmodel.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/rmodel.py	(original)
+++ pypy/dist/pypy/rpython/rctypes/rmodel.py	Mon Apr 17 13:40:19 2006
@@ -51,21 +51,20 @@
 
         if self.ownsmemory:
             self.r_memoryowner = self
-            self.lowleveltype = lltype.Ptr(
-                    lltype.GcStruct( "CtypesBox_%s" % (ctype.__name__,),
-                        ( "c_data", self.c_data_type ),
-                        *content_keepalives
-                    )
-                )
+            fields = content_keepalives + [
+                ( "c_data", self.c_data_type ),
+                ]
         else:
             s_memoryowner = SomeCTypesObject(ctype,
                                              SomeCTypesObject.OWNSMEMORY)
             self.r_memoryowner = rtyper.getrepr(s_memoryowner)
-            self.lowleveltype = lltype.Ptr(
+            fields = content_keepalives + [
+                ( "c_data_owner_keepalive", self.r_memoryowner.lowleveltype ),
+                ( "c_data", lltype.Ptr(self.c_data_type) ),
+                ]
+        self.lowleveltype = lltype.Ptr(
                 lltype.GcStruct( "CtypesBox_%s" % (ctype.__name__,),
-                 ( "c_data", lltype.Ptr(self.c_data_type) ),
-                 ( "c_data_owner_keepalive", self.r_memoryowner.lowleveltype ),
-                 *content_keepalives
+                    *fields
                 )
             )
         self.const_cache = {} # store generated const values+original value

Added: pypy/dist/pypy/rpython/rctypes/rstringbuf.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/rpython/rctypes/rstringbuf.py	Mon Apr 17 13:40:19 2006
@@ -0,0 +1,9 @@
+from pypy.rpython.lltypesystem import lltype
+from pypy.rpython.rctypes.rmodel import CTypesRefRepr
+
+
+class StringBufRepr(CTypesRefRepr):
+    pass
+
+
+STRBUFTYPE = lltype.Array(lltype.Char)

Modified: pypy/dist/pypy/rpython/rctypes/test/test_rstringbuf.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/test/test_rstringbuf.py	(original)
+++ pypy/dist/pypy/rpython/rctypes/test/test_rstringbuf.py	Mon Apr 17 13:40:19 2006
@@ -46,3 +46,15 @@
 
         if conftest.option.view:
             a.translator.view()
+
+class Test_specialization:
+    def test_specialize_create(self):
+        def func(n):
+            return create_string_buffer(n)
+
+        res = interpret(func, [17])
+        c_data = res.c_data
+        assert c_data[0] == '\x00'
+        assert c_data[16] == '\x00'
+        assert len(c_data) == 17
+        py.test.raises(IndexError, "c_data[17]")



More information about the Pypy-commit mailing list