I want to integrate python code with C++, in which c++ source code call a function/class in python. I specifically need to pass array from c++ code to python code and get another array from python in c++ source code. To illustarte,
the main.cc
// call_function.c - A sample of calling
// python functions from C code
//
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
#include <Python.h>
#include <iostream>
#include "numpy/arrayobject.h"
int main(int argc, char *argv[])
{
PyObject *pName, *pModule, *pDict, *pFunc, *pValue, *pRet;
const int SIZE{ 10 };
npy_intp dims = SIZE;
const int ND{ 1 };
double* a = new double[10]();
double* b = new double[10]();
if (a == NULL)
{
exit(1);
}
std::cout << a[1] << std::endl;
// Initialize the Python Interpreter
Py_Initialize();
import_array();
PyRun_SimpleString("import sys\n");
PyRun_SimpleString("sys.path.append(\"/home/mehdi\")");
// Build the name object
pName = PyUnicode_FromString("pytst");
if (pName == NULL) exit(1);
// Load the module object
pModule = PyImport_Import(pName);
if (pModule == NULL) exit(1);
Py_DECREF(pName);
if (!pModule){
std::cout << "mymodule can not be imported" << std::endl;
return 1;
}
// pDict is a borrowed reference
pDict = PyModule_GetDict(pModule);
if (pDict == NULL) exit(1);
PyObject *pArraya = PyArray_SimpleNewFromData(ND, &dims, NPY_DOUBLE,
reinterpret_cast<void*>(a));
import_array();
PyArrayObject *np_arra = reinterpret_cast<PyArrayObject*>(pArraya);
PyObject *pArrayb = PyArray_SimpleNewFromData(
ND, &dims, NPY_LONGDOUBLE, reinterpret_cast<void*>(b));
PyArrayObject *np_arrb = reinterpret_cast<PyArrayObject*>(pArrayb);
if (!pArraya || !pArrayb)
std::cout << "Error" << std::endl;
// pFunc is also a borrowed reference
pFunc = PyDict_GetItemString(pDict, "multiply");
if (PyCallable_Check(pFunc))
{
pRet = PyObject_CallObject(pFunc, pArraya);
} else
{
PyErr_Print();
}
if (!pRet)
std::cout << "error" << std::endl;
if (!PyArray_Check(pRet)) {
std::cerr << " did not return an array." << std::endl;
}
if (pRet == NULL) exit(1);
std::cout << 9 << std::endl;
PyArrayObject *np_ret = reinterpret_cast<PyArrayObject*>(pRet);
double* c_out;
//int len = PyArray_SHAPE(np_ret)[0];
std::cout << 10 << std::endl;
c_out = reinterpret_cast<double*>(PyArray_DATA(np_ret));
// Clean up
Py_DECREF(pModule);
Py_DECREF(pName);
// Finish the Python Interpreter
Py_Finalize();
return 0;
}
}
pytst.py
import numpy as np
def multiply(a, b):
c = np.array([])
for i in range(10):
c = np.append(c, a[i] + b[i])
return c
and to compile it I use
g++ -Wall main.cc -o main -I/usr/include/python3.8 -I /usr/local/lib/python3.5/dist-packages/numpy/core/include.
I converted double* to PyObject* to pass from python to c++. but I get NULL for pRet (return array from python code to c++). I'd really appreciate if anyone let me know how this communication between c++ and python code take place.