PyArg_ParseTupleAndKeywords

Mr.M mrm at unknown.nospam
Sat Jan 23 00:23:20 CET 2010


Hi,

i can't understand what i'm doing wrong. I have a c/api that implements 
a new class.
In (initproc) function i have somethink like this:

[code]

(some declarations omitted here)
static char* keywordlist[] = {"service",
                               "event_type",
                               "free_text",
                               "group_uid",
                               "remote_name",
                               "remote_abook_uid",
                               "start_time",
                               "end_time",
                               "storage_time",
                               "flags",
                               "bytes_sent",
                               "bytes_received",
                                NULL};

if (!PyArg_ParseTupleAndKeywords(args, keywords, "|ssssssiiiiii", 
keywordlist,
                                &service,
                                &event_type,
                                &free_text,
                                &group_uid,
                                &remote_name,
                                &remote_abook_uid,
                                &start_time,
                                &end_time,
                                &storage_time,
                                &flags,
                                &bytes_sent,
                                &bytes_received)) return -1;

[/code]

Ok, what i want is something like this:

[code]
import mymodule
a = mymodule.myclass() # ok, this works
a = mymodule.myclass(flags = 1) # bad, this won't work
 > TypeError: expected string or Unicode object, tuple found
[/code]

I found that if i write this everything works fine:

[code]
import mymodule
a = mymodule.myclass(service = "blabla", event_type = "event", free_text 
  = "free", group_uid = "group", remote_name = "remote name", 
remote_abook_uid = "abook", start_time = 1, end_time = 61, storage_time 
= 61, flags = 2)
[/code]

in other words, the code only works if *EVERY* argument is specified in 
exactly the same position it appears in keyword-null-terminated array.

Could you please help me?

Thank you,

Luca.



More information about the Python-list mailing list