Wrapping a C library in Python

Roy Smith roy at panix.com
Thu Nov 18 20:10:35 EST 2004


I've got a C library with about 50 calls in it that I want to wrap in 
Python.  I know I could use some tool like SWIG, but that will give me a 
too-literal translation; I want to make some modifications along the way 
to make the interface more Pythonic.

For example, all of these functions return an error code (typically just 
errno passed along, but not always).  They all accept as one of their 
arguments a pointer to someplace to store their result.  I want to 
change all that to returning the result directly and throwing exceptions.

I also want to mutate some of the return types.  A common way these 
functions return a set of values is a pair of arrays of strings, forming 
key-value pairs.  In Python, it would make sense to return this as a 
dictionary.

I know what I'm describing is kind of vague, but are there tools around 
which might help automate (at least in part) this translation process?



More information about the Python-list mailing list