[Python-Dev] example of module interface to a varargs function?

Martin von Loewis loewis@informatik.hu-berlin.de
Tue, 19 Jun 2001 23:25:26 +0200 (MEST)

> The only place in the standard modules I saw that processed a truly
> arbitrary number of arguments is the struct_pack method of the
> struct module, and it doesn't use PyArg_Parse* to process them.  Can
> someone point me to an example of marshalling arbitrary numbers of
> arguments then calling a varargs function?

In a true varargs function, you cannot use PyArg_Parse*. Instead, you
have to iterate over the argument tuple with PyTuple_GetItem, fetching
one argument after another.

Another example of such a function is builtin_max.

> (I'll worry about calling gtk_binding_entry_add_signal after I
> figure out how to marshal the args.)

I'd worry about this first: In C, it is not possible to call a true
varargs function in a portable way if the caller doesn't statically
(i.e. in source code) know the number of arguments. Only the callee
can be variable, not the caller.

A slight exception is that you are allowed to pass-through va_list
objects from one function to another. However, that requires that the
callee expects a va_list argument, i.e. is not a varargs function,
plus there is no portable way to create a va_list object from scratch.

If you absolutely need to call such a function, you can use the Cygnus
libffi function, which, for a certain number of microprocessors and C
ABIs, allows to call arbitrary function pointers.

However, I'd rather recommend to look for alternatives to
gtk_binding_entry_add_signal. E.g. gtk_binding_entry_add_signall
accepts a GSList*, which is a chained list of arguments, instead of
being varargs. This you can call in a C module - the other one is out
of reach.