return tuple from C to python (extending python)

Discussion in 'Python' started by Kiran, Oct 24, 2006.

  1. Kiran

    Kiran Guest

    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
     
    Kiran, Oct 24, 2006
    #1
    1. Advertising

  2. Kiran

    Simon Forman Guest

    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);
    > 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
     
    Simon Forman, Oct 25, 2006
    #2
    1. Advertising

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

    -Farshid
     
    Farshid Lashkari, Oct 25, 2006
    #3
  4. Kiran

    Chetan Guest

    "Kiran" <> 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);
    > 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.
    >

    You could use PyInt_FromXXXX functions to convert dat to PyInt
     
    Chetan, Oct 25, 2006
    #4
  5. 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/>
     
    Andrew Poelstra, Oct 25, 2006
    #5
  6. Kiran

    Kiran Guest

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


    That did the trick. thanks guys both for your help!
     
    Kiran, Oct 26, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michal Mikolajczyk
    Replies:
    1
    Views:
    812
    Larry Bates
    Apr 20, 2004
  2. Jeff Epler
    Replies:
    0
    Views:
    960
    Jeff Epler
    Apr 20, 2004
  3. Bill Scherer
    Replies:
    0
    Views:
    615
    Bill Scherer
    Apr 20, 2004
  4. Gregor Horvath

    Why tuple with one item is no tuple

    Gregor Horvath, Mar 15, 2005, in forum: Python
    Replies:
    37
    Views:
    824
    Antoon Pardon
    Mar 30, 2005
  5. Kiran
    Replies:
    3
    Views:
    644
    Kiran
    Oct 26, 2006
Loading...

Share This Page