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

arigo at codespeak.net arigo at codespeak.net
Mon Jun 12 23:14:56 CEST 2006


Author: arigo
Date: Mon Jun 12 23:14:55 2006
New Revision: 28730

Modified:
   pypy/dist/pypy/rpython/rctypes/astringbuf.py
   pypy/dist/pypy/rpython/rctypes/rstringbuf.py
   pypy/dist/pypy/rpython/rctypes/test/test_rstringbuf.py
Log:
Support for the stringbuf.raw attribute in rctypes.


Modified: pypy/dist/pypy/rpython/rctypes/astringbuf.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/astringbuf.py	(original)
+++ pypy/dist/pypy/rpython/rctypes/astringbuf.py	Mon Jun 12 23:14:55 2006
@@ -40,7 +40,7 @@
     _type_ = StringBufferType
 
     def get_field_annotation(self, s_array, fieldname):
-        assert fieldname == 'value'
+        assert fieldname in ('value', 'raw')
         return SomeString()   # can_be_None = False
 
     def get_repr(self, rtyper, s_stringbuf):

Modified: pypy/dist/pypy/rpython/rctypes/rstringbuf.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/rstringbuf.py	(original)
+++ pypy/dist/pypy/rpython/rctypes/rstringbuf.py	Mon Jun 12 23:14:55 2006
@@ -5,6 +5,7 @@
 from pypy.objspace.flow.model import Constant
 from pypy.rpython.rslice import AbstractSliceRepr
 from pypy.rpython.lltypesystem.rstr import string_repr
+from pypy.rpython.error import TyperError
 
 class StringBufRepr(CTypesRefRepr):
 
@@ -15,13 +16,18 @@
                          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)
         hop.exception_cannot_occur()
-        return hop.gendirectcall(ll_chararrayvalue, v_box)
+        if s_attr.const == 'value':
+            from pypy.rpython.rctypes.rarray import ll_chararrayvalue
+            return hop.gendirectcall(ll_chararrayvalue, v_box)
+        elif s_attr.const == 'raw':
+            return hop.gendirectcall(ll_stringbufraw, v_box)
+        else:
+            raise TyperError("StringBufRepr has no attribute %r" % (
+                s_attr.const,))
 
     def rtype_setattr(self, hop):
         s_attr = hop.args_s[1]
@@ -112,5 +118,12 @@
     for i in range(n):
         p[i] = s.chars[i]
 
+def ll_stringbufraw(box):
+    p = box.c_data
+    length = len(p)
+    newstr = lltype.malloc(string_repr.lowleveltype.TO, length)
+    for i in range(length):
+        newstr.chars[i] = p[i]
+    return newstr
 
 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 Jun 12 23:14:55 2006
@@ -124,6 +124,17 @@
         res = interpret(func, [12])
         assert ''.join(res.chars) == "xy"
 
+    def test_specialize_raw(self):
+        def func(n):
+            buf = create_string_buffer(n)
+            buf[0] = 'x'
+            buf[1] = '\x00'
+            buf[2] = 'y'
+            return buf.raw
+
+        res = interpret(func, [3])
+        assert ''.join(res.chars) == "x\x00y"
+
     def test_specialize_setvalue(self):
         def func(n):
             buf = create_string_buffer(n)



More information about the Pypy-commit mailing list