Py_BuildValue or PyList_SetItem()

S

Sheldon

Hi,

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);
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>


Any help is appreciated!

/Sheldon
 
J

Jack Diederich

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);
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
 
S

Sheldon

Jack Diederich skrev:
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);
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


Thanks Jack,

It works now!

/Sheldon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top