I want to know if there is any way to expose a C++ class to Python but without building an intermediate shared library.
Here is my desirable scenario. For example I have following C++ class:
class toto
{
public:
toto(int iValue1_, int iValue2_): iValue1(iValue1_), iValue2(iValue2_) {}
int Addition(void) const {if (!this) return 0; return iValue1 + iValue2;}
private:
int iValue1;
int iValue2;
};
I would like to convert somehow this class (or its intance) to a PyObject* in order to send it as paremter (args) to for example PyObject_CallObject:
PyObject* PyObject_CallObject(PyObject* wrapperFunction, PyObject* args)
In the other hand in my python side, I'll have a wrapperFunction which gets the pointer on my C++ class (or its instance) as parameter and it calls its methods or uses its properties:
def wrapper_function(cPlusPlusClass):
instance = cPlusPlusClass(4, 5)
result = instance.Addition()
As you can see, I don't really need/want to have a separate shared library or build a module by boost python. All that I need is to find a way to convert a C++ code to PyObject and send it to python. I cannot find a way to do that by C python libraries, boost or SWIG.
Do you have any idea? Thanks for your help.
I found my answer. Actually what I was searching was pretty similar to this answer (thanks moooeeeep for his comment):
Exposing a C++ class instance to a python embedded interpreter
Following C++ class (Attention! default constructor is mandatory):
could be exposed by boost by following macro:
In the other hand I have a python function defined in
python_script.py
which takes an instance of this class and do something. For example:Then in C++ side, I can call this function by sending at the same time the instance of my class, like this:
Advantages:
As far as I know, there is no easy way to accomplish this.
To extend Python with C++ with neither a module nor an intermediate library, it would require dynamically loading a library, then importing the functions. This approach is used by the
ctypes
module. To accomplish the same with C++, one would need to write actypes
-like library that understood the C++ ABI for the target compiler(s).To extend Python without introducing a module, an intermediate library could be created that provided a C API that wraps the C++ library. This intermediate library could then be used in Python through
ctypes
. While it does not provide the exact calling syntax and does introduce an intermediate library, it would likely be less effort than building actypes
-like library that could interface directly with C++.However, if an intermediate library is going to be introduced, it may be worthwhile to use Boost.Python, SWIG, or some other C++/Python language binding tool. While many of these tools will introduce the extension via a module, they often provide cleaner calling conventions, better error checking in the binding process, and may be easier to maintain.