PyArg_ParseTuple and dict

S

sjh

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?

-Seth
 
J

John Machin

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
 
S

sjh

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.

I'll post it later tonight.
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.

So, I started out using "iO!" and having PyArgParseTuple do the type
checking for me (passing in an &PyDict_Type).

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?

Python 2.4 (#1, Mar 10 2005, 16:54:23) [C] on sunos5

Solaris 9 x86, forte 6.2

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


A list works fine, as does a 1 element tuple with a dict in it. I'm
not sure what you mean by minimal user-created object.

-Seth
 
J

John Machin

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?


Python 2.4 (#1, Mar 10 2005, 16:54:23) [C] on sunos5

Solaris 9 x86, forte 6.2

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

A list works fine, as does a 1 element tuple with a dict in it. I'm
not sure what you mean by minimal user-created object.

class Muco:
pass

my_extension_func(42) # good
my_extension_func("xyzzy") # good
my_extension_func([]) # good
my_extension_func({}) # bad
my_extension_func(Muco()) # dunno yet
 
J

Jeff Epler

I tried to recreate the problem based on what you described in your
message. I was unable to recreate the problem.

I wrote the following file "sjh.c":

#include <Python.h>

PyObject *f(PyObject *self, PyObject *args) {
PyObject *ob = NULL;
if(!PyArg_ParseTuple(args, "O", &ob)) return NULL;
Py_INCREF(Py_None);
return Py_None;
}

PyMethodDef methods[] = {
{"f", (PyCFunction)f, METH_VARARGS, "test function"},
{NULL}
};

void initsjh() {
Py_InitModule3("sjh", methods, "test module");
}

I compiled it for Python 2.3:
$ gcc sjh.c -I/usr/include/python2.3 -L/usr/lib/python2.3/config -lpython2.3 -shared -o sjh.so

and I tested it:
$ python -c 'import sjh; print sjh.f(1)'
None
$ python -c 'import sjh; print sjh.f({})'
None
$ python -c 'import sjh; print sjh.f({None: None})'
None

Jeff

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.6 (GNU/Linux)

iD8DBQFCo7aRJd01MZaTXX0RApSHAJ4/nxFjm+t0flRTLem4/J4+JiluHQCfZ36z
C/WAYVwip2U+34fBjqmSV0g=
=z7Vw
-----END PGP SIGNATURE-----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top