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

arigo at codespeak.net arigo at codespeak.net
Sat Apr 29 15:24:04 CEST 2006


Author: arigo
Date: Sat Apr 29 15:24:04 2006
New Revision: 26576

Modified:
   pypy/dist/pypy/rpython/rctypes/rstringbuf.py
   pypy/dist/pypy/rpython/rctypes/test/test_rstringbuf.py
Log:
Assign to .value on string buffer objects in rctypes.


Modified: pypy/dist/pypy/rpython/rctypes/rstringbuf.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/rstringbuf.py	(original)
+++ pypy/dist/pypy/rpython/rctypes/rstringbuf.py	Sat Apr 29 15:24:04 2006
@@ -23,6 +23,13 @@
         hop.exception_cannot_occur()
         return hop.gendirectcall(ll_chararrayvalue, v_box)
 
+    def rtype_setattr(self, hop):
+        s_attr = hop.args_s[1]
+        assert s_attr.is_constant()
+        assert s_attr.const == 'value'
+        v_box, v_attr, v_value = hop.inputargs(self, lltype.Void, string_repr)
+        hop.gendirectcall(ll_stringbuf_setvalue_from_string, v_box, v_value)
+
     def get_c_data_of_item(self, llops, v_stringbuf, v_index):
         v_array = self.get_c_data(llops, v_stringbuf)
         v_char_p = llops.genop('direct_arrayitems', [v_array],
@@ -94,4 +101,16 @@
     return newstr
 
 
+def ll_stringbuf_setvalue_from_string(box, s):
+    # Copy the string into the stringbuf.  In ctypes the final \x00 is
+    # copied unless the string has exactly the same size as the stringbuf.
+    # We do the same, but unlike ctypes don't raise ValueError if the
+    # string is longer than the stringbuf; we just truncate instead.
+    # There is no support for setattr raising exceptions in RPython so far.
+    p = box.c_data
+    n = min(len(s.chars) + 1, len(p))
+    for i in range(n):
+        p[i] = s.chars[i]
+
+
 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	Sat Apr 29 15:24:04 2006
@@ -124,6 +124,18 @@
         res = interpret(func, [12])
         assert ''.join(res.chars) == "xy"
 
+    def test_specialize_setvalue(self):
+        def func(n):
+            buf = create_string_buffer(n)
+            buf.value = 'abcde'
+            assert buf.value == 'abcde'
+            buf.value = 'x'
+            assert buf.value == 'x'
+            return ord(buf[2])
+
+        res = interpret(func, [12])
+        assert res == ord('c')    # not overridden by buf.value='x'
+
     def test_specialize_slice(self):
         def func(n):
             buf = create_string_buffer(n)



More information about the Pypy-commit mailing list