Mailing List Archive

Using PyArg_VaParse
Hi All,
I want to use PyArg_VaParse to retrieve variable number of arguments.
I am not sure if this is the correct function to use. I have embedded
python in a C++ application and implemented python methods in C++. I
have a situation where, the method may pass any number of string
parameters. I would like to get these parameters in my C++ code. Here is
a scenario of the whole thing.

(C++ implementation.. for method bar in the module foo.)

static PyObject *bar(PyObject *self,PyObject *args)
{
....
if(PyArg_VaParse(args,...) /* What is that I need to use here...? */
...
}

and a pythod call may be like,
import foo
foo.bar('a','b','c')
foo.bar('a','b','c','d'.....)

I need to know the Python API call to use to get the arguments. As I
said before I donot know how many arguments are being passed.

Thanks for any help...

Sundar.R


Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.
Using PyArg_VaParse [ In reply to ]
bhosu@my-deja.com wrote:
>
> Hi All,
> I want to use PyArg_VaParse to retrieve variable number of arguments.

Why? See below.

> I am not sure if this is the correct function to use. I have embedded
> python in a C++ application and implemented python methods in C++. I
> have a situation where, the method may pass any number of string
> parameters. I would like to get these parameters in my C++ code. Here is
> a scenario of the whole thing.
>
> (C++ implementation.. for method bar in the module foo.)
>
> static PyObject *bar(PyObject *self,PyObject *args)
> {
> ....
> if(PyArg_VaParse(args,...) /* What is that I need to use here...? */
> ...
> }

The PyObject *args is already a Tuple, so the only thing
you need to do is to check its length and if
the parameters are stings indeed.

>
> and a pythod call may be like,
> import foo
> foo.bar('a','b','c')
> foo.bar('a','b','c','d'.....)
>
> I need to know the Python API call to use to get the arguments. As I
> said before I donot know how many arguments are being passed.

Just use PyTuple_Size(args), in a loop
use PyTuple_GetItem(args, i)
and check them each with PyString_Check.

good luck - chris

--
Christian Tismer :^) <mailto:tismer@appliedbiometrics.com>
Applied Biometrics GmbH : Have a break! Take a ride on Python's
Kaiserin-Augusta-Allee 101 : *Starship* http://starship.python.net
10553 Berlin : PGP key -> http://wwwkeys.pgp.net
PGP Fingerprint E182 71C7 1A9D 66E9 9D15 D3CC D4D7 93E2 1FAE F6DF
we're tired of banana software - shipped green, ripens at home