Author: Armin Rigo <[email protected]>
Branch: ffi-backend
Changeset: r56602:9d73f57914cd
Date: 2012-08-06 11:12 +0200
http://bitbucket.org/pypy/pypy/changeset/9d73f57914cd/

Log:    in-progress

diff --git a/lib_pypy/_rawffi.py b/lib_pypy/_rawffi.py
--- a/lib_pypy/_rawffi.py
+++ b/lib_pypy/_rawffi.py
@@ -8,6 +8,8 @@
 cffi_type_uchar   = _cffi_backend.new_primitive_type("unsigned char")
 cffi_type_short   = _cffi_backend.new_primitive_type("short")
 cffi_type_ushort  = _cffi_backend.new_primitive_type("unsigned short")
+cffi_type_int     = _cffi_backend.new_primitive_type("int")
+cffi_type_uint    = _cffi_backend.new_primitive_type("unsigned int")
 cffi_type_long    = _cffi_backend.new_primitive_type("long")
 cffi_type_ulong   = _cffi_backend.new_primitive_type("unsigned long")
 cffi_type_longlong   = _cffi_backend.new_primitive_type("long long")
@@ -15,6 +17,7 @@
 cffi_type_float   = _cffi_backend.new_primitive_type("float")
 cffi_type_double  = _cffi_backend.new_primitive_type("double")
 cffi_type_longdouble = _cffi_backend.new_primitive_type("long double")
+cffi_type_wchar_t = _cffi_backend.new_primitive_type("wchar_t")
 
 cffi_type_short_p  = _cffi_backend.new_pointer_type(cffi_type_short)
 cffi_type_ushort_p = _cffi_backend.new_pointer_type(cffi_type_ushort)
@@ -26,7 +29,10 @@
     'b': cffi_type_schar,
     'B': cffi_type_uchar,
     'h': cffi_type_short,
+    'u': cffi_type_wchar_t,
     'H': cffi_type_ushort,
+    'i': cffi_type_int,
+    'I': cffi_type_uint,
     'l': cffi_type_long,
     'L': cffi_type_ulong,
     'q': cffi_type_longlong,
@@ -34,11 +40,15 @@
     'f': cffi_type_float,
     'd': cffi_type_double,
     'g': cffi_type_longdouble,
-    'z': cffi_type_pointer,
-    'P': cffi_type_pointer,
-    'O': cffi_type_pointer,
+    's' : cffi_type_pointer,
+    'P' : cffi_type_pointer,
+    'z' : cffi_type_pointer,
+    'O' : cffi_type_pointer,
+    'Z' : cffi_type_pointer,
+    '?' : cffi_type_uchar,
     }
 
+# ____________________________________________________________
 
 def sizeof(tp_letter):
     return _cffi_backend.sizeof(cffi_types[tp_letter])
@@ -46,33 +56,64 @@
 def alignment(tp_letter):
     return _cffi_backend.alignof(cffi_types[tp_letter])
 
+FUNCFLAG_STDCALL   = 0    # on Windows: for WINAPI calls
+FUNCFLAG_CDECL     = 1    # on Windows: for __cdecl calls
+FUNCFLAG_PYTHONAPI = 4
+FUNCFLAG_USE_ERRNO = 8
+FUNCFLAG_USE_LASTERROR = 16
+
 class CDLL(object):
     def __init__(self, libname):
         if libname is None:
             from ctypes.util import find_library
             libname = find_library('c')
         self._cffi_library = _cffi_backend.load_library(libname)
-        self.libname = libname
+        self._libname = libname
+        self._cache = {}
 
     def getaddressindll(self, name):
         return self._cffi_library.read_variable(cffi_type_pointer, name)
 
+    def ptr(self, name, argtypes, restype, flags=FUNCFLAG_CDECL):
+        """ Get a pointer for function name with provided argtypes
+        and restype
+        """
+        key = name, tuple(argtypes), restype
+        try:
+            return self._cache[key]
+        except KeyError:
+            pass
+        assert not argtypes
+        if restype is None:
+            cffi_restype = cffi_type_void
+        else:
+            cffi_restype = cffi_types[restype]
+        assert isinstance(name, str)
+        cffi_functype = _cffi_backend.new_function_type((), cffi_restype,
+                                                        False)  # XXX abi
+        cfunc = self._cffi_library.load_function(cffi_functype, name)
+        funcptr = FuncPtr(cfunc)
+        self._cache[key] = funcptr
+        return funcptr
+
 def get_libc():
-    return CDLL(None)
-
-FUNCFLAG_STDCALL   = 0    # on Windows: for WINAPI calls
-FUNCFLAG_CDECL     = 1    # on Windows: for __cdecl calls
-FUNCFLAG_PYTHONAPI = 4
-FUNCFLAG_USE_ERRNO = 8
-FUNCFLAG_USE_LASTERROR = 16
+    return CDLL('libc.so.6')    # XXX
 
 class DataInstance(object):
     pass
 
+class FuncPtr(object):
+    def __init__(self, cfunc):
+        self.cfunc = cfunc
+
+# ____________________________________________________________
+
 class Array(DataInstance):
     def __init__(self, shape):
         pass
 
+# ____________________________________________________________
+
 class CallbackPtr(DataInstance):
     def __init__(self, *stuff):
         pass
diff --git a/lib_pypy/pypy_test/test__rawffi.py 
b/lib_pypy/pypy_test/test__rawffi.py
--- a/lib_pypy/pypy_test/test__rawffi.py
+++ b/lib_pypy/pypy_test/test__rawffi.py
@@ -184,8 +184,13 @@
                      AAA_first_ordinal_function
                      ret_un_func
                   """.split()
-        eci = ExternalCompilationInfo(export_symbols=symbols)
-        return str(platform.compile([c_file], eci, 'x', standalone=False))
+        #eci = ExternalCompilationInfo(export_symbols=symbols)
+        #return str(platform.compile([c_file], eci, 'x', standalone=False))
+        import subprocess
+        subprocess.check_call(
+            'gcc xlib.c -shared -fPIC -o testxlib.so',
+            cwd=str(c_file.dirpath()), shell=True)
+        return str(c_file.dirpath().join('testxlib.so'))
     prepare_c_example = staticmethod(prepare_c_example)
     
 ##    def setup_class(cls):
@@ -206,7 +211,10 @@
 ##        cls.w_sizes_and_alignments = space.wrap(dict(
 ##            [(k, (v.c_size, v.c_alignment)) for k,v in TYPEMAP.iteritems()]))
 
-    libc_name = 'libc.so.6'   # XXX
+    def setup_class(cls):
+        cls.libc_name = 'libc.so.6'   # XXX
+        cls.iswin32   = False         # XXX
+        cls.lib_name = cls.prepare_c_example()
 
     def test_libload(self):
         import _rawffi
@@ -218,13 +226,14 @@
             _rawffi.CDLL("xxxxx_this_name_does_not_exist_xxxxx")
         except OSError, e:
             print e
-            assert str(e).startswith("xxxxx_this_name_does_not_exist_xxxxx: ")
+            assert "xxxxx_this_name_does_not_exist_xxxxx" in str(e)
         else:
             raise AssertionError("did not fail??")
 
     def test_libload_None(self):
         if self.iswin32:
             skip("unix specific")
+        skip("XXX in-progress")
         import _rawffi
         # this should return *all* loaded libs, dlopen(NULL)
         dll = _rawffi.CDLL(None)
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to