PyArg_ParseTuple question
Mark Crispin
nospam at panda.com
Mon Dec 13 19:50:21 EST 2010
In a C module, I want to pick up the arguments for a Python call like:
module.call("string1",["string2a", "string2b", "string2c"], "string3")
and stash these into:
char *arg1;
char *arg2[];
char *arg3;
All arguments are required, and we can assume that the arg2 vector is
terminated with a null pointer.
It doesn't look like PyArg_ParseTuple will do this easily; and that
instead I have to use either the "O!" format with a PyList prototype, or
use "O&" and write a converter.
If I use "O!", at what level does it check? In particular, does it just
check that the argument is a list, so I can get away with something like:
static PyObject *call(PyObject *self, PyObject *args)
{
char *arg1, **arg2, *arg3;
PyObject *arg2obj;
PyObject *list = PyList_New(0);
PyObject *ret = NULL;
if(PyArg_ParseTuple(args, "sO!s", &arg1, list, &arg2obj, &arg3) {
// grovel over arg2obj to get the number of members and make
// sure they are all strings
// allocate a big enough arg2
// copy the string pointers.
ret = doMyThing(arg1, arg2, arg3);
}
PyObject_del(list);
return ret;
}
By the way,...
Python has tossed me a couple of curve balls, but all in all it's been
quite reasonable. I still haven't read though the Python tutorial (I
really do need to get up to speed on all the data types), but I've been
able to get a tremendous amount of work done in a very short period of
time.
Oh, I've made it SEGV a few times, but these were all blunders in my C
module, such as a C method returning self to indicate success (oops!).
Python is the best development environment since LISP and SmallTalk nearly
20 years ago. Coming from me, that's quite high praise.
-- Mark --
http://panda.com/mrc
Democracy is two wolves and a sheep deciding what to eat for lunch.
Liberty is a well-armed sheep contesting the vote.
More information about the Python-list
mailing list