how to convert Python C API code to Boost::Python?

Viewed 83

i have to call a python function that like below form:

def f_function(arg1,arg2,...)
    #do something

i have a section code of Python C API as below to call above function in C++ application:

PyObject* f_function;//stand for above python function 
PyObject* args = NULL;

args= PyTuple_New(num_args);
for (Py_ssize_t i = 0; i < num_args; ++i)
{
//assign value by type
    if(TYPE_DOUBLE==type)
    {
        PyObject* arg = PyFloat_FromDouble(dVal);
        PyTuple_SetItem(args, i, arg);
    }
    else
    {
        // other type 
    }
}
// finally call the function
PyObject* pRet = PyObject_CallObject(f_function, args);
if (pRet)
{
    long result = PyLong_AsLong(pRet);
    cout << "result:" << -1;
}

my question is : i have a request to use boost::python to rewrite above process, my idea is :

boost::python::object f_function;//same as above python function
boost::python::tuple data;
for (Py_ssize_t i = 0; i < num_args; ++i)
{
   //but seems boost python tuple can't insert data as python C API 
}

but i don't know how to pass arguments to it , i use boost::python::tuple that simlar as C API above mentioned , but get errors, i tried many times , seems only below form calling is ok :

f_function(arg1,arg2,...)

i am really appreciated someone can help me to solve this problem .

0 Answers
Related