h2pxd

1,064 views
Skip to first unread message

Evan Buswell

unread,
Jul 10, 2011, 3:20:30 AM7/10/11
to cython...@googlegroups.com
Hi all,

I've been working on creating a python wrapper around a C library
using Cython. In the process of doing this, I solved a more general
problem and created a script that converts any C header to a pxd
definition file. This is attached here, in the hopes that it may be
useful to someone else as well. I suppose I could put it in github or
something, but that just seems silly for a single file. Perhaps the
Cython maintainers will see fit to pull it into the main distribution.

The script, of course, is not very magic. It converts whatever C
definitions it sees, but it doesn't really know anything about
conditional compilation (though you can instruct it to ignore
#if(n)def blocks based on a given definition), it ignores macro
functions, and it assumes that all instances of "#define x y" define a
constant. You can configure quite a lot of the behavior, via
commandline or configuration file, e.g. to make all structs opaque, or
to skip headers or add other header files. After tweaking the
configuration, I was able to use most of my header files as-is, though
I had to hand-code a few stubs. For more complicated headers, you
might have to tweak the output manually, but you'll probably still
save time over writing it yourself.

Cheers,

Evan

h2pxd

Matěj Týč

unread,
Apr 4, 2012, 3:14:04 PM4/4/12
to cython...@googlegroups.com

On Sunday, July 10, 2011 9:20:30 AM UTC+2, Evan Buswell wrote:
Hi all,

I've been working on creating a python wrapper around a C library
using Cython.  In the process of doing this, I solved a more general
problem and created a script that converts any C header to a pxd
definition file.  This is attached here, in the hopes that it may be
useful to someone else as well.  I suppose I could put it in github or
something, but that just seems silly for a single file.  Perhaps the
Cython maintainers will see fit to pull it into the main distribution.


This is a really good thing and I am convinced that Cython users would benefit  from this tool if it was a part of Cython.
It probably needs some further tweaking, I have tried to use it on a "simple" library header and it didn't work so well, but it may be due to my lack of knowledge, too.
Are you still willing to spend some time with this tool?
Matej

Robert Bradshaw

unread,
Apr 4, 2012, 3:26:23 PM4/4/12
to cython...@googlegroups.com

I agree that this would be very useful. There's a potential GSoC
project to do this leveraging GCC's parser too.

- Robert

Reply all
Reply to author
Forward
0 new messages