Problems sending list/tuple to a C extension.

Discussion in 'Python' started by Christian Seberino, Nov 21, 2003.

  1. I successfuly created some toy C extensions that compiled and
    ran without bugs with arguments that consisted of strings and numbers.

    python.org docs seem to suggest that sending in lists and tuples
    was as simple as changing string in PyArg_ParseTuple to contain
    parens or brackets....

    e.g.

    "ii" ==> "(ii)" or "[ii]"

    I tried this many times and always never got PyArg_ParseTuple to
    successfully work with new version.


    >>> import mymodule2
    >>> mymodule2.test([3,4])

    a=1108554272, b=1107769229

    a should be 3 and b should be 4!!!

    Chris
    Christian Seberino, Nov 21, 2003
    #1
    1. Advertising

  2. (Christian Seberino) writes:

    > python.org docs seem to suggest that sending in lists and tuples
    > was as simple as changing string in PyArg_ParseTuple to contain
    > parens or brackets....


    What documentation are you specifically referring to? PyArg_ParseTuple
    does not support square brackets.

    Regards,
    Martin
    Martin v. =?iso-8859-15?q?L=F6wis?=, Nov 22, 2003
    #2
    1. Advertising

  3. Martin

    Thanks for your reply. I found an example on python.org
    of parens for PyArg_ParseTuple at

    http://python.org/doc/current/ext/parseTuple.html

    And an example of parens AND square brackets for Py_BuildValue at:

    http://python.org/doc/current/ext/buildValue.html

    but you are right... no square bracket for PyArg_ParseTuple.


    I was not able to get parens (tuples) to work for either
    successfully. Any ideas?

    Chris


    (Martin v. Löwis) wrote in message news:<-berlin.de>...
    > (Christian Seberino) writes:
    >
    > > python.org docs seem to suggest that sending in lists and tuples
    > > was as simple as changing string in PyArg_ParseTuple to contain
    > > parens or brackets....

    >
    > What documentation are you specifically referring to? PyArg_ParseTuple
    > does not support square brackets.
    >
    > Regards,
    > Martin
    Christian Seberino, Nov 22, 2003
    #3
  4. (Christian Seberino) writes:

    > I was not able to get parens (tuples) to work for either
    > successfully. Any ideas?


    You probably did something wrong. Without seeing the code, it is hard
    to tell what that might be.

    Regards,
    Martin
    Martin v. =?iso-8859-15?q?L=F6wis?=, Nov 22, 2003
    #4
  5. Martin

    I hope the mistake in this simple code is obvious to you:

    ======================================================================
    #include <Python.h>

    static PyObject* test(PyObject* self, PyObject* args) {
    int a;

    PyArg_ParseTuple(args, "(i)", &a);

    printf("a = %d\n",a);

    return Py_BuildValue("i", &a);
    };

    static char test_doc[] = "Does nothing.\n";

    static PyMethodDef mapper[] = {
    {"test", (PyCFunction) test, METH_VARARGS, test_doc},
    {NULL}
    };

    void inittest(void) {
    Py_InitModule3("test", mapper, test_doc);
    };


    ======================================================================

    Here is me compiling and running...

    % gcc test.c -I /usr/include/python2.2 -fpic -shared -o test.so
    % python
    Python 2.2.2 (#1, Feb 24 2003, 19:13:11)
    [GCC 3.2.2 20030222 (Red Hat Linux 3.2.2-4)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import test
    >>> test.test( (3) )

    a = 135386744
    -1073750864
    >>>


    Thanks again for the help.

    Chris

    (Martin v. Löwis) wrote in message news:<-berlin.de>...
    > (Christian Seberino) writes:
    >
    > > I was not able to get parens (tuples) to work for either
    > > successfully. Any ideas?

    >
    > You probably did something wrong. Without seeing the code, it is hard
    > to tell what that might be.
    >
    > Regards,
    > Martin
    Christian Seberino, Nov 23, 2003
    #5
  6. (Christian Seberino) writes:

    > I hope the mistake in this simple code is obvious to you:


    There are two errors in your code:

    > PyArg_ParseTuple(args, "(i)", &a);


    You MUST check the return value of PyArg_ParseTuple. Otherwise,
    if it gives an exception, you just continue with incorrect data,
    which is what happened here.

    > >>> test.test( (3) )


    This does NOT pass a tuple, but an integer only. Use (3,) to display a
    tuple.

    Regards,
    Martin
    Martin v. =?iso-8859-15?q?L=F6wis?=, Nov 23, 2003
    #6
  7. Martin

    THANKS!! Everything works now!!! I really appreciate it.
    This was really valuable help since this is really important
    to know. It was reallly kind of you to look at my code and
    help me out.

    Two comments:

    1. It seems very strange that checking/not checking return
    value of PyArg_ParseTuple would make the difference
    between working code and code that bombs. Not ALL code
    is like this!!! I ignore lots of return codes and
    things work fine lots of times!
    Any idea why this API is so picky this way unlike others?

    2. PyArg_ParseTuple string cannot have square brackets.
    ( "(ii)" is OK but "[ii]" is NOT OK )
    However, I noticed if I call function with a list instead
    of a tuple in "(ii)" case that it WORKS!!! So it seems
    "(ii)" case takes care of tuples AND lists right??
    This is also strange since Py_BuildValue accepts
    "[ii]". Any ideas way Py_BuildValue likes square brackets
    but PyArg_ParseTuple does not?

    Thanks again,

    Chris


    (Martin v. Löwis) wrote in message news:<-berlin.de>...
    > (Christian Seberino) writes:
    >
    > > I hope the mistake in this simple code is obvious to you:

    >
    > There are two errors in your code:
    >
    > > PyArg_ParseTuple(args, "(i)", &a);

    >
    > You MUST check the return value of PyArg_ParseTuple. Otherwise,
    > if it gives an exception, you just continue with incorrect data,
    > which is what happened here.
    >
    > > >>> test.test( (3) )

    >
    > This does NOT pass a tuple, but an integer only. Use (3,) to display a
    > tuple.
    >
    > Regards,
    > Martin
    Christian Seberino, Nov 23, 2003
    #7
    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. Tim Peters
    Replies:
    3
    Views:
    250
    Christian Seberino
    Nov 26, 2003
  2. Michal Mikolajczyk
    Replies:
    1
    Views:
    792
    Larry Bates
    Apr 20, 2004
  3. Jeff Epler
    Replies:
    0
    Views:
    924
    Jeff Epler
    Apr 20, 2004
  4. Davy
    Replies:
    3
    Views:
    1,880
    Wildemar Wildenburger
    Nov 7, 2007
  5. Jeff Nyman
    Replies:
    8
    Views:
    371
    Terry Reedy
    Jun 5, 2008
Loading...

Share This Page