By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,100 Members | 2,846 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,100 IT Pros & Developers. It's quick & easy.

Py_BuildValue or PyList_SetItem()

P: n/a
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[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>
Any help is appreciated!

/Sheldon

Jan 3 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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
Jan 3 '07 #2

P: n/a

Jack Diederich skrev:
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
Thanks Jack,

It works now!

/Sheldon

Jan 3 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.