#include "Python.h"#include <stdio.h>
double call_func(PyObject *self, PyObject *args) { printf("call_func\n'"); const int *addr; double res;
printf("call_func: converting to address\n"); if (!PyArg_ParseTuple(args, "i", &addr)) { printf("Could not parse integer value in call_func.\n"); return -1; }
printf("call_func: converting address to func pointer\n'"); //double (*func)(double, double) = addr; double (__cdecl *func)(double, double) = addr;
printf("call_func: calling function\n"); res = (double)func(1.0, 2.0);
return res;}
static PyMethodDef RunCFuncMethods[] = { {"call", call_func, METH_VARARGS, "Execute function."}, {NULL, NULL, 0, NULL} /* Sentinel */};
PyMODINIT_FUNCinit_run_cfunc(void){ printf("Initializing module\n"); PyObject *m; m = Py_InitModule("_run_cfunc", RunCFuncMethods); if(m == NULL) { printf("Could not initialize module!\n"); }}
import numba
cfunc = numba.cfunc
def add(p1, p2): return p1 + p2
add_nb = cfunc("float64(float64, float64)", nopython=True, cache=True)(add)
add_nb.ctypes(2, 3)print(add_nb.address)
import _run_cfunc
_run_cfunc.call(add_nb.address)
#include "Python.h"
#include <stdio.h>
typedef double (*func_t)(double, double);
static PyObject * call_func(PyObject *self, PyObject *args)
{
printf("call_func\n'");
const Py_ssize_t *addr;
double res;
func_t func;
PyObject *ret;
printf("call_func: converting to address\n");
if (!PyArg_ParseTuple(args, "n", &addr))
{
printf("Could not parse integer value in call_func.\n");
return NULL;
}
printf("call_func: converting address to func pointer\n'");
func = (func_t)addr;
printf("call_func: calling function\n");
res = func(1.0, 2.0);
printf("call_func: %f\n", res);
/* return the result */
ret = PyFloat_FromDouble(res);
if(PyErr_Occurred()) return NULL;
return ret;
}
static PyMethodDef RunCFuncMethods[] =
{
{
"call", call_func, METH_VARARGS,
"Execute function."
},
{NULL, NULL, 0, NULL} /* Sentinel */
};
PyMODINIT_FUNC
init_run_cfunc(void)
{
printf("Initializing module\n");
PyObject *m;
m = Py_InitModule("_run_cfunc", RunCFuncMethods);
if(m == NULL)
{
printf("Could not initialize module!\n");
}
}