Run python in c ++

I have an application written in C ++ and a test system (also in C ++). The testing system is quite complex and difficult to change (I want to make only small changes). My class is as follows:

class Derived : public Base {
public:
    void somefunc(const AnotherClass& file) {
}
};

Inside there are several features. My testing system creates an instance of the Derived class and then uses its methods to perform some actions.

Now I want to be able to write a solution in Python. I need two-way integration. My idea is to write a Python function that will execute every time it is called somefunc. And I do not want to lose the values ​​of variables in Python from one function run to another. And I also want to be able to use the methods that are defined in the Base instance of the class from python. How can I achieve these goals?

I chose Boost.Python for these purposes. At the moment, I understand how to use a C ++ function or even a simple class in Python after some work. But I do not understand how to run a Python function from C ++.

The second question is: is Boost.Python a good choice? I need something very fast and at the same time easy to use.

Thank you for your help.

+4
1

Cython . . (: , ++, . ).


: , (++ → Python).

quacker.py:

def quack():
    print("Quack!")

cquacker.pyx:

from quacker import quack

cdef public void cquack():
    quack()

main.cpp:

#if _WIN32
#include <direct.h>
#define getcwd _getcwd
#define PATH_SEPARATOR ';'
#else
#include <unistd.h>
#define PATH_SEPARATOR ':'
#endif

#include <iostream>
#include <string>
#include <sstream>

#include <Python.h>
#include "cquacker.h"

std::wstring getSysPath()
{
  char cwd[FILENAME_MAX];
  getcwd(cwd, FILENAME_MAX);
  std::wstringstream path;
  path << Py_GetPath() << PATH_SEPARATOR << cwd;
  return path.str();
}

int main()
{
  Py_Initialize();
  PySys_SetPath(getSysPath().c_str());
  PyInit_cquacker();
  if (PyErr_Occurred())
  {
    PyErr_Print();
    return -1;
  }
  cquack();
  Py_Finalize();
  return 0;
}

: , (++ → Python → ++).

quacker.py:

def qcallback(duck):
    duck.quack()

quacker/Duck.hpp

#include <iostream>

namespace quacker {

class Duck
{
public:
    void quack() { std::cout << "Quack!" << "\n"; }
};

}

cquacker_defs.pxd:

cdef extern from "quacker/Duck.hpp" namespace "quacker":
    cdef cppclass Duck:
        Duck() except +
        void quack()

cquacker.pyx:

from cython.operator cimport dereference as deref
from libcpp.memory cimport shared_ptr

cimport cquacker_defs

from quacker import qcallback

cdef class Duck:
    cdef shared_ptr[cquacker_defs.Duck] _this

    @staticmethod
    cdef inline Duck _from_this(shared_ptr[cquacker_defs.Duck] _this):
        cdef Duck result = Duck.__new__(Duck)
        result._this = _this
        return result

    def __init__(self):
        self._this.reset(new cquacker_defs.Duck())

    def quack(self):
        assert self._this != NULL
        deref(self._this).quack()


cdef public void cqcallback(shared_ptr[cquacker_defs.Duck] duck):
    qcallback(Duck._from_this(duck))

main.cpp:

#if _WIN32
#include <direct.h>
#define getcwd _getcwd
#define PATH_SEPARATOR ';'
#else
#include <unistd.h>
#define PATH_SEPARATOR ':'
#endif

#include <iostream>
#include <memory>
#include <string>
#include <sstream>

#include "quacker/Duck.hpp"

#include <Python.h>
#include "cquacker.h"

std::wstring getSysPath()
{
  char cwd[FILENAME_MAX];
  getcwd(cwd, FILENAME_MAX);
  std::wstringstream path;
  path << Py_GetPath() << PATH_SEPARATOR << cwd;
  return path.str();
}

int main()
{
  Py_Initialize();
  PySys_SetPath(getSysPath().c_str());
  PyInit_cquacker();
  if (PyErr_Occurred())
  {
    PyErr_Print();
    return -1;
  }
  auto duck = std::make_shared<quacker::Duck>();
  cqcallback(duck);
  Py_Finalize();
  return 0;
}
+3

Source: https://habr.com/ru/post/1652281/


All Articles