PyArg_ParseTuple part 2

John john.thai at dspfactory.com
Wed Jun 13 21:03:28 CEST 2001


Okay, here's some code (I've omitted some of the error checking for
simplicity):

static PyObject *PyGetComInterface(PyObject *self, PyObject *args) {

    PyObject *t;
    PyObject *pyList;
    int x;

    // extract list
    PyArg_ParseTuple(args, "O!", &PyList_Type, pyList);

    // make a tuple version of list
    t = PyList_AsTuple(pyList);

    // extract an integer from list *Note: t is always null at this point
    if (t != NULL) {
        PyArg_ParseTuple(t, "i", &x);
        fprintf(stdout, "List contains: %d\n", x);
    }

    Py_INCREF(Py_None);
    return Py_None;
}


"Michael Hudson" <mwh at python.net> wrote in message
news:m3g0d4w7gy.fsf at atrus.jesus.cam.ac.uk...
> "John" <john.thai at dspfactory.com> writes:
>
> > Hi,
> >
> >     In an extended python c function, I use PyArg_ParseTuple to extract
a
> > list object from python.  I then use PyList_AsTuple to get a tuple
version
> > of the list.  I'm expecting an list of 2 integers, so I use
PyArg_ParseTuple
> > again, passing in this new derived tuple, and specifying "ii".  Should
this
> > not work?  First, after I call PyList_AsTuple, I get NULL back instead
of a
> > PyObject.  Second, lets say I pass variable x (from python) = [1,2] into
my
> > function, right after making this function call foo(x), no matter what I
> > type, either x, or just enter, I get the following msg:
> >
> > SystemError: C:\Code\python\dist\src\Objects\listobject.c:1317: bad
argument
> > to
> > internal function
> >
> > Anyone know what's going on?
>
> Err, you are specifying your function as METH_VARARGS, right?  (Just a
> guess).  Otherwise, how about showing us some code - I can't really
> follow your description above.
>
> > Related questions:  Is there a fast way of extracting elements from a
list,
> > other than using PyList_GetItem, then converting the PyObject into the
type
> > you are looking for?
>
> Well, there's PyList_GET_ITEM, but no, not really.  Lists store
> PyObject*s - what were you expecting?
>
> > And why isn't there a PyInt_FromInt(int) function?
>
> There's PyInt_FromLong - is that what you're after?
>
> Cheers,
> M.
>
> --
>   Presumably pronging in the wrong place zogs it.
>                                         -- Aldabra Stoddart, ucam.chat





More information about the Python-list mailing list