C Extension question-> How handle lists and tuples as arguments?
Martin v. Löwis
martin at v.loewis.de
Thu Oct 9 00:28:50 EDT 2003
seberino at spawar.navy.mil (Christian Seberino) writes:
> Py_ParseTuple is a great C function to set C variables from
> Python objects in a C extension.
I disagree. It is much better to use specific functions.
> Usage is straight forward for integers and strings but I'm wondering
> what to do if I ever want to send in a list or tuple.
>
> First problem is that we don't know the LENGTH of the list/tuple.
>
> Second problem is we don't know the TYPES of the elements.
Just like you would do it in Python:
len(x) --> PyObject_Length(x)
x[i] --> PySequence_GetItem(x, i) (assuming x is a sequence)
isinstance(o, int) --> PyInt_Check(o)
obtain native value --> PyInt_AsLong(o)
HTH,
Martin
More information about the Python-list
mailing list