Fwd: PyArg_ParseTupleAndKeywords in Python3.1

Emeka emekamicro at gmail.com
Fri Dec 18 05:24:26 EST 2009


static PyObject* foo(PyObject *self, PyObject *args, PyObject *kwrds)
{
   int a=65, b=66;
   char *kwlist[] = {"a", "b", NULL};
I am yet to understand what kwlist pointer  does and why it is needed?

   if (!PyArg_ParseTupleAndKeywords(args, kwrds, "|CC", kwlist, &a,
&b))
       return NULL;
   return Py_BuildValue("(CC)", a, b);
}

Regards,
Emeka


The following code seems to work fine for me:

static PyObject* foo(PyObject *self, PyObject *args, PyObject *kwrds)
{
   int a=65, b=66;
   char *kwlist[] = {"a", "b", NULL};
I am yet to understand what kwlist pointer  does and why it is needed?

   if (!PyArg_ParseTupleAndKeywords(args, kwrds, "|CC", kwlist, &a,
&b))
       return NULL;
   return Py_BuildValue("(CC)", a, b);
}

Regards,
Emeka


The default values seem to remain as 'A' and 'B'


On Fri, Dec 18, 2009 at 8:17 AM, casevh <casevh at gmail.com> wrote:

> On Dec 17, 11:14 am, Joachim Dahl <dahl.joac... at gmail.com> wrote:
> > In the Ubuntu 9.10 version of Python 3.1 (using your patch), there's a
> > related bug:
> >
> > >>> foo(b='b')
> >
> > will set the value of a in the extension module to zero, thus clearing
> > whatever
> > default value it may have had.  In other words, the optional character
> > arguments
> > that are skipped seem to be nulled by PyArg_ParseTupleAndKeywords().
>
> The following code seems to work fine for me:
>
> static PyObject* foo(PyObject *self, PyObject *args, PyObject *kwrds)
> {
>     int a=65, b=66;
>     char *kwlist[] = {"a", "b", NULL};
>    if (!PyArg_ParseTupleAndKeywords(args, kwrds, "|CC", kwlist, &a,
> &b))
>        return NULL;
>     return Py_BuildValue("(CC)", a, b);
> }
>
> The default values seem to remain as 'A' and 'B'.
>
> >>> foo()
> ('A', 'B')
> >>> foo(b='b')
> ('A', 'b')
> >>> foo()
> ('A', 'B')
> >>> foo('a')
> ('a', 'B')
> >>> foo('a', b='b')
> ('a', 'b')
> >>> foo()
> ('A', 'B')
> >>>
>
> casevh
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20091218/edd6fdf8/attachment.html>


More information about the Python-list mailing list