473,465 Members | 1,934 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 2329
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Artco News | last post by:
I thought I ask the scripting guru about the following. I have a file containing records of data with the following format(first column is the label): CODE#1^DESCRIPTION^CODE#2^NOTES...
1
by: Andrew MacIntyre | last post by:
I'm seeing a bizarre situation where IndexErrors are being thrown with "tuple index out of range" error strings. The scenario is something like: l = for a, b in l: ...
2
by: Todd Moyer | last post by:
I would like to use Python to parse a *python-like* data description language. That is, it would have it's own keywords, but would have a syntax like Python. For instance: Ob1 ('A'): Ob2...
5
by: Chris | last post by:
Hi Consider the following tuples: >>> t = (,) >>> u = (1,2) >>> v = ('1','2') >>> t (, ) >>> u (1, 2)
2
by: Jinming Xu | last post by:
Hi Folks, Python seems unstable, when allocating big memory. For example, the following C++ code creates a tuple of tuples: PyObject* arCoord = PyTuple_New(n); double d = 1.5; for(int i=0;...
9
by: ankitdesai | last post by:
I would like to parse a couple of tables within an individual player's SHTML page. For example, I would like to get the "Actual Pitching Statistics" and the "Translated Pitching Statistics"...
3
by: Daniel | last post by:
I have a list of strings, which I need to convert into tuples. If the string is not in python tuple format (i.e. "('one', 'two')", "("one", 'two')", etc.), then I can just make it a 1-tuple (i.e....
2
by: Felipe De Bene | last post by:
I'm having problems parsing an HTML file with the following syntax : <TABLE cellspacing=0 cellpadding=0 ALIGN=CENTER BORDER=1 width='100%'> <TH BGCOLOR='#c0c0c0' Width='3%'>User ID</TH> <TH...
10
by: len | last post by:
I have created the following program to read a text file which happens to be a cobol filed definition. The program then outputs to a file what is essentially a file which is a list definition...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.