Thread: [pygccxml-development] ctypes code generation?
Brought to you by:
mbaas,
roman_yakovenko
From: Gordon K. <gk...@bw...> - 2008-10-13 15:02:22
|
Hello, I'm new to pygccxml and have a simple question: does anyone have suggestions on how to use pygccxml to generate python code corresponding to the structs, functions, and variables that are available (from a shared library) in ctypes? I'm thinking of something that takes a C header like: typedef struct { int aaaa, bbbb; } tst; extern int tstSet(tst *t, int v); and generates python code which includes things like: class tst(Structure): pass tst._fields_ = [ ('aaaa', c_int), ('bbbb', c_int), ] tstSet.restype = c_int tstSet.argtypes = [POINTER(tst), c_int] I know that ctypeslib serves this purpose: http://starship.python.net/crew/theller/wiki/CodeGenerator (and my example above is based on its output) but I've had problems using it with the current version of gccxml: its xml2py phase doesn't pick up the fields in C structs and convert them to a "_fields_" assignment. The author of ctypeslib has warned me that ctypeslib is not really maintained anymore, and he pointed me to a python-only C- parser (pyglet/tools/wraptypes/wrap.py) included in pyglet <http:// www.pyglet.org/>, but that choked on some of my header files, and pyglet doesn't really seem to be the right tool for this job. pygccxml, however, seems theoretically perfect, since it works directly from gccxml, right? There are some ctypes-related comments that show up in the pygccxml-commit mailing list archive, but nothing (I could find, anyway) about ctypes in the documentation, or on this list. I'd very much appreciate any tips, suggestions, or examples that might help me. Thanks in advance, Gordon |
From: Roman Y. <rom...@gm...> - 2008-10-13 20:39:53
|
On Mon, Oct 13, 2008 at 4:46 PM, Gordon Kindlmann <gk...@bw...> wrote: > Hello, > > I'm new to pygccxml and have a simple question: does anyone have > suggestions on how to use pygccxml to generate python code > corresponding to the structs, functions, and variables that are > available (from a shared library) in ctypes? > > I'm thinking of something that takes a C header like: > > typedef struct { > int aaaa, bbbb; > } tst; > > extern int tstSet(tst *t, int v); > > and generates python code which includes things like: > > class tst(Structure): > pass > tst._fields_ = [ > ('aaaa', c_int), > ('bbbb', c_int), > ] > > tstSet.restype = c_int > tstSet.argtypes = [POINTER(tst), c_int] > > I know that ctypeslib serves this purpose: > > http://starship.python.net/crew/theller/wiki/CodeGenerator > > (and my example above is based on its output) but I've had problems > using it with the current version of gccxml: its xml2py phase doesn't > pick up the fields in C structs and convert them to a "_fields_" > assignment. The author of ctypeslib has warned me that ctypeslib is > not really maintained anymore, and he pointed me to a python-only C- > parser (pyglet/tools/wraptypes/wrap.py) included in pyglet <http:// > www.pyglet.org/>, but that choked on some of my header files, and > pyglet doesn't really seem to be the right tool for this job. > > pygccxml, however, seems theoretically perfect, since it works > directly from gccxml, right? There are some ctypes-related comments > that show up in the pygccxml-commit mailing list archive, but nothing > (I could find, anyway) about ctypes in the documentation, or on this > list. > > I'd very much appreciate any tips, suggestions, or examples that > might help me. I don't think that I can help you right now, may be in future. I am developing Py++, a code generator for Boost.Python library, and recently integrated some functionality, which allows to export some "C" code easily. There are few other solutions that help to export and create Pythonic interface for "C" classes and functions. For example take a look on functions transformation future ( http://language-binding.net/pyplusplus/documentation/functions/transformation/transformation.html ). Do you want to reconsider your approach :-) ? May be the best solution for you is to add missing functionality to ctypeslib? It is not too difficult after all. I can help you with gccxml. I did not know that ctypes code generator is not maintained. I think, I am going to evaluate and may be to support it. If you want we can join the efforts. -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ |
From: Gordon K. <gk...@bw...> - 2008-10-15 16:46:03
|
hello, On Oct 13, 2008, at 4:39 PM, Roman Yakovenko wrote: > I am developing Py++, a code generator for Boost.Python library, and > recently integrated some functionality, which allows to export some > "C" code easily. ... I noticed that your announcement of the next version also seemed to mention something about this. Would it be possible to point me to an existing example of doing this? Or any hints about how to do it myself? > There are few other solutions that help to export and > create Pythonic interface for "C" classes and functions. For example > take a look on functions transformation future ( > http://language-binding.net/pyplusplus/documentation/functions/ > transformation/transformation.html > ). Do you want to reconsider your approach :-) ? I'm relatively new to python, so I have to admit I don't grasp the significance of what this page describes. Again, some simple examples of wrapping C code would really help me understand how I could use your tools. > May be the best solution for you is to add missing functionality to > ctypeslib? It is not too difficult after all. I can help you with > gccxml. > > I did not know that ctypes code generator is not maintained. I think, > I am going to evaluate and may be to support it. If you want we can > join the efforts. Well, in the mean time I have in fact found a way to tweak the xml2py script of ctypeslib, and I've announced the patch on the ctypes-users mailing list, which I can forward to you if interested. So ctypeslib is working again for me, with gcc 4.0.1 and gccxml 0.9.0, but there are probably still some small problems. I do think it would be very good if ctypeslib is still maintained by someone. To me it seems like a tool that is very good at its one thing, and its a shame when those tools stop working. Alternatively, from what I understand, it seems like the pygccxml output could also be parsed to generate the same ctypes code that ctypeslib generates- that is, the brains of ctypeslib's xml2py script could operate on the pygccxml parse tree instead of the XML file directly. But I barely know what I'm talking about! thanks, Gordon |
From: Roman Y. <rom...@gm...> - 2008-10-15 18:59:10
|
On Wed, Oct 15, 2008 at 6:43 PM, Gordon Kindlmann <gk...@bw...> wrote: > Would it be possible to point me to an existing example of doing this? Or > any hints about how to do it myself? I am not sure you need this, but if you ask: http://pygccxml.svn.sourceforge.net/viewvc/pygccxml/pyplusplus_dev/docs/documentation/ctypes/ The link contains few documents, which describes the idea and implementation behind "Py++ & ctypes integration". > I'm relatively new to python, so I have to admit I don't grasp the > significance of what this page describes. Again, some simple examples of > wrapping C code would really help me understand how I could use your tools. If you are wrapping pure C code, I suggest you to stay with ctypeslib only. The overhead is too big. >> May be the best solution for you is to add missing functionality to >> ctypeslib? It is not too difficult after all. I can help you with >> gccxml. >>... > Well, in the mean time I have in fact found a way to tweak the xml2py script > of ctypeslib, and I've announced the patch on the ctypes-users mailing list, > which I can forward to you if interested. So ctypeslib is working again for > me, with gcc 4.0.1 and gccxml 0.9.0, but there are probably still some small > problems. > > I do think it would be very good if ctypeslib is still maintained by > someone. To me it seems like a tool that is very good at its one thing, and > its a shame when those tools stop working. Alternatively, from what I > understand, it seems like the pygccxml output could also be parsed to > generate the same ctypes code that ctypeslib generates- that is, the brains > of ctypeslib's xml2py script could operate on the pygccxml parse tree > instead of the XML file directly. But I barely know what I'm talking about! This is exactly my plan - to port current code generator to deal with pygccxml and not with xml directly. -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ |
From: Gordon K. <gk...@bw...> - 2008-10-16 15:54:51
|
hello- Okay, thanks for these links and ideas. I encourage you to work on a pygccxml-to-ctypes-wrapper-code-generator in all your free time :) Gordon On Oct 15, 2008, at 2:58 PM, Roman Yakovenko wrote: > On Wed, Oct 15, 2008 at 6:43 PM, Gordon Kindlmann > <gk...@bw...> wrote: >> Would it be possible to point me to an existing example of doing >> this? Or >> any hints about how to do it myself? > > I am not sure you need this, but if you ask: > http://pygccxml.svn.sourceforge.net/viewvc/pygccxml/pyplusplus_dev/ > docs/documentation/ctypes/ > > The link contains few documents, which describes the idea and > implementation behind "Py++ & ctypes integration". > >> I'm relatively new to python, so I have to admit I don't grasp the >> significance of what this page describes. Again, some simple >> examples of >> wrapping C code would really help me understand how I could use >> your tools. > > If you are wrapping pure C code, I suggest you to stay with ctypeslib > only. The overhead is too big. > >>> May be the best solution for you is to add missing functionality to >>> ctypeslib? It is not too difficult after all. I can help you with >>> gccxml. >>> ... >> Well, in the mean time I have in fact found a way to tweak the >> xml2py script >> of ctypeslib, and I've announced the patch on the ctypes-users >> mailing list, >> which I can forward to you if interested. So ctypeslib is working >> again for >> me, with gcc 4.0.1 and gccxml 0.9.0, but there are probably still >> some small >> problems. >> >> I do think it would be very good if ctypeslib is still maintained by >> someone. To me it seems like a tool that is very good at its one >> thing, and >> its a shame when those tools stop working. Alternatively, from >> what I >> understand, it seems like the pygccxml output could also be parsed to >> generate the same ctypes code that ctypeslib generates- that is, >> the brains >> of ctypeslib's xml2py script could operate on the pygccxml parse tree >> instead of the XML file directly. But I barely know what I'm >> talking about! > > This is exactly my plan - to port current code generator to deal with > pygccxml and not with xml directly. > > -- > Roman Yakovenko > C++ Python language binding > http://www.language-binding.net/ |