473,396 Members | 1,683 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

PyArg_ParseTuple, test for SEVERAL possibilities

Hi,

i'd like to write an extension module and use
PyArg_ParseTuple in that to parse the parameters.

I have several questions related to this that i did
not find in the documentation:

1. I'd like to check for a list of "int"s and i don't
know how many int's are in this array, it could be
0 to 8, all of this would make sense.

How can i check for that?
2. I'd like to check for SEVERAL possibilities of
argument string, e.g. for "sss" and "li" and "[3]".
How can i do that without error message when the
parameters do not match?
3. Not really related to ParseTuple, but to BuildValue:
I want to create a list as a return value. I want to
create the size of that list at runtime.
For example if i want to return a list with up to
200 values, how can i build that at runtime?
I'm sorry if i missed something, but i read the documentation
and googled for it. But sadly no results...
Best regards,
Torsten.
Jul 18 '05 #1
1 1982

Torsten> 1. I'd like to check for a list of "int"s and i don't
Torsten> know how many int's are in this array, it could be
Torsten> 0 to 8, all of this would make sense.

Torsten> How can i check for that?

How about:

/* define my int args with desired default values */
int i1 = 0, i2 = 0, ..., i8 = 0;

if (!PyArg_ParseTuple(args, "|iiiiiiii", &i1, &i2, ..., &i8) {
return NULL;
}

/* do stuff with i1 ... i8 */

Torsten> 2. I'd like to check for SEVERAL possibilities of
Torsten> argument string, e.g. for "sss" and "li" and "[3]".
Torsten> How can i do that without error message when the
Torsten> parameters do not match?

Just call PyArg_ParseTuple repeatedly until one variant works. Call
PyErr_Clear() before each call. After the last possibility if it still
fails, suppress that last error and raise your own error which will make
more sense than the generic message in the raised exception.

Torsten> 3. Not really related to ParseTuple, but to BuildValue:
Torsten> I want to create a list as a return value. I want to
Torsten> create the size of that list at runtime.
Torsten> For example if i want to return a list with up to
Torsten> 200 values, how can i build that at runtime?

This would be

mylist = PyList_New(0);
if (!mylist) { handle error }

/* append up to 200 values to the list */

In other words, you probably don't want to you Py_BuildValue for this.

Skip

Jul 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: sjh | last post by:
I'm trying to write an extension for python 2.4, and I can't seem to get PyArg_ParseTuple to work with a dict. I've tried all sorts of things, but the most simple thing that fails is: if...
1
by: zyqnews | last post by:
hello all, how to parse the arguments of a c function with PyArg_ParseTuple? The prototype of the c function is : int func(unsigned char * in , int inlen, unsigned char * v, unsigned char * out,...
16
by: Wayne Aprato | last post by:
I have several Access 97 databases which are split into front end and back end running off a server. The front end mde is shared by 2 or 3 - absolute maximum of 6 concurrent users. This scenario...
3
by: Wiktor Zychla | last post by:
I would like to be able to convert enums to their string representation but I would like to be able to apply a custom formatting, so that I could write for example: enum E { a, b, c }; string...
1
by: x0rster | last post by:
Hi, I'm getting an error when using PyArg_ParseTuple and I don't know why...In the documentation I've read that PyArg_ParseTuple raise an appropriate exception : "on failure, it returns false...
27
by: Josh | last post by:
We have a program written in VB6 (over 100,000 lines of code and 230 UI screens) that we want to get out of VB and into a better language. The program is over 10 years old and has already been...
176
by: nw | last post by:
Hi, I previously asked for suggestions on teaching testing in C++. Based on some of the replies I received I decided that best way to proceed would be to teach the students how they might write...
1
by: Miles Lubin | last post by:
I am using PyArg_ParseTuple to parse the arguments (ignoring the keyword arguments) to my initproc for a type I define. It seems that something goes wrong inside PyArg_ParseTuple when it gets the...
0
by: Gabriel Genellina | last post by:
En Mon, 15 Sep 2008 06:58:48 -0300, Pau Freixes <pfreixes@gmail.com> escribió: I don't understand the actual question - the example looks fine, what's your problem? Do you want to check the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...

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.