PyArg_ParseTupleAndKeywords

Carl Banks pavlovevidence at gmail.com
Sat Jan 23 03:08:26 CET 2010


On Jan 22, 3:23 pm, "Mr.M" <m... at unknown.nospam> wrote:
> 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)

You probably shouldn't have, that could be where the error is....  I'd
include the whole function up to the call that raises the exception.


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


This should be return NULL in most cases, but not all (such as if it's
an object's tp_init--and it does look like that's the case here, so
probably not the issue).


> [/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]

Are you sure that PyArg_ParseTupleAndKeywords is what's raising the
error?

Are you subclassing this type in Python, and passing one of the string
parameters a tuple by mistake?  For instance, did you do something
like this:

class myclass(_mycmodule._myctype):
    def __init__(self,*args,**kwargs):
        log_something_here()
        super(myclass,self).__init__(args,**kwargs)

Note the missing * on args.


> 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.

That it would have to be in a certain order is strange.  Are you sure
it's just not merely that they all have to be present?


> Could you please help me?

Tricky, but I think it'd help if you provided more information.  My
gut feeling is that there exception is being raised somewhere other
than you think it is (i.e., not by PyArg_ParseTuple), so I'd recommend
adding some debugging statements to track down the exact point wher
the error occurs.


Carl Banks



More information about the Python-list mailing list