C Extension question-> How handle lists and tuples as arguments?

Discussion in 'Python' started by Christian Seberino, Oct 8, 2003.

  1. Py_ParseTuple is a great C function to set C variables from
    Python objects in a C extension.

    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.

    Chris
    Christian Seberino, Oct 8, 2003
    #1
    1. Advertising

  2. Christian Seberino wrote:
    > Py_ParseTuple is a great C function to set C variables from
    > Python objects in a C extension.
    >
    > 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.


    I don't understand your problem. For "other" parameters, use "O" as
    format character in ParseTuple...(). Then if you need to check if the
    passed parameter is a sequence (either tuple or list), you can just use
    PySequenceCheck().

    For the rest, use the functions for the sequence protocol, like
    PySequence_Length() and the like.

    HTH,

    -- Gerhard
    =?ISO-8859-1?Q?Gerhard_H=E4ring?=, Oct 8, 2003
    #2
    1. Advertising

  3. (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 --> PySequence_GetItem(x, i) (assuming x is a sequence)
    isinstance(o, int) --> PyInt_Check(o)
    obtain native value --> PyInt_AsLong(o)

    HTH,
    Martin
    Martin v. =?iso-8859-15?q?L=F6wis?=, Oct 9, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Daniel Nogradi
    Replies:
    3
    Views:
    349
    Dennis Lee Bieber
    Nov 10, 2006
  2. Replies:
    5
    Views:
    553
    Thomas J. Gritzan
    Oct 6, 2006
  3. tuples within tuples

    , Oct 26, 2007, in forum: Python
    Replies:
    12
    Views:
    574
    Dennis Lee Bieber
    Oct 27, 2007
  4. xera121
    Replies:
    8
    Views:
    721
    lolmc
    Sep 30, 2009
  5. Jon Reyes
    Replies:
    18
    Views:
    229
    Mitya Sirenef
    Feb 19, 2013
Loading...

Share This Page