For example, it would be nice to be able to do this:
from ctypes import CDLL
mylib = CDLL('/my/path/mylib.so',header='/some/path/mylib.h')
instead of
from ctypes import *
mylib = CDLL('/my/path/mylib.so')
mylib.f.restype = c_double
mylib.f.argtypes = [c_double, c_double]
mylib.g.restype = c_int
mylib.g.argtypes = [c_double, c_int]
My experience with python suggests that either something very close to this has been done already and I just haven't been able to find it, or that there is a good reason not to. Are either of these the case?
ctypes is the de facto standard library for interfacing with C/C++ from CPython, and it provides not only full access to the native C interface of most major operating systems (e.g., kernel32 on Windows, or libc on *nix), but also provides support for loading and interfacing with dynamic libraries, such as DLLs or ...
ctypes is a foreign function library for Python. It provides C compatible data types, and allows calling functions in DLLs or shared libraries. It can be used to wrap these libraries in pure Python.
I asked myself the same question and before I traveled down that road too far, I ran into ctypesgen:
http://code.google.com/p/ctypesgen/
It will handle all of this for you, although you will need to do a little learning up front. We use ctypesgen to generate one version of the Python bindings for the Subversion bindings. It works very well.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With