Thursday, August 5, 2010

python: calling c++ functions from python

 

Why would you want to do this? To test CPP APIs, SDKs – by consuming API/SDK code in Python. If you already have an automation test suite in Python, you can write and maintain test cases easily in Python, calling C++ functions as required, instead of writing C++ code to consume C++ libraries – which can get painful since the effort and time to write/maintain C++ is much higher than Python in my experience.

There are at least three different ways to create a python binding to a CPP library:

 

According to this discussion on stackoverflow, ctypes has a few advantages (including simplicity) over the other options:

ctypes has the advantage that you don't need to satisfy any compile time dependency on python, and your binding will work on any python that has ctypes, not just the one it was compiled against.

No comments: