PyArg_ParseTuple and dict

John Machin sjmachin at lexicon.net
Sun Jun 5 20:52:55 EDT 2005


sjh at gmail.com wrote:
> I'm trying to write an extension for python 2.4, and I can't seem to
> get PyArg_ParseTuple to work with a dict.  I've tried all sorts of
> things, but the most simple thing that fails is:
> 
> [...]
>   if (!PyArg_ParseTuple(args, "O",  &file)) {
>         return NULL;
>   }
> [...]
> 
> 
> If I call the function from python with an int, or a string, etc it
> works fine.  If I pass in a dict I get:
> 
> SystemError: new style getargs format but argument is not a tuple
> 
> 
> even though a call to:
> PyObject_TypeCheck(args,  &PyTuple_Type)
> 
> tells me that args is a tuple.  PyObject_Print(args, stdout, NULL) even
> produces:
> 
> ({'foo': 1},)
> 
> Can anyone give me a minimal example of a C function that takes a dict
> and parses it out with PyArg_ParseTuple?

1. On the surface, there appears to be a bug. In the interests of 
finding out where the bug is, perhaps it would be better if you posted 
your minimal compilable runnable example of what *doesn't* work.

2. To get you off the ground: *if* there is only one argument and it can 
only be described in general terms like "O" (i.e. you still need to 
check the type) then there is little point in using PyArg_ParseTuple; 
describe the function as using METH_O instead of METH_VARARGS, and 
access the arg directly.

3. What is your platform? Which C compiler? What warnings does it give, 
[or would it give if allowed free speech]? Are you running Python 2.4 or 
2.4.1?

4. Do you get the same symptoms when you pass in a list instead of a 
dict? What about a minimal user-created object?

Cheers,
John



More information about the Python-list mailing list