473,651 Members | 2,635 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

PyArg_ParseTupl e, test for SEVERAL possibilities

Hi,

i'd like to write an extension module and use
PyArg_ParseTupl e 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 1992

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_ParseTu ple(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_ParseTupl e 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
5134
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 (!PyArg_ParseTuple(args, "O", &file)) { return NULL; }
1
3171
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, int * outlen); The problem is , when the func returns, there will be results in out and outlen. Is the following format is correct? PyObject* wrap_func(PyObject* self, PyObject* args)
16
2191
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 has been working flawlessly for about 2 years. I am now at a point where these databases need to be converted to Access 2003. I think I read somewhere on this forum that the newer versions of Access are not as tolerant to multiple users...
3
3299
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 s1 = E.a.ToString( "d" ); // returns for example 'a' string s2 = E.a.ToString( "f" ); // returns for example 'this is an enum value a'
1
2489
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 and raises the appropriate exception." How can I get the exception in C ? because I've checked the arguments before calling the C python function and I don't know why I've got a problem when parsing the arguments list...
27
3775
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 ported from VB3 to VB6, a job which took over two years. We would like to port it to Python, but we need to continue to offer upgrades and fixes to the current VB6 version. Does anybody know of ways we could go about rewriting this, one screen at a...
176
8331
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 their own unit test framework, and then in a lab session see if I can get them to write their own. To give them an example I've created the following UTF class (with a simple test program following). I would welcome and suggestions on how anybody...
1
1845
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 wrong number of arguments (my format string is "OO"); if the function isn't given exactly two arguments, I get a bus error on OS X and a segfault on Linux. If two arguments are given, the code runs as expected. This does not occur when using...
0
1251
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 object types? Use PyDict_Check / PyList_Check. -- Gabriel Genellina
0
8349
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8275
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8460
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8576
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7296
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5609
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4281
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2696
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 we have to send another system
2
1585
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.