int PyCall(
const
char * module,
const
char *
function
,
const
char *format, ... )
{
PyObject* pMod = NULL;
PyObject* pFunc = NULL;
PyObject* pParm = NULL;
PyObject* pRetVal = NULL;
if
( !(pMod = PyImport_ImportModule(module) ) ){
return
-1;
}
if
( !(pFunc = PyObject_GetAttrString(pMod,
function
) ) ){
return
-2;
}
va_list vargs;
va_start( vargs, format );
pParm = Py_VaBuildValue( format, vargs );
va_end(vargs);
pRetVal = PyEval_CallObject( pFunc, pParm);
int ret;
PyArg_Parse( pRetVal,
"i"
, &ret );
return
ret;
}
int main(int argc, char* argv[])
{
Py_Initialize();
printf(
"ret = %d\n"
, PyCall(
"pytest"
,
"fun"
,
"()"
);
printf(
"ret = %d\n"
, PyCall(
"pytest"
,
"fun1"
,
"(i)"
, 12 );
printf(
"ret = %d\n"
, PyCall(
"pytest"
,
"fun2"
,
"(is)"
, 12,
"12"
);
Py_Finalize();
return
0;
}