Commits

Armin Rigo committed 6da597c

load_function().

Comments (0)

Files changed (1)

pypy/module/_cffi_backend/libraryobj.py

 from pypy.interpreter.gateway import interp2app, unwrap_spec
 from pypy.interpreter.typedef import TypeDef
 from pypy.rpython.lltypesystem import lltype, rffi
-from pypy.rlib.rdynload import DLLHANDLE, dlopen, dlclose, DLOpenError
+from pypy.rlib.rdynload import DLLHANDLE, dlopen, dlsym, dlclose, DLOpenError
+
+from pypy.module._cffi_backend.cdataobj import W_CData
+from pypy.module._cffi_backend.ctypefunc import W_CTypeFunc
 
 
 class W_Library(Wrappable):
         space = self.space
         return space.wrap("<clibrary '%s'>" % self.name)
 
+    @unwrap_spec(ctypefunc=W_CTypeFunc, name=str)
+    def load_function(self, ctypefunc, name):
+        space = self.space
+        cdata = dlsym(self.handle, name)
+        if not cdata:
+            raise operationerrfmt(space.w_KeyError,
+                                  "function '%s' not found in library '%s'",
+                                  name, self.name)
+        return W_CData(space, rffi.cast(rffi.CCHARP, cdata), ctypefunc)
+
 
 W_Library.typedef = TypeDef(
     '_cffi_backend.Library',
     __repr__ = interp2app(W_Library.repr),
+    load_function = interp2app(W_Library.load_function),
     )
 W_Library.acceptable_as_base_class = False