extending with C
Pete Shinners
pete at shinners.org
Wed Jan 9 11:22:10 EST 2002
Jesper Olsen wrote:
> I need to write a python extension in C, and the method interface should
> be able to handle "arrays of float".
>
> In the python code, the array could be represented as a tupel, a list or
> an array it is not really important.
>
> The extension needs to be able to receive such arrays, and to return them
> to python.
here's a quick dirty sample, reading a set of floats from inside a tuple...
PyObject* do_floats(PyObject* self, PyObject* args)
{
PyObject* tuple;
float* floatlist;
int size, loop;
if(!PyArg_ParseTuple("O", &tuple))
return NULL;
if(!PyTuple_Check(tuple))
{
PyErr_SetString(PyExc_TypeError, "need tuple");
return NULL;
}
size = PyTuple_Size(tuple);
floatlist = PyMem_New(float, size);
if(!floatlist)
return NULL;
for(loop = 0; loop<size; ++loop)
floatlist[loop] = PyTuple_GET_ITEM(tuple, loop);
/* DO SOMETHING WITH THE FLOATLIST HERE */
PyMem_Del(floatlist);
Py_INCREF(Py_None);
return Py_None;
}
of course, my email composer probably parses this better than a real c
compiler, so you might find some typos in there. :]
More information about the Python-list
mailing list