Author: Armin Rigo <[email protected]>
Branch: 
Changeset: r1190:64f8c3b134b0
Date: 2013-03-08 01:14 +0100
http://bitbucket.org/cffi/cffi/changeset/64f8c3b134b0/

Log:    Rename ffi.dlopen() into ffi.dlopen_abi_unchecked(), and make the
        functionality stick as close as possible to the C-level dlopen().

diff --git a/cffi/api.py b/cffi/api.py
--- a/cffi/api.py
+++ b/cffi/api.py
@@ -99,17 +99,19 @@
                 cache.clear()
 
     def dlopen(self, name, flags=0):
-        """Load and return a dynamic library identified by 'name'.
-        The standard C library can be loaded by passing None.
+        import warnings; warnings.warn(
+            "ffi.dlopen() will be removed; use ffi.dlopen_abi_unchecked()",
+            stacklevel=2)
+        return _make_ffi_library(self, name, flags, guess=True)
+
+    def dlopen_abi_unchecked(self, filename, flags=0):
+        """Load and return a dynamic library file identified by 'filename'.
+        On Posix systems this works exactly like dlopen(); see dlopen(3).
         Note that functions and types declared by 'ffi.cdef()' are not
         linked to a particular library, just like C headers; in the
         library we only look for the actual (untyped) symbols.
         """
-        assert isinstance(name, basestring) or name is None
-        lib, function_cache = _make_ffi_library(self, name, flags)
-        self._function_caches.append(function_cache)
-        self._libraries.append(lib)
-        return lib
+        return _make_ffi_library(self, filename, flags)
 
     def _typeof(self, cdecl, consider_function_as_funcptr=False):
         # string -> ctype object
@@ -340,22 +342,26 @@
         self._cdefsources.append(']')
 
 
-def _make_ffi_library(ffi, libname, flags):
+def _make_ffi_library(ffi, libname, flags, guess=False):
     import os
-    name = libname
-    if name is None:
-        name = 'c'    # on Posix only
+    assert isinstance(libname, basestring) or libname is None
     backend = ffi._backend
-    try:
-        if '.' not in name and '/' not in name:
-            raise OSError
-        backendlib = backend.load_library(name, flags)
-    except OSError:
-        import ctypes.util
-        path = ctypes.util.find_library(name)
-        if path is None:
-            raise OSError("library not found: %r" % (name,))
-        backendlib = backend.load_library(path, flags)
+    if guess:
+        _name = libname
+        if _name is None:
+            _name = 'c'    # on Posix only
+        try:
+            if '.' not in _name and '/' not in _name:
+                raise OSError
+            backendlib = backend.load_library(_name, flags)
+        except OSError:
+            import ctypes.util
+            path = ctypes.util.find_library(_name)
+            if path is None:
+                raise OSError("library not found: %r" % (_name,))
+            backendlib = backend.load_library(path, flags)
+    else:
+        backendlib = backend.load_library(libname, flags)
     #
     def make_accessor(name):
         key = 'function ' + name
@@ -400,4 +406,6 @@
         except UnicodeError:
             pass
     library = FFILibrary()
-    return library, library.__dict__
+    ffi._function_caches.append(library.__dict__)
+    ffi._libraries.append(library)
+    return library
diff --git a/testing/test_function.py b/testing/test_function.py
--- a/testing/test_function.py
+++ b/testing/test_function.py
@@ -3,6 +3,7 @@
 import math, os, sys
 import ctypes.util
 from cffi.backend_ctypes import CTypesBackend
+from cffi.verifier import _get_so_suffix
 from testing.udir import udir
 
 try:
@@ -47,6 +48,17 @@
         x = m.sin(1.23)
         assert x == math.sin(1.23)
 
+    def test_sin_with_dlopen_abi_unchecked(self):
+        if _get_so_suffix() != ".so":
+            py.test.skip("no 'libm.so'")
+        ffi = FFI(backend=self.Backend())
+        ffi.cdef("""
+            double sin(double x);
+        """)
+        m = ffi.dlopen_abi_unchecked("libm.so")
+        x = m.sin(1.23)
+        assert x == math.sin(1.23)
+
     def test_sinf(self):
         if sys.platform == 'win32':
             py.test.skip("no 'sinf'")
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to