Is there a reason why Python's ctypes.CDLL cannot automatically generate restype and argtypes from C header files?
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/myli开发者_Go百科b.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?
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.
精彩评论