PyArg_ParseTupleAndKeywords problem 
Author Message
 PyArg_ParseTupleAndKeywords problem

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

Quote:
}

<snip>

After building a module I can do in Pyhton:

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

(2,1)

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

Quote:
>>> test.foo(1,b=4)

Traceback (innermost last):
  File "<string>", line 1, in ?
TypeError: keyword parameter b redefined
Quote:
>>> 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

Quote:
>>>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://www.*-*-*.com/ ~pearu/
Institute of {*filter*}netics 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) ***



Sat, 27 Apr 2002 03:00:00 GMT  
 PyArg_ParseTupleAndKeywords problem

Quote:

> 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};

When using PyArg_ParseTupleAndKeywords I've always had to specify a
keyword string for ALL of the arguments:

     static char *capi_kwlist[] = {"a","b"};

Quote:
> So, clearly PyArg_ParseTupleAndKeywords command emulates only a subset
> of Python possibilities.

Travis


Sat, 27 Apr 2002 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. PyArg_ParseTupleAndKeywords docu ?

2. Problems, problems, problems

3. Eiffel Problems, Problems, Problems

4. Finn Idiom problems and Re: {rho} problem

5. Combinatorial Problem [ & a new Combinatorial Problem ]

6. Database problem/Memory problem??

7. CW 2003 - Focus problem & a select problem

8. 32bit problem - one problem solved & another found

9. GForth problem or my problem?

10. REXX DLL problems solved (was Re: Problems getting C subcommand to work)

11. HTTP-Access2 problem (was Ruby Google problem)

12. Problem with this excercise problem.

 

 
Powered by phpBB® Forum Software