How does a C API-function returns a list to python

"Martin v. Löwis" martin at v.loewis.de
Sun Jan 12 16:29:52 EST 2003


torsten.maass at imms.de wrote:
> I want to write a communication-functions set for a special
> hardware component for python using C API.
> 
> Does anybody know if and how it is possible to return a list 
> to python from a read C-Array.

 > double a[5]={1.0,2.0,3.0,4.0,5.0};

Try to avoid Py_BuildValue if you can. Instead, I recommend something like

   len = sizeof(a)/sizeof(a[0]);
   result = PyList_New(len);
   if (!result)return NULL;
   for (i=0; i<len; i++){
     PyObject *item = PyFloat_FromDouble(a[i]);
     if(!item){
       Py_DECREF(result);
       return NULL;
     }
     PyList_SET_ITEM(result, i, item);
   }
   return result;

Regards,
Martin





More information about the Python-list mailing list