PyArg_ParseTupleAndKeywords problem

Pearu Peterson pearu at ioc.ee
Tue Nov 9 20:05:59 CET 1999


Hi!

I am writing a C/API function that has optional arguments. What I would
like to obtain is a C/API version of the following Python function (just
for example):

def foo(a,b=1):
    return a,b

So, I write the following C code:
<snip>
static PyObject *test_foo(PyObject *capi_self, PyObject *capi_args,
PyObject *capi_keywds) {
    PyObject *a_capi = NULL;
    PyObject *b_capi = NULL;
    static char *capi_kwlist[] = {"b",NULL};
    if
(!PyArg_ParseTupleAndKeywords(capi_args,capi_keywds,"O|O",capi_kwlist,&a_capi,&b_capi))
goto capi_fail;
    return Py_BuildValue("OO",a_capi,b_capi);
capi_fail:
    return NULL
}
<snip>

After building a module I can do in Pyhton:

>>> import test
>>> test.foo(1,4)
(1,4)
>>> test.foo(2)
(2,1)

but that is all!!!!!!
For example,

>>> test.foo(1,b=4)
Traceback (innermost last):
  File "<string>", line 1, in ?
TypeError: keyword parameter b redefined
>>> test.foo(a=1,b=4)
Traceback (innermost last):
  File "<string>", line 1, in ?
SystemError: number of items in format string and keyword list do not
match

etc.
Note that if

>>>def foo(a,b=1): return a,b

then
    foo(1,b=4)
    foo(a=1,b=4)
    foo(b=4,a=1)
are correct.

So, clearly PyArg_ParseTupleAndKeywords command emulates only a subset
of Python possibilities.
Can anyone point me out how to get 'test.foo(1,b=4)' to work?
Should I write my own PyArg_ParseTupleAndKeywords function for that?

Thanks,
    Pearu

--
Pearu Peterson , MSc, Researcher
Department of Mechanics and Applied Mathematics          http://koer.ioc.ee/~pearu/
Institute of Cybernetics at Tallinn Technical University Phone: (+372) 6204168
Akadeemia Rd. 21, 12618 Tallinn ESTONIA                  Fax:   (+372) 6204161
*** the nonvalidity of rigorous causality is necessary
    and not just consistently possible (Heisenberg, 1925) ***







More information about the Python-list mailing list