Python Forum

Full Version: generating ctypes wrapper for a c library?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
hi,
i want to wrap a C header into ctypes
but, as the header file is large, (lots of structs, callbacks etc), i don't want to put most of my effort on writing the ctypes wrapper by hand
is there any way of parsing the header and generate the ctypes wrapper?
i have seen pycparser, but i don't know how to generate ctypes wrappers with it.
thanks
python has ctypes built in:
import ctypes
hi again,
i know ctypes can be imported!.
but the problem is interfacing the module with it. (like the structs, enums, #defines, unions, pointers to functions etc)
for example:
from ctypes import *
class vector3f(Structure):
    _fields_ = [
("x", c_float),
("y", c_float),
("z", c_float)
]
this was a simple structure
but, i need to generate them automatically. not writing everything by hand.
since my library has pointers to functions, this can't help me. otherwise i would have written a swig wrapper for it!.
Obviously people thought of using pycparser for generating ctypes wrappers. A search for the keywords pycparser ctypes yields this module among other results. Could it be what you're looking for?
somehow yes.
but i've tried using it, but gives errors like cannot import pipeline
first printer_python, which i've copied that into my site-packages directory, then pipeline which i couldn't install from pip and i don't know where should i get that.
(Nov-03-2018, 04:01 PM)brighteningeyes Wrote: [ -> ]which i've copied that into my site-packages directory
This is not the correct way to install python modules. Have you tried
Output:
python3 -m pip install ctypesgen
hi again,
i didn't know that this module is also available on pip
first i got ctypesgen from gitHub and tried running it's setup.py install
the error of ModuleNotFoundError: could not find pipeline was fixed because i have mistakenly copied ctypesgen's processor into my site-packages directory replacing the processor module
now, the error that i got:
Error:
Collecting ctypesgen Using cached https://files.pythonhosted.org/packages/b8/9d/13bcf53a190d2a5b3512d3c116920b4a2fadf007600722114b1a17602524/ctypesgen-0.r125.tar.gz Complete output from command python setup.py egg_info: Traceback (most recent call last): File "<string>", line 1, in <module> File "C:\Users\Amir\AppData\Local\Temp\pip-install-y0k3z6ra\ctypesgen\setup.py", line 13, in <module> import ctypesgencore File "C:\Users\Amir\AppData\Local\Temp\pip-install-y0k3z6ra\ctypesgen\ctypesgencore\__init__.py", line 60, in <module> import descriptions ModuleNotFoundError: No module named 'descriptions' ----------------------------------------
by trying pip install descriptions, i couldn't find it on pip.
thanks
it seems that ctypesgen or processor is available for python2 (not python3) although in the processor's pip page, it said in it's classifiers that it is available for python3