Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to expose a C++ class to Python without building a module

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.

like image 459
Sacha Avatar asked Aug 26 '13 10:08

Sacha


People also ask

Can Python interact with C?

In general, already-written C code will require no modifications to be used by Python. The only work we need to do to integrate C code in Python is on Python's side. The steps for interfacing Python with C using Ctypes.

How do I use C++ DLL in Python?

To do this, uncomment the appropriate line at the top of the Python code. Run the Python Script. With the Python script open in the editor, press the F5 button to run it. This will save the Python code, hide the Python Editor window and then execute the Python code which will call the C++ DLL.

What is a Python binding?

Are you a Python developer with a C or C++ library you'd like to use from Python? If so, then Python bindings allow you to call functions and pass data from Python to C or C++, letting you take advantage of the strengths of both languages.


1 Answers

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 a ctypes-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 a ctypes-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.

like image 113
Tanner Sansbury Avatar answered Nov 15 '22 10:11

Tanner Sansbury