parsing a tuple in embedded python

Discussion in 'Python' started by jenkins.justin@gmail.com, Oct 30, 2005.

  1. Guest

    I am returning a tuple from my python method and am stuck trying to
    figure out how to read it into a C array using PyArg_Parse.
    My C Code:
    int array[3];
    PyArg_Parse(return, "(iii)", &array);

    My Python Code:
    mytuple = (1,2,3)
    return mytuple

    That gives me a segmentation fault. What am I doing wrong?
     
    , Oct 30, 2005
    #1
    1. Advertisements

  2. wrote:

    > I am returning a tuple from my python method and am stuck trying to
    > figure out how to read it into a C array using PyArg_Parse.
    > My C Code:
    > int array[3];
    > PyArg_Parse(return, "(iii)", &array);
    >
    > My Python Code:
    > mytuple = (1,2,3)
    > return mytuple
    >
    > That gives me a segmentation fault. What am I doing wrong?


    you're not providing enough arguments; "iii" means three pointers, not
    one. try:

    PyArg_Parse(return, "(iii)", array, array+1, array+2)

    instead. or, if you prefer maximum clarity:

    PyArg_Parse(return, "(iii)", &array[0], &array[1], &array[2])

    (I assume you left out the error handling code; ignoring the return value
    from PyArg_Parse is not a good idea)

    </F>
     
    Fredrik Lundh, Oct 30, 2005
    #2
    1. Advertisements

  3. Guest

    Thanks Fredrik. Yes, I see now how the function works. I'm new to
    Python and the book I'm studying out of wasn't too explicit in how to
    handle arrays. I've changed the code to what you suggested, but
    strangely enough nothing got read into my array. If I return a single
    integer from my Python method, I get the expected return using:
    PyArg_Parse(return, "i", &integer);
    But,
    PyArg_Parse(return, "(iii)", array, array+1, array+2)
    and
    PyArg_Parse(return, "(iii)", &array[0], &array[1], &array[2])
    does not alter "array" at all. Any ideas? Thanks



    Fredrik Lundh wrote:
    > wrote:
    >
    > > I am returning a tuple from my python method and am stuck trying to
    > > figure out how to read it into a C array using PyArg_Parse.
    > > My C Code:
    > > int array[3];
    > > PyArg_Parse(return, "(iii)", &array);
    > >
    > > My Python Code:
    > > mytuple = (1,2,3)
    > > return mytuple
    > >
    > > That gives me a segmentation fault. What am I doing wrong?

    >
    > you're not providing enough arguments; "iii" means three pointers, not
    > one. try:
    >
    > PyArg_Parse(return, "(iii)", array, array+1, array+2)
    >
    > instead. or, if you prefer maximum clarity:
    >
    > PyArg_Parse(return, "(iii)", &array[0], &array[1], &array[2])
    >
    > (I assume you left out the error handling code; ignoring the return value
    > from PyArg_Parse is not a good idea)
    >
    > </F>
     
    , Oct 30, 2005
    #3
    1. Advertisements

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:
    1,013
    Larry Bates
    Apr 20, 2004
  2. Jinming Xu
    Replies:
    2
    Views:
    3,109
    Craig Ringer
    Jan 21, 2005
  3. Gregor Horvath

    Why tuple with one item is no tuple

    Gregor Horvath, Mar 15, 2005, in forum: Python
    Replies:
    37
    Views:
    1,181
    Antoon Pardon
    Mar 30, 2005
  4. Steve
    Replies:
    1
    Views:
    1,315
    Fredrik Lundh
    Dec 13, 2005
  5. Replies:
    6
    Views:
    326
    Duncan Booth
    Feb 23, 2007
  6. Davy
    Replies:
    3
    Views:
    2,060
    Wildemar Wildenburger
    Nov 7, 2007
  7. Jeff Nyman
    Replies:
    8
    Views:
    650
    Terry Reedy
    Jun 5, 2008
  8. Stuart
    Replies:
    6
    Views:
    372
    Seungbeom Kim
    Mar 11, 2013
Loading...