1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| #include <Python.h>
#include <iostream>
#include <stdlib.h>
using namespace std;
string fctPython(char *pScript, char *pFunct, PyObject *pArgs);
int main(int argc, char *argv[])
{
PyObject *pArgs1, *pArgs2, *pValue1_1, *pValue1_2;
char *module = "myTest", *fct1 = "functString";
Py_Initialize();
PyRun_SimpleString("import sys; sys.path.append('.')");
/*Essai de functString*/
pArgs1 = PyTuple_New(2);
pValue1_1 = PyBytes_FromFormat("%s","functString : ceci est l'essai n°");
PyTuple_SetItem(pArgs1, 0, pValue1_1);
pValue1_2 = PyBytes_FromFormat("%i",1);
PyTuple_SetItem(pArgs1, 1, pValue1_2);
string resultat1 = fctPython(module, fct1, pArgs1);
cout << resultat1 << endl;
Py_Finalize();
return 0;
}
string fctPython(char *pScript, char *pFunct, PyObject *pArgs)
{
PyObject *pName, *pModule, *pFunc, *pValue;
pName = PyUnicode_FromString(pScript);
pModule = PyImport_Import(pName);
pFunc = PyObject_GetAttrString(pModule, pFunct);
pValue = PyObject_CallObject(pFunc, pArgs);
return PyBytes_AsString(pValue);
} |
Partager