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

arigo at codespeak.net arigo at codespeak.net
Mon Apr 17 13:52:27 CEST 2006


Author: arigo
Date: Mon Apr 17 13:52:25 2006
New Revision: 25881

Modified:
   pypy/dist/pypy/rpython/rctypes/astringbuf.py
   pypy/dist/pypy/rpython/rctypes/rstringbuf.py
   pypy/dist/pypy/rpython/rctypes/test/test_ctypes.py
   pypy/dist/pypy/rpython/rctypes/test/test_rstringbuf.py
Log:
Operations on 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:52:25 2006
@@ -38,5 +38,9 @@
     from pypy.rpython.rctypes import rstringbuf
     return rstringbuf.StringBufRepr(rtyper, s_stringbuf, rstringbuf.STRBUFTYPE)
 
-extregistry.register_type(StringBufferType,
+entry = extregistry.register_type(StringBufferType,
     get_repr=stringbuf_get_repr)
+def stringbuf_get_field_annotation(s_array, fieldname):
+    assert fieldname == 'value'
+    return annmodel.SomeString()   # can_be_None = False
+entry.get_field_annotation = stringbuf_get_field_annotation

Modified: pypy/dist/pypy/rpython/rctypes/rstringbuf.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/rstringbuf.py	(original)
+++ pypy/dist/pypy/rpython/rctypes/rstringbuf.py	Mon Apr 17 13:52:25 2006
@@ -1,9 +1,39 @@
 from pypy.rpython.lltypesystem import lltype
+from pypy.annotation.pairtype import pairtype
+from pypy.rpython.rmodel import IntegerRepr, inputconst
 from pypy.rpython.rctypes.rmodel import CTypesRefRepr
 
 
 class StringBufRepr(CTypesRefRepr):
-    pass
+
+    def rtype_len(self, hop):
+        [v_stringbuf] = hop.inputargs(self)
+        v_array = self.get_c_data(hop.llops, v_stringbuf)
+        return hop.genop('getarraysize', [v_array],
+                         resulttype = lltype.Signed)
+
+    def rtype_getattr(self, hop):
+        from pypy.rpython.rctypes.rarray import ll_chararrayvalue
+        s_attr = hop.args_s[1]
+        assert s_attr.is_constant()
+        assert s_attr.const == 'value'
+        v_box = hop.inputarg(self, 0)
+        return hop.gendirectcall(ll_chararrayvalue, v_box)
+
+
+class __extend__(pairtype(StringBufRepr, IntegerRepr)):
+    def rtype_getitem((r_stringbuf, r_int), hop):
+        v_stringbuf, v_index = hop.inputargs(r_stringbuf, lltype.Signed)
+        v_array = r_stringbuf.get_c_data(hop.llops, v_stringbuf)
+        return hop.genop('getarrayitem', [v_array, v_index],
+                         resulttype = lltype.Char)
+
+    def rtype_setitem((r_stringbuf, r_int), hop):
+        v_stringbuf, v_index, v_item = hop.inputargs(r_stringbuf,
+                                                     lltype.Signed,
+                                                     lltype.Char)
+        v_array = r_stringbuf.get_c_data(hop.llops, v_stringbuf)
+        hop.genop('setarrayitem', [v_array, v_index, v_item])
 
 
 STRBUFTYPE = lltype.Array(lltype.Char)

Modified: pypy/dist/pypy/rpython/rctypes/test/test_ctypes.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/test/test_ctypes.py	(original)
+++ pypy/dist/pypy/rpython/rctypes/test/test_ctypes.py	Mon Apr 17 13:52:25 2006
@@ -100,6 +100,7 @@
 
     b = create_string_buffer(3)
     assert type(b) is type(a)
+    assert len(b) == 3
 
     b.value = "nxw"
     assert b[0] == 'n'
@@ -116,3 +117,5 @@
 
     s = S()
     s.p = b
+
+    assert len(create_string_buffer(0)) == 0

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:52:25 2006
@@ -47,6 +47,18 @@
         if conftest.option.view:
             a.translator.view()
 
+    def test_annotate_len(self):
+        def func(n):
+            buf = create_string_buffer(n)
+            return len(buf)
+
+        a = RPythonAnnotator()
+        s = a.build_types(func, [int])
+        assert s.knowntype == int
+
+        if conftest.option.view:
+            a.translator.view()
+
 class Test_specialization:
     def test_specialize_create(self):
         def func(n):
@@ -58,3 +70,33 @@
         assert c_data[16] == '\x00'
         assert len(c_data) == 17
         py.test.raises(IndexError, "c_data[17]")
+
+    def test_specialize_access(self):
+        def func(n):
+            buf = create_string_buffer(n)
+            buf[0] = 'x'
+            buf[1] = 'y'
+            return buf[0]
+
+        res = interpret(func, [17])
+        assert res == 'x'
+
+    def test_specialize_len(self):
+        def func(n):
+            buf = create_string_buffer(n)
+            return len(buf)
+
+        res = interpret(func, [17])
+        assert res == 17
+        res = interpret(func, [0])
+        assert res == 0
+
+    def test_annotate_value(self):
+        def func(n):
+            buf = create_string_buffer(n)
+            buf[0] = 'x'
+            buf[1] = 'y'
+            return buf.value
+
+        res = interpret(func, [12])
+        assert ''.join(res.chars) == "xy"



More information about the Pypy-commit mailing list