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

arigo at codespeak.net arigo at codespeak.net
Mon Apr 17 14:09:56 CEST 2006


Author: arigo
Date: Mon Apr 17 14:09:54 2006
New Revision: 25885

Modified:
   pypy/dist/pypy/rpython/rctypes/rchar_p.py
   pypy/dist/pypy/rpython/rctypes/rstringbuf.py
   pypy/dist/pypy/rpython/rctypes/test/test_ctypes.py
   pypy/dist/pypy/rpython/rctypes/test/test_rarray.py
   pypy/dist/pypy/rpython/rctypes/test/test_rfunc.py
Log:
Automatic cast from a string buffer to c_char_p function arguments.


Modified: pypy/dist/pypy/rpython/rctypes/rchar_p.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/rchar_p.py	(original)
+++ pypy/dist/pypy/rpython/rctypes/rchar_p.py	Mon Apr 17 14:09:54 2006
@@ -3,6 +3,7 @@
 from pypy.rpython.rstr import StringRepr, string_repr
 from pypy.rpython.rctypes.rmodel import CTypesValueRepr, C_ZERO
 from pypy.rpython.rctypes.rarray import ArrayRepr
+from pypy.rpython.rctypes.rstringbuf import StringBufRepr
 from pypy.annotation.pairtype import pairtype
 
 from ctypes import c_char, c_char_p
@@ -75,6 +76,17 @@
         r_temp.setvalue(llops, v_owned_box, v_c_array)
         return llops.convertvar(v_owned_box, r_temp, r_to)
 
+class __extend__(pairtype(StringBufRepr, CCharPRepr)):
+    def convert_from_to((r_from, r_to), v, llops):
+        # warning: no keepalives, only for short-lived conversions like
+        # in argument passing
+        r_temp = r_to.r_memoryowner
+        v_owned_box = r_temp.allocate_instance(llops)
+        v_c_array = r_from.get_c_data_of_item(llops, v, C_ZERO)
+        r_temp.setvalue(llops, v_owned_box, v_c_array)
+        return llops.convertvar(v_owned_box, r_temp, r_to)
+        # XXX some code duplication above
+
 
 CCHARP = lltype.Ptr(lltype.FixedSizeArray(lltype.Char, 1))
 

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 14:09:54 2006
@@ -1,5 +1,6 @@
 from pypy.rpython.lltypesystem import lltype
 from pypy.annotation.pairtype import pairtype
+from pypy.rpython.rbuiltin import gen_cast_subarray_pointer
 from pypy.rpython.rmodel import IntegerRepr, inputconst
 from pypy.rpython.rctypes.rmodel import CTypesRefRepr
 
@@ -20,6 +21,13 @@
         v_box = hop.inputarg(self, 0)
         return hop.gendirectcall(ll_chararrayvalue, v_box)
 
+    def get_c_data_of_item(self, llops, v_stringbuf, v_index):
+        v_array = self.get_c_data(llops, v_stringbuf)
+        return gen_cast_subarray_pointer(llops, ONE_CHAR_PTR,
+                                         v_array, v_index)
+
+ONE_CHAR_PTR = lltype.Ptr(lltype.FixedSizeArray(lltype.Char, 1))
+
 
 class __extend__(pairtype(StringBufRepr, IntegerRepr)):
     def rtype_getitem((r_stringbuf, r_int), hop):

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 14:09:54 2006
@@ -117,5 +117,7 @@
 
     s = S()
     s.p = b
+    s.p.contents.value = '!'
+    assert b.value == '!'
 
     assert len(create_string_buffer(0)) == 0

Modified: pypy/dist/pypy/rpython/rctypes/test/test_rarray.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/test/test_rarray.py	(original)
+++ pypy/dist/pypy/rpython/rctypes/test/test_rarray.py	Mon Apr 17 14:09:54 2006
@@ -38,6 +38,10 @@
         return res
     return func, 1234 * ord('e')
 
+def test_base():
+    func, expected = maketest()
+    assert func() == expected
+
 
 class Test_annotation:
     def test_annotate_array(self):

Modified: pypy/dist/pypy/rpython/rctypes/test/test_rfunc.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/test/test_rfunc.py	(original)
+++ pypy/dist/pypy/rpython/rctypes/test/test_rfunc.py	Mon Apr 17 14:09:54 2006
@@ -9,7 +9,7 @@
 from pypy.rpython.lltypesystem import lltype
 
 from ctypes import cdll
-from ctypes import c_int, c_long, c_char_p, c_char
+from ctypes import c_int, c_long, c_char_p, c_char, create_string_buffer
 
 # __________ the standard C library __________
 
@@ -122,6 +122,17 @@
         res = [interpret(fn, [i]) for i in range(4)]
         assert res == [0, 42, 0, 18238]
 
+    def test_specialize_atoi_stringbuf(self):
+        def fn(n):
+            buf = create_string_buffer(n)
+            buf[0] = '4'
+            buf[1] = '2'
+            return atoi(buf)
+
+        assert fn(11) == 42
+        res = interpret(fn, [11])
+        assert res == 42
+
 class Test_compile:
     def test_compile_labs(self):
         fn = compile(test_labs, [int])



More information about the Pypy-commit mailing list