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

arigo at codespeak.net arigo at codespeak.net
Fri Apr 14 18:37:29 CEST 2006


Author: arigo
Date: Fri Apr 14 18:37:28 2006
New Revision: 25832

Modified:
   pypy/dist/pypy/rpython/rctypes/rfunc.py
   pypy/dist/pypy/rpython/rctypes/test/_rctypes_test.c
   pypy/dist/pypy/rpython/rctypes/test/test_rctypes.py
Log:
Support for rctypes functions returning void (restype==None).


Modified: pypy/dist/pypy/rpython/rctypes/rfunc.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/rfunc.py	(original)
+++ pypy/dist/pypy/rpython/rctypes/rfunc.py	Fri Apr 14 18:37:28 2006
@@ -15,6 +15,8 @@
         Answer the annotation of the external function's result
         """
         result_ctype = instance.restype
+        if result_ctype is None:
+            return None
         s_result = annmodel.SomeCTypesObject(result_ctype,
                                          annmodel.SomeCTypesObject.OWNSMEMORY)
         return s_result.return_annotation()
@@ -51,18 +53,22 @@
             # generate the correct dereferencing
             unwrapped_args_v.append(r_arg.get_c_data(hop.llops, v))
             ARGTYPES.append(r_arg.c_data_type)
-    s_res = annmodel.SomeCTypesObject(cfuncptr.restype,
-                                      annmodel.SomeCTypesObject.OWNSMEMORY)
-    r_res = hop.rtyper.getrepr(s_res)
+    if cfuncptr.restype is not None:
+        s_res = annmodel.SomeCTypesObject(cfuncptr.restype,
+                                          annmodel.SomeCTypesObject.OWNSMEMORY)
+        r_res = hop.rtyper.getrepr(s_res)
+        RESTYPE = r_res.ll_type
+    else:
+        RESTYPE = lltype.Void
 
     ll_func = getattr(cfuncptr, 'llinterp_friendly_version', None)
     includes = getattr(cfuncptr, 'includes', ())
     v_result = hop.llops.gencapicall(fnname, unwrapped_args_v,
-                                     resulttype = r_res.ll_type,
+                                     resulttype = RESTYPE,
                                      _callable = ll_func,
                                      includes = includes)
     # XXX hack! hack! temporary! I promize!
-    FUNCTYPE = lltype.FuncType(ARGTYPES, r_res.ll_type)
+    FUNCTYPE = lltype.FuncType(ARGTYPES, RESTYPE)
     last_op = hop.llops[-1]
     assert last_op.opname == 'direct_call'
     last_op.args[0].concretetype = lltype.Ptr(FUNCTYPE)
@@ -70,7 +76,10 @@
     last_op.args[0].value._set_T(FUNCTYPE)
     last_op.args[0].value._obj._TYPE = FUNCTYPE
 
-    return r_res.return_value(hop.llops, v_result)
+    if RESTYPE is lltype.Void:
+        return None
+    else:
+        return r_res.return_value(hop.llops, v_result)
 
 extregistry.register_metatype(CFuncPtrType, 
     compute_annotation=cfuncptrtype_compute_annotation,

Modified: pypy/dist/pypy/rpython/rctypes/test/_rctypes_test.c
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/test/_rctypes_test.c	(original)
+++ pypy/dist/pypy/rpython/rctypes/test/_rctypes_test.c	Fri Apr 14 18:37:28 2006
@@ -29,6 +29,13 @@
     return in.x + in.y;
 }
 
+EXPORT(void) _testfunc_swap(point *p)
+{
+    int tmp = p->x;
+    p->x = p->y;
+    p->y = tmp;
+}
+
 EXPORT(int) _testfunc_struct(point in)
 {
     return in.x + in.y;

Modified: pypy/dist/pypy/rpython/rctypes/test/test_rctypes.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/test/test_rctypes.py	(original)
+++ pypy/dist/pypy/rpython/rctypes/test/test_rctypes.py	Fri Apr 14 18:37:28 2006
@@ -91,6 +91,16 @@
     ll_testfunc_struct_pointer_id)
 testfunc_struct_pointer_id.includes = includes
 
+# _testfunc_swap
+testfunc_swap = _rctypes_test._testfunc_swap
+testfunc_swap.restype = None
+testfunc_swap.argtypes = [tagpointptr]
+
+def ll_testfunc_swap(p):
+    p.x, p.y = p.y, p.x
+testfunc_swap.llinterp_friendly_version = ll_testfunc_swap
+testfunc_swap.includes = includes
+
 
 def test_testfunc_struct():
     in_point = tagpoint()
@@ -120,6 +130,15 @@
     assert in_point.x == 21
     return in_point.x - in_point.y       # this test function is reused below
 
+def test_testfunc_swap():
+    pt = tagpoint()
+    pt.x = 5
+    pt.y = 9
+    testfunc_swap(pointer(pt))
+    assert pt.x == 9
+    assert pt.y == 5
+    return pt.x - pt.y                   # this test function is reused below
+
 class Test_annotation:
     def test_annotate_struct(self):
         t = TranslationContext()
@@ -150,6 +169,10 @@
         res = interpret(test_testfunc_struct_pointer_id, [])
         assert res == 21 - 17
 
+    def test_specialize_swap(self):
+        res = interpret(test_testfunc_swap, [])
+        assert res == 4
+
 class Test_compile:
     def test_compile_byval(self):
         fn = compile(test_testfunc_byval, [])
@@ -158,3 +181,7 @@
     def test_compile_struct_pointer_id(self):
         fn = compile(test_testfunc_struct_pointer_id, [])
         assert fn() == 21 - 17
+
+    def test_compile_swap(self):
+        fn = compile(test_testfunc_swap, [])
+        assert fn() == 4



More information about the Pypy-commit mailing list