Automatically generate pxd from C headers

470 views
Skip to first unread message

Thiago Arruda

unread,
Jun 1, 2016, 2:01:18 PM6/1/16
to cython-users
Hi

Just wanted to share a script I've written to generate pxd external declarations from C headers: https://github.com/tarruda/python-autopxd. It uses pycparser(https://github.com/eliben/pycparser) to parse C declarations and doesn't depend on native libraries(like libclang), all it requires is a C preprocessor to be available on $PATH. The module also exposes an API that returns a syntax tree for the converted cython declarations, so it can be modified as part of a build script before the final pxd is generated.

It is not very well tested yet, but the script is =~ 350 LOC so hopefully it is easily hackable and will attract contributions.

Robert Bradshaw

unread,
Jun 1, 2016, 11:48:23 PM6/1/16
to cython...@googlegroups.com
Cool! Want to add this to
https://github.com/cython/cython/wiki/AutoPxd ? Can it be run without
a C preprocessor if complicated macros and/or includes are not used?
> --
>
> ---
> You received this message because you are subscribed to the Google Groups
> "cython-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to cython-users...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Thiago Arruda

unread,
Jun 2, 2016, 9:08:08 AM6/2/16
to cython-users
Added

> Can it be run without a C preprocessor if complicated macros and/or includes are not used?

Pycparser requires types to be defined before declarations are properly parsed, so it will have to be a very simple header with only standard C types. While theoretically it could work, I'm not sure it would be very useful.

Note that the pypi package for autopxd includes a set of stub headers from standard C libraries(provided by pycparser itself) and these are used instead of the standard headers, so there's a good chance it will work even on complicated headers. I have successfully converted libuv headers, for example(though I didn't examine the result to see if it was correct)

What would be the use case for using autopxd without C preprocessor being available? There's a pure python implementation of cpp: https://github.com/dabeaz/ply/blob/master/ply/cpp.py , in theory it could be used as a fallback if a real cpp is not available on PATH.
Reply all
Reply to author
Forward
0 new messages