Re: Variable arguments in modules

Guido.van.Rossum@cwi.nl
Wed, 03 Nov 1993 10:26:48 +0100

> A variable number of arguments to a C-function isn't that unusual. (At
> least I have a C-library that I would like to interface to python that
> has a lot of functions that take variable number of arguments.)
> Unfortunately it seems like the support for extension modules written
> in C currently doesn't support this. Right?

I assume you're thinking of making a Python wrapper for a
printf()-like function?

The problem here is that C doesn't support this in a portable general
manner (in fact that's why vprintf() et al. were invented). There is
no way to "break open" the calling sequence. (I.e. C doesn't have an
equivalent to Python's "apply()".)

> I guess I can always write a C-layer with fixed number argument
> functions that concatenates the arguments and a corresponding python
> wrapper but that seems a bit silly.

Yes I guess the closest you can get is something like

switch (gettuplesize(args)) {
case 0: foo(); break;
case 1: foo(getintvalue(gettupleitem(args, 0)),
getintvalue(gettupleitem(args, 1))); break;
/* etc. */
}

This assumes that all arguments are of a fixed type (i.e. printf()
would still be impossible to do right protably, since there are
compilers that pass floats or doubles in a different way than ints).

Note that you can set the third element of the "struct methodlist" to
1 to indicate that the Python argument list should always be passed as
a tuple, instead of passing NULL for no arguments and the argument
itself for one argument as is the default.

--Guido van Rossum, CWI, Amsterdam <Guido.van.Rossum@cwi.nl>