return tuple from C to python (extending python)

K

Kiran

Hi all, I want to make python call some C functions, process them and
return them.

Ok, I got all the wrapper functions and everything setup right. Here
is my problem. What I need to do is to return a tuple from C to
python.

To go about doing this, I first create a tuple object in C doing the
following:

PyObject* toRet;
toRet = PyTuple_New(num_addr);

then, in a for loop, i assign values to the tuple as follows:

for ( i = 0; i < num_addr; i++ )
{
printf("%d\n", dat);
PyTuple_SET_ITEM(toRet, i, (PyObject*)dat );
}
(dat is declared as follows: unsigned int* dat; )

then, i say return toRet at the end of my C function.

when I try to print the tuple in python, it says the memory address
0x.... could not be written, and I can see a part of the tuple
printout, which is as follows:
( <nil>,
then i get my error.

can someone please help me with this?

thanks a lot!
-- Kiran
 
S

Simon Forman

Kiran said:
Hi all, I want to make python call some C functions, process them and
return them.

Ok, I got all the wrapper functions and everything setup right. Here
is my problem. What I need to do is to return a tuple from C to
python.

To go about doing this, I first create a tuple object in C doing the
following:

PyObject* toRet;
toRet = PyTuple_New(num_addr);

then, in a for loop, i assign values to the tuple as follows:

for ( i = 0; i < num_addr; i++ )
{
printf("%d\n", dat);
PyTuple_SET_ITEM(toRet, i, (PyObject*)dat );
}
(dat is declared as follows: unsigned int* dat; )

then, i say return toRet at the end of my C function.

when I try to print the tuple in python, it says the memory address
0x.... could not be written, and I can see a part of the tuple
printout, which is as follows:
( <nil>,
then i get my error.

can someone please help me with this?

thanks a lot!
-- Kiran


I have not done a great deal of extension work with python, however, I
do not believe you can simply cast an int (or pointer to int, which is
what you say dat is declared as, unless my C is /really/ rusty) to
PyObject*.

I think you need to do something like Py_BuildValue("i", 123), but see
http://docs.python.org/ext/buildValue.html for more info.

Peace,
~Simon
 
F

Farshid Lashkari

Simon said:
I have not done a great deal of extension work with python, however, I
do not believe you can simply cast an int (or pointer to int, which is
what you say dat is declared as, unless my C is /really/ rusty) to
PyObject*.

I think you need to do something like Py_BuildValue("i", 123), but see
http://docs.python.org/ext/buildValue.html for more info.

Simon is correct. You need to create a python object from your unsigned
int. Try the following instead:

PyTuple_SET_ITEM(toRet, i, PyInt_FromLong(dat) );

-Farshid
 
K

Kiran

Farshid said:
Simon said:
I have not done a great deal of extension work with python, however, I
do not believe you can simply cast an int (or pointer to int, which is
what you say dat is declared as, unless my C is /really/ rusty) to
PyObject*.

I think you need to do something like Py_BuildValue("i", 123), but see
http://docs.python.org/ext/buildValue.html for more info.

Simon is correct. You need to create a python object from your unsigned
int. Try the following instead:

PyTuple_SET_ITEM(toRet, i, PyInt_FromLong(dat) );

-Farshid


That did the trick. thanks guys both for your help!
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top