Re: PyArg_ParseTuple question

Discussion in 'Python' started by Mark Wooding, Dec 14, 2010.

  1. Mark Wooding

    Mark Wooding Guest

    Mark Crispin <> writes:

    > In a C module, I want to pick up the arguments for a Python call like:
    > module.call("string1",["string2a", "string2b", "string2c"], "string3")
    > and stash these into:
    > char *arg1;
    > char *arg2[];
    > char *arg3;
    > All arguments are required, and we can assume that the arg2 vector is
    > terminated with a null pointer.
    >
    > It doesn't look like PyArg_ParseTuple will do this easily; and that
    > instead I have to use either the "O!" format with a PyList prototype,
    > or use "O&" and write a converter.


    I think the latter is probably your best bet.

    > If I use "O!", at what level does it check? In particular, does it
    > just check that the argument is a list, so I can get away with
    > something like:


    It does the equivalent of `isinstance', so you'll accept a `list' or an
    instance of any subclass of `list'.

    The `O&' converter is pretty straightforward. Something like this ought
    to do.

    static int convertlist(PyObject *o, void *p)
    {
    PyObject **v;
    Py_ssize_t i, n;

    /* Could allow general sequences using PySequence_Fast */
    if (!PyList_Check(o)) return (0);

    /* Copy stuff */
    n = PyList_GET_SIZE(o);
    if ((v = PyMem_New(PyObject *, n + 1)) == 0) return (0);
    for (i = 0; i < n; i++) {
    v = PyList_GET_ITEM(o, n);
    Py_INCREF(v);
    }
    v[n] = 0;

    return (1);
    }

    If you want to do a more complex conversion (e.g., to the individual
    items) there's more work to be done.

    I could have used PySequence_* functions to read the size and items, but
    that makes error handling more complicated. One could also borrow the
    references from the underlying list, which would leave the underlying
    storage for the vector as the only thing to free.

    I ended up writing a lot of conversion functions when I was doing Python
    library bindings (for a crypto library); they're generally a good thing.
    I found that the trickiest thing about PyArg_ParseTuple is in making
    sure that you can clean everything up even if it goes wrong half way
    through.

    -- [mdw]
    Mark Wooding, Dec 14, 2010
    #1
    1. Advertising

  2. Mark Wooding, 14.12.2010 21:42:
    > I could have used PySequence_* functions to read the size and items, but
    > that makes error handling more complicated. One could also borrow the
    > references from the underlying list, which would leave the underlying
    > storage for the vector as the only thing to free.
    >
    > I ended up writing a lot of conversion functions when I was doing Python
    > library bindings (for a crypto library); they're generally a good thing.
    > I found that the trickiest thing about PyArg_ParseTuple is in making
    > sure that you can clean everything up even if it goes wrong half way
    > through.


    I think it's worth mentioning here that the Cython compiler generates
    tightly tailored code for argument unpacking and type conversion. So unless
    you really want (or need) to write your code directly against Python's
    C-API, Cython will do a substantially better job with much less coding.
    Even if you have already written a major part of your code in C, it's worth
    considering to (re-)write the Python API wrapper parts in Cython if you
    need better performance.

    Stefan
    Stefan Behnel, Dec 15, 2010
    #2
    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. Michael Gross
    Replies:
    0
    Views:
    614
    Michael Gross
    Jul 30, 2003
  2. =?iso-8859-1?b?IkrpcvRtZQ==?= Laheurte

    Re: Python/C API - PyArg_ParseTuple problem ?

    =?iso-8859-1?b?IkrpcvRtZQ==?= Laheurte, Aug 22, 2003, in forum: Python
    Replies:
    1
    Views:
    386
    =?iso-8859-1?b?IkrpcvRtZQ==?= Laheurte
    Aug 22, 2003
  3. Skip Montanaro
    Replies:
    1
    Views:
    332
    Torsten Mohr
    Jun 7, 2004
  4. Patrick Stinson

    PyArg_ParseTuple

    Patrick Stinson, Jun 9, 2004, in forum: Python
    Replies:
    1
    Views:
    437
    Jeff Epler
    Jun 10, 2004
  5. Patrick Stinson

    lenght of char buffers in PyArg_ParseTuple

    Patrick Stinson, Jun 10, 2004, in forum: Python
    Replies:
    1
    Views:
    394
    Duncan Booth
    Jun 10, 2004
Loading...

Share This Page