0

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?

2
  • 2
    Did you look at Boost.Python ? Commented Aug 26, 2013 at 17:26
  • Yes, but how would one do this without Boost, SWIG, etc. Commented Aug 26, 2013 at 17:30

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.