1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
|
#include <stdio.h>
#include <Python/Python.h>
static int
_shift_function(int *output_coordinates, double* input_coordinates,
int output_rank, int input_rank, void *callback_data)
{
int ii;
/* get the shift from the callback data pointer: */
double shift = *(double*)callback_data;
/* calculate the coordinates: */
for(ii = 0; ii < input_rank; ii++){
input_coordinates[ii] = output_coordinates[ii] - shift;
}
/* return OK status: */
return 1;
}
static void
_destructor(void* cobject, void *cdata)
{
if (cdata)
free(cdata);
}
static PyObject *
py_shift_function(PyObject *obj, PyObject *args)
{
double shift = 0.0;
if (!PyArg_ParseTuple(args, "d", &shift)) {
PyErr_SetString(PyExc_RuntimeError, "invalid parameters");
return NULL;
}
else {
/* assign the shift to a dynamically allocated location: */
double *cdata = (double*)malloc(sizeof(double));
*cdata = shift;
/* wrap function and callback_data in a CObject: */
return PyCObject_FromVoidPtrAndDesc(_shift_function, cdata,
_destructor);
}
}
static PyMethodDef methods[] = {
{"shift_function", (PyCFunction)py_shift_function, METH_VARARGS, ""},
{NULL, NULL, 0, NULL}
};
void initexample(void) {
Py_InitModule("example", methods);
} |
Partager