Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > return tuple from C to python (extending python)

Reply
Thread Tools

return tuple from C to python (extending python)

 
 
Kiran
Guest
Posts: n/a
 
      10-24-2006
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[i]);
PyTuple_SET_ITEM(toRet, i, (PyObject*)dat[i] );
}
(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

 
Reply With Quote
 
 
 
 
Simon Forman
Guest
Posts: n/a
 
      10-24-2006
Kiran wrote:
> 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[i]);
> PyTuple_SET_ITEM(toRet, i, (PyObject*)dat[i] );
> }
> (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

 
Reply With Quote
 
 
 
 
Farshid Lashkari
Guest
Posts: n/a
 
      10-25-2006
Simon Forman wrote:
> 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[i]) );

-Farshid
 
Reply With Quote
 
Chetan
Guest
Posts: n/a
 
      10-25-2006
"Kiran" <(E-Mail Removed)> writes:

> 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[i]);
> PyTuple_SET_ITEM(toRet, i, (PyObject*)dat[i] );
> }
> (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.
>

You could use PyInt_FromXXXX functions to convert dat[i] to PyInt
 
Reply With Quote
 
Andrew Poelstra
Guest
Posts: n/a
 
      10-25-2006
On Tue, 2006-10-24 at 15:24 -0700, Kiran wrote:
> Hi all, I want to make python call some C functions, process them and
> return them.


Try comp.lang.python
--
Andrew Poelstra <http://www.wpsoftware.net/projects/>

 
Reply With Quote
 
Kiran
Guest
Posts: n/a
 
      10-26-2006

Farshid Lashkari wrote:
> Simon Forman wrote:
> > 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[i]) );
>
> -Farshid


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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
*tuple vs tuple example print os.path.join(os.path.dirname(os.tmpnam()),*("a","b","c")) Steve Python 1 12-13-2005 10:25 PM
Why tuple with one item is no tuple Gregor Horvath Python 37 03-30-2005 06:58 AM
Easily convert unicode tuple to python string tuple??? Michal Mikolajczyk Python 1 04-20-2004 08:37 PM
Re: Easily convert unicode tuple to python string tuple??? Jeff Epler Python 0 04-20-2004 03:36 PM
Re: Easily convert unicode tuple to python string tuple??? Bill Scherer Python 0 04-20-2004 03:34 PM



Advertisments