On Wed, Jan 03, 2007 at 01:16:38PM -0800, Sheldon wrote: > I have a function that creates python objects out of C arrays and > returns them to Python. Below is a working example that I later want to > expand to return 12 arrays back to Python. The problem is that when I > print out the values in Python I get undesired reults. See below. Does > anyone know what is going on here? > The array values are good before the conversion. > > ************************************** > int createPythonObject(void) { > int i,j,k; > PyObject *Rva=PyList_New(12); > > for (i = 0; i < 12; i++) { > PyObject *op = PyFloat_FromDouble((double)va[i]); > if (PyList_SetItem(Rva,i,op) !=0) { > fprintf(stderr,"Error in creating python va object\n"); > exit(EXIT_FAILURE); > } > Py_DECREF(op); > op = 0; > return Py_BuildValue("N",Rva); > } > > Results in Python: > > <refcnt 0 at 0x80d885c>, <refcnt 0 at 0x80d885c>, <refcnt 0 at > 0x80d885c>, <refcnt 0 at 0x80d885c>, <refcnt 0 at 0x80d885c>, <refcnt 0 > at 0x80d885c>, <refcnt 0 at 0x80d885c>, <refcnt 0 at 0x80d885c>, > <refcnt 0 at 0x80d885c>, <refcnt 0 at 0x80d885c>, <refcnt 0 at > 0x80d885c>, <refcnt 0 at 0x80d885c> >
PyList_SetItem steals a reference to "op" so DECREF'ing it reduces the refcount to zero. http://docs.python.org/api/listObjects.html -Jack -- http://mail.python.org/mailman/listinfo/python-list