I am trying to use the Python/C API to run a Python function which returns a string. I want to store that returned string to a C++ variable, but I can't get it to work. I am using Python 3 and it seems like the PyString_FromString()
method doesn't work anymore. Here is my code:
int main(int argc, char *argv[])
{
PyObject *pName, *pModule, *pDict, *pFunc, *pValue;
Py_Initialize();
pName = PyUnicode_FromString("ocr");
pModule = PyImport_Import(pName);
pDict = PyModule_GetDict(pModule);
pFunc = PyDict_GetItemString(pDict, "get_text");
pValue = PyUnicode_FromString("ocr_noise.png");
pValue = PyObject_CallObject(pFunc, pValue);
std::string result = PyUnicode_FromObject(pValue);
Py_DECREF(pModule);
Py_DECREF(pName);
Py_DECREF(pValue);
Py_Finalize();
return 0;
}
The python file is called ocr.py
, and the function I am trying to call is called get_text(value)
. I am trying to pass in "ocr_noise.png"
as an argument. Any ideas what I should do?
EDIT 2: I don't need to use std::string as in the code. What else can I use to store the string returned from the function?