problem with Py_BuildValue

Christian Meesters meesters at uni-mainz.de
Sun Jun 15 07:55:12 EDT 2008


Hi,

currently I have a problem understanding Py_BuildValue. I have this code:

static PyObject *function(PyObject *self, PyObject *args) {
   PyObject * python_return_value = NULL;
   PyObject * dummy = NULL;
   double * internal_list;
   <snip and forget the rest>
   
   /* converting to python representation */
   for (i=0; i < limit; i++) {
       dummy = Py_BuildValue("d", internal_list[i]);
          if (!dummy) return NULL;
          PyList_Append(python_return_value, dummy);
          Py_DECREF(dummy); dummy = NULL; 
        }
   return python_return_value
}

This doesn't work. What I see, when invoking the function "function()" in
Python is a list of refcounts, like: [<refcnt 0 at 0x94a29d4>, <refcnt 0 at
0x94a29e4>, ...]. However, if I change the Py_BuildValue-line to be
dummy = Py_BuildValue("i", (int)internal_list[i]);
I do get the 'right' integer return values. Point is that I really would
like to work with Python-floats afterwards.

Any idea where a pitfall might be here?

TIA
Christian

PS Oh, and I tried casting to float and explicitly to double, too. Same
result as without the casts.




More information about the Python-list mailing list