468,284 Members | 1,609 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,284 developers. It's quick & easy.

parsing a tuple in embedded python

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 '05 #1
2 2167
je************@gmail.com 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 '05 #2
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:
je************@gmail.com 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 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Artco News | last post: by
1 post views Thread by Andrew MacIntyre | last post: by
2 posts views Thread by Todd Moyer | last post: by
5 posts views Thread by Chris | last post: by
2 posts views Thread by Jinming Xu | last post: by
9 posts views Thread by ankitdesai | last post: by
3 posts views Thread by Daniel | last post: by
2 posts views Thread by Felipe De Bene | last post: by
reply views Thread by NPC403 | last post: by
2 posts views Thread by MrBee | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.