Question

I have a real simple Python function:

def myfunc(x): 
    return 2.0 * x 


I want to send this function to a C++ program and call it so I have done this:

#include "Python.h"
static PyObject *execMyPyFunc(PyObject *self, PyObject *args) {
    PyObject *Fx, *pyresult;
    double x;
    PyArg_ParseTuple(args, "dO", &x, &Fx);
    pyresult = PyObject_CallFunction(Fx, "d", x);
    return pyresult;
}
static PyMethodDef C_API_TestMethods[] = {
    {"execMyPyFunc", execMyPyFunc, METH_VARARGS, "Add documentation here.."},
    {NULL, NULL}
};
PyMODINIT_FUNC initC_API_Test(void) {
    Py_InitModule("C_API_Test", C_API_TestMethods);
}


My Python program works correctly:

from C_API_Test import execMyPyFunc
def myfunc(x): 
    return 2.0 * x
fx = execMyPyFunc(1.28,myfunc)
print fx


What I would like to do though is to somehow get the pointer from my Python function (PyObject *Fx) and pass this to a C++ function expecting: double(*fx)(double). Does anyone know how to do this (if possible)? I tried to initialize double(*cFx)(double) and cast my Python function as cFx = (double(*)(double))Fx but this does not work. Any ideas?

Was it helpful?

Solution

You aren't going to be able to simply cast a Python function to C like that.

Instead pass the PyObject function pointer, call the function, and convert to C double. This code will return -1 on failure.

static double cFx(PyObject *fx, double x){
    PyObject *pyresult = PyObject_CallFunction(fx, "d", x);
    if (pyresult == NULL) return -1;
    double cppresult = PyFloat_AsDouble(pyresult);
    Py_DECREF(pyresult);
    if (PyErr_Occurred()) return -1;
    return cppresult;
}

Important part of this is to decrement the reference count to the return value of PyObject_CallFunction since you aren't passing it off to the Python interpreter to deal with.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top