[pypy-svn] r75442 - in pypy/branch/fast-ctypes/pypy/module/jitffi: . test

getxsick at codespeak.net getxsick at codespeak.net
Thu Jun 17 23:04:31 CEST 2010


Author: getxsick
Date: Thu Jun 17 23:04:29 2010
New Revision: 75442

Added:
   pypy/branch/fast-ctypes/pypy/module/jitffi/   (props changed)
   pypy/branch/fast-ctypes/pypy/module/jitffi/__init__.py   (contents, props changed)
   pypy/branch/fast-ctypes/pypy/module/jitffi/interp_jitffi.py   (contents, props changed)
   pypy/branch/fast-ctypes/pypy/module/jitffi/test/   (props changed)
   pypy/branch/fast-ctypes/pypy/module/jitffi/test/__init__.py   (contents, props changed)
   pypy/branch/fast-ctypes/pypy/module/jitffi/test/test_jitffi.py   (contents, props changed)
Log:
module/jitffi is back. it doesn't work, just initial check in.


Added: pypy/branch/fast-ctypes/pypy/module/jitffi/__init__.py
==============================================================================
--- (empty file)
+++ pypy/branch/fast-ctypes/pypy/module/jitffi/__init__.py	Thu Jun 17 23:04:29 2010
@@ -0,0 +1,8 @@
+from pypy.interpreter.mixedmodule import MixedModule
+
+class Module(MixedModule):
+    interpleveldefs = {
+        'CDLL' : 'interp_jitffi.W_CDLL',
+    }
+
+    appleveldefs = {}

Added: pypy/branch/fast-ctypes/pypy/module/jitffi/interp_jitffi.py
==============================================================================
--- (empty file)
+++ pypy/branch/fast-ctypes/pypy/module/jitffi/interp_jitffi.py	Thu Jun 17 23:04:29 2010
@@ -0,0 +1,32 @@
+from pypy.rlib import rjitffi
+from pypy.interpreter.baseobjspace import ObjSpace, W_Root, Wrappable
+from pypy.interpreter.error import wrap_oserror
+from pypy.interpreter.gateway import interp2app
+from pypy.interpreter.typedef import TypeDef
+
+class W_CDLL(Wrappable, rjitffi.CDLL):
+    def __init__(self, space, name):
+        self.space = space
+        super(W_CDLL, self).__init__(name)
+
+    def get_w(self, func, w_args_type, res_type='void'):
+        args_type_w = [ self.space.str_w(w_x)
+                        for w_x in self.space.listview(w_args_type) ]
+        return self.space.wrap(self.get(func, args_type_w, res_type))
+
+def descr_new_cdll(space, w_type, name):
+    try:
+        return space.wrap(W_CDLL(space, name))
+    except OSError, e:
+        raise wrap_oserror(space, e)
+descr_new_cdll.unwrap_spec = [ObjSpace, W_Root, str]
+
+W_CDLL.typedef = TypeDef(
+        'CDLL',
+        __new__ = interp2app(descr_new_cdll),
+        get     = interp2app(W_CDLL.get_w, unwrap_spec=['self',
+                                                        str, W_Root, str]),
+        __doc__ = """ C Dynamically loaded library
+use CDLL(libname) to create a handle to a C library (the argument is processed
+the same way as dlopen processes it)."""
+)

Added: pypy/branch/fast-ctypes/pypy/module/jitffi/test/__init__.py
==============================================================================

Added: pypy/branch/fast-ctypes/pypy/module/jitffi/test/test_jitffi.py
==============================================================================
--- (empty file)
+++ pypy/branch/fast-ctypes/pypy/module/jitffi/test/test_jitffi.py	Thu Jun 17 23:04:29 2010
@@ -0,0 +1,97 @@
+from pypy.conftest import gettestobjspace
+from pypy.translator.tool.cbuild import ExternalCompilationInfo
+from pypy.translator.platform import platform
+
+import py
+
+class AppTestJitffi(object):
+    @staticmethod
+    def preprare_c_example():
+        from pypy.tool.udir import udir
+        c_file = udir.ensure("test_jitffi", dir=True).join("xlib.c")
+        c_file.write(py.code.Source('''
+        int add_integers(int a, int b)
+        {
+           return a+b;
+        }
+
+        double add_floats(double a, double b)
+        {
+           return a+b;
+        }
+
+        double return_float(int a, int b)
+        {
+           return a+b;
+        }
+
+        int max3(int a, int b, int c)
+        {
+           int max = a;
+           if (b > max) max = b;
+           if (c > max) max = c;
+           return max;
+        }
+
+        int fvoid(void)
+        {
+           return 1;
+        }
+
+        void return_void(int a, int b)
+        {
+            int c;
+            c = a + b;
+        }
+        '''
+        ))
+
+        symbols = ['add_integers', 'add_floats', 'return_float',
+                   'max3', 'fvoid', 'return_void']
+        eci = ExternalCompilationInfo(export_symbols=symbols)
+
+        return str(platform.compile([c_file], eci, 'x', standalone=False))
+
+    def setup_class(cls):
+        space = gettestobjspace(usemodules=('jitffi',))
+        cls.space = space
+        cls.w_lib_name = space.wrap(cls.preprare_c_example())
+
+    #def test_missing_lib(self):
+    #    py.test.raises(OSError, rjitffi.CDLL, 'xxxfoo888baryyy')
+
+    def test_get(self):
+        import jitffi
+        lib = jitffi.CDLL(self.lib_name)
+
+        func = lib.get('add_integers', ['int', 'int'], 'int')
+        assert 3 == func.call(1,2)
+        func = lib.get('add_integers', ['int', 'int'], 'int')
+        assert 1 == func.call(-1,2)
+        func = lib.get('add_integers', ['int', 'int'], 'int')
+        assert 0 == func.call(0,0)
+
+        func = lib.get('max3', ['int', 'int', 'int'], 'int')
+        assert 8 == func.call(2, 8, 3)
+
+        func = lib.get('add_floats', ['float', 'float'], 'float')
+        assert 2.7 == func.call(1.2, 1.5)
+
+    def test_get_void(self):
+        import jitffi
+        lib = jitffi.CDLL(self.lib_name)
+
+        func = lib.get('fvoid', [], 'int')
+        assert 1 == func.call()
+
+        func = lib.get('return_void', ['int', 'int'], 'void')
+        assert func.call(1, 2) is None
+        func = lib.get('return_void', ['int', 'int'])
+        assert func.call(1, 2) is None
+
+    def test_undefined_func(self):
+        import jitffi
+        lib = jitffi.CDLL(self.lib_name)
+        # xxxfoo888baryyy - not existed function
+        py.test.raises(ValueError, lib.get, 'xxxfoo888baryyy', [])
+        py.test.raises(ValueError, lib.get, 'xxxfoo888baryyy', ['int'], 'int')



More information about the Pypy-commit mailing list