parsing a tuple in embedded python

J

jenkins.justin

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?
 
F

Fredrik Lundh

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>
 
J

jenkins.justin

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 said:
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>
 

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,997
Messages
2,570,240
Members
46,828
Latest member
LauraCastr

Latest Threads

Top