[pypy-svn] r43552 - in pypy/branch/kill-ctypes/pypy/rpython/lltypesystem: . test

fijal at codespeak.net fijal at codespeak.net
Tue May 22 10:57:41 CEST 2007


Author: fijal
Date: Tue May 22 10:57:41 2007
New Revision: 43552

Added:
   pypy/branch/kill-ctypes/pypy/rpython/lltypesystem/rffi.py
   pypy/branch/kill-ctypes/pypy/rpython/lltypesystem/test/test_rffi.py
Log:
A simple wrapper to allow easy defining function as external


Added: pypy/branch/kill-ctypes/pypy/rpython/lltypesystem/rffi.py
==============================================================================
--- (empty file)
+++ pypy/branch/kill-ctypes/pypy/rpython/lltypesystem/rffi.py	Tue May 22 10:57:41 2007
@@ -0,0 +1,9 @@
+
+from pypy.rpython.lltypesystem.lltype import functionptr, FuncType
+
+def llexternal(name, args, result, sources=[], includes=[]):
+    ext_type = FuncType(args, result)
+    return functionptr(ext_type, name, external='C', sources=tuple(sources),
+                       includes=tuple(includes))
+
+

Added: pypy/branch/kill-ctypes/pypy/rpython/lltypesystem/test/test_rffi.py
==============================================================================
--- (empty file)
+++ pypy/branch/kill-ctypes/pypy/rpython/lltypesystem/test/test_rffi.py	Tue May 22 10:57:41 2007
@@ -0,0 +1,38 @@
+
+from pypy.rpython.lltypesystem.rffi import llexternal
+from pypy.translator.c.test.test_genc import compile
+from pypy.rpython.lltypesystem.lltype import Signed
+from pypy.tool.udir import udir
+
+def test_basic():
+    c_source = """
+    int z(int x)
+    {
+        return (x + 3);
+    }
+    """
+    c_file = udir.join("stuff.c")
+    c_file.write(c_source)
+    z = llexternal('z', [Signed], Signed, sources=[str(c_file)])
+
+    def f():
+        return z(8)
+
+    xf = compile(f, [])
+    assert xf() == 8+3
+
+def test_hashdefine():
+    c_source = """
+    #define X(i) (i+3)
+    """
+
+    c_file = udir.join("stuff.c")
+    c_file.write(c_source)
+
+    z = llexternal('X', [Signed], Signed, includes=[str(c_file)])
+
+    def f():
+        return z(8)
+
+    xf = compile(f, [])
+    assert xf() == 8+3



More information about the Pypy-commit mailing list