One solution was found here, but it may not be the best:
%typemap(in) double[ANY] (double temp[$1_dim0]) { int i; if (!PySequence_Check($input)) { PyErr_SetString(PyExc_ValueError,"Expected a sequence"); return NULL; } if (PySequence_Length($input) != $1_dim0) { PyErr_SetString(PyExc_ValueError,"Size mismatch. Expected $1_dim0 elements"); return NULL; } for (i = 0; i < $1_dim0; i++) { PyObject *o = PySequence_GetItem($input,i); if (PyNumber_Check(o)) { temp[i] = (double) PyFloat_AsDouble(o); } else { PyErr_SetString(PyExc_ValueError,"Sequence elements must be numbers"); return NULL; } } $1 = temp; }
This is an example in the docs I finally got into that convert Python lists to arrays. The next part was more difficult, having collected some examples, I was able to convert the returned array to a python list:
%typemap(argout) double PointB[3]{ PyObject *o = PyList_New(3); int i; for(i=0; i<3; i++) { PyList_SetItem(o, i, PyFloat_FromDouble($1[i])); } $result = o; }
However, I must create one for each return value in the API. Also I have to call it with a dummy value as a parameter:
point_b = convertAtoB(s, point_a, dummy)
Is there a better way?
source share