Automatically Wrapping Libraries In Python (work in progress)

Introduction
I spend a lot of my time at work writing code in Python and using external libraries. ctypes is the built in method of accessing foreign functions and it works wonderfully. I’ve even used their C datatypes to emulate integer overflow for a platform specific checksum. However, there is no built in support for consuming an API. It’s possible to do it yourself, but some libraries are pretty complicated. You’re never actually done either, because one change upstream and your definitions could be off. The only sensible solution is to generate ctypes definitions of library functions automatically. The most promising method of doing this looks to be the excellent Py++ package.

Tools
LibRaw is the raw image processing library based on dcraw that I want to use from Python. Has a C++ and C interface. I’m going with the C++ interface because it’s easier to use.

VC9 is the C++ compiler I’m using on Windows, provided free with Visual C++ Express 2008. I used MinGW and it worked OK, but most libraries and programs (LibRaw and Python included) use MSVC, so I figured I might as well.

GCC-XML simulates a compiler and writes out an XML representation of the internal code structure inside the compiler. Once installed, I needed to run the included patcher to detect and support VC9.

pygccxml exposes the functionality of GCC-XML in Python and gets a list of exported functions from the binary library.

Py++ is the code generator that will write my ctypes definitions for everything found by pygccxml. To avoid manual intervention, there is a powerful set of queries available so I can exclude extraneous functions and resolve errors and warnings before building the code. If LibRaw updates, I simply have to run the script again and I have a new ctypes interface. Looks super interesting, but I haven’t played much with it yet.