1

Below is CPP calling python file source code for a reference.

    int main(int argc, char *argv[])
{
PyObject *pName, *pModule, *pFunc;
PyObject *pArgs, *pValue;
int i;
string pythonFunction = "sample_fun";
string pythonFile     = "sample"
string tensoflow_env  = "tensorflow_env_path";
wchar *env = Py_DecodeLocal(tensoflow_env,NULL);
Py_SetPythonHome(env);
Py_Initialize();
pName = PyUnicode_DecodeFSDefault(pythonFile.c_str());
/* Error checking of pName left out */

pModule = PyImport_Import(pName);
Py_DECREF(pName);

if (pModule != NULL) {
pFunc = PyObject_GetAttrString(pModule, pythonFunction.c_str());
/* pFunc is a new reference */

if (pFunc && PyCallable_Check(pFunc)) {
    
    pValue = PyObject_CallObject(pFunc, NULL);
    if (pValue != NULL) {
        Py_DECREF(pValue);
    }
    else {
        Py_DECREF(pFunc);
        Py_DECREF(pModule);
        PyErr_Print();
        fprintf(stderr,"Call failed\n");
        return 1;
    }
}
else {
    if (PyErr_Occurred())
        PyErr_Print();
    fprintf(stderr, "Cannot find function \"%s\"\n", argv[2]);
}
Py_XDECREF(pFunc);
Py_DECREF(pModule);
}
else {
    PyErr_Print();
    fprintf(stderr, "Failed to load \"%s\"\n", argv[1]);
    return 1;
}
if (Py_FinalizeEx() < 0) {
return 120;
}
return 0;

}

Below is sample.py file for a reference.

def sample_fun():
   import tensorflow as tf
   return 'Success'

Problem :

Above code is working fine while running through tensorflow2.0 env but same code if I run through tensorflow2.2 env then it's give below exception at PYObject_CallObject(pFunc, NULL)

Error : Microsoft c++ exception : pybind11::error_already_set at memory location XXXXXX

Any body has idea regarding what is possible cause to above issue ? Note : Tensorflow2.2 build with python 3.7.6 and Tensorflow2.0 build with python 3.7.7

0 Answers0