Python Forum
generating ctypes wrapper for a c library?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
generating ctypes wrapper for a c library?
#1
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
Reply
#2
python has ctypes built in:
import ctypes
Reply
#3
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.
Reply
#4
Does this help?: https://docs.python.org/3.7/extending/extending.html
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
since my library has pointers to functions, this can't help me. otherwise i would have written a swig wrapper for it!.
Reply
#6
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?
Reply
#7
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.
Reply
#8
(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
Reply
#9
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
Reply
#10
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  using ctypes to use a dll in a python module dauriac 3 379 Mar-06-2024, 04:38 PM
Last Post: dauriac
  Trying to debug segfault in ctypes binding to Fortran bthomas 1 600 Sep-01-2023, 11:26 AM
Last Post: bthomas
  ctypes juliolop 7 1,390 Apr-20-2023, 03:33 PM
Last Post: Larz60+
  SystemError: <method-wrapper '__getattribute__' of EnumMeta object sciloop 4 1,511 Jul-23-2022, 07:57 PM
Last Post: sciloop
  Using C++/CLI wrapper functions by Python Ales1000 2 1,775 Jun-21-2022, 04:50 AM
Last Post: Ales1000
  Issue while using ctypes in python GiggsB 6 2,783 Mar-27-2022, 03:38 AM
Last Post: GiggsB
  Python executable Script (Wrapper) Hwang 2 1,893 Jan-12-2022, 06:53 PM
Last Post: Hwang
  Sqla wrapper Slojure 1 1,537 Nov-01-2021, 04:36 PM
Last Post: Larz60+
  Ctypes and libffi.so.7 luxedo 1 6,057 Oct-23-2021, 09:24 PM
Last Post: DeaD_EyE
  Python C++ wrapper problem JESuh 1 2,169 Mar-30-2021, 04:37 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020