Maintainability of a python wrapping of a C library
Asked Answered
W

2

7

I have a poorly designed and big (> 300 public functions, >200 numeric constants defined with #define in the header file) that I have to wrap in Python. I have the dll and the h file. The library is updated yearly, till now in a backwards compatible way (i.e. just functions were added, a constant keep their numerical values, etc). But I have no guarantees as I do not control the library.

Using ctypes, I see two ways of wrapping this in Python:

  1. Mapping every constant and function to python, 1 to 1
  2. Redefining the API in Python and making calls to the library.

The first can be done in a (roughly) automatic way from the header file and therefore is easier to maintain and upgrade, the second requires a lot of python code but it will be easier to use.

I would appreciate some opinions based on your experience with this type of problem and some examples.

Wastebasket answered 7/10, 2012 at 15:21 Comment(2)
Sorry but as good as this is, Its not really a clear question.Bangtail
SIP may be an interesting solution.Heimer
I
4

I recently used ctypesgen to create a ctypes wrapping for SDL, and complementary libraries (SDL_image, SDL_ttf, SDL_mixer).

For me, it worked fairly well. It generates Python 2.x, but I was able to get the desired 3.x code by using the "2to3" utility.

I think it's a good idea to use the ctypes wrapping as a foundation for a more "pythonic" api, and that's basically what I did (on a very simple level) with my pslab module.

So, if you're looking to do something similar, that would be one way.

Intervocalic answered 8/10, 2012 at 12:48 Comment(0)
W
0

Maintaining a Python library with a ctypes backend isn't an unmanageable approach. Obviously the initial investment is larger than using automated tools, but the API you are left with should be much better.

If you do take that route, aim to separate the Python API entirely from the C library though. Supporting multiple ctypes backends with one Python front end api isn't too bad - just query at runtime and dynamically load the correct ctypes wrapper module. I've done that to wrap different dll files and .so files for windows and linux but it would work for versions of a library as well.

Weatherley answered 21/10, 2012 at 10:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.