Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a reason why Python's ctypes.CDLL cannot automatically generate restype and argtypes from C header files?

Tags:

python

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?

like image 318
EHN Avatar asked Feb 17 '11 18:02

EHN


People also ask

Does ctypes work with C++?

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 ...

How does Python ctypes work?

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.


1 Answers

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.

like image 179
Jeremy Whitlock Avatar answered Sep 28 '22 06:09

Jeremy Whitlock