1

If I am extending Python in C, I can parse arguments provided on the Python side using functions like PyArg_ParseTuple(). However, this only allows me to specify scalar arguments, or tuple arguments of known length. How do I deal with the case where the user calls my function from Python with a tuple of arbitrary length? For example:

import foo
foo.bar([1, 2, 3]) 
foo.bar([4, 5])

Here, bar() is defined in C, and its arguments are parsed using PyArg_ParseTuple(), which copies the argument values to a local variable. I have to specify the argument format and an address into which to copy the parsed value. For instance:

int i,j;
PyArg_ParseTuple(args, "i", &i)
/* or */
PyArg_ParseTuple(args, "(ii)", &i, &j);

But what I want is something like this:

int *i;
int ni;
PyArg_ParseTuple(args, "(i...)", &ni, i);

where i is an array, and ni is the number of elements in the array. Is this possible?

Fadecomic
  • 1,220
  • 1
  • 10
  • 23

1 Answers1

1

Please refer to this thread for detailed explanations, but this was flagged as a duplicate

Python extension module with variable number of arguments

Andre Motta
  • 759
  • 3
  • 16
  • You are just copy pasting answers. That is not cool – Bayko Jul 17 '18 at 17:19
  • I flagged as a duplicate but i didn't know it is not good practice to bring out the best answer from another thread. I stand corrected and will delete this – Andre Motta Jul 17 '18 at 17:24
  • I understand I can manually parse `args`, but what this solution assumes that the only thing passed to bar is the arbitrary length tuple. What happens when the function has more than one argument? What about when there are keyword arguments, which would normally be handled by `PyArg_ParseTupleAndKeywords`? – Fadecomic Jul 17 '18 at 18:17