optional arguments in c extensions

Paul Probert pprobert at wisc.edu
Fri Oct 24 12:58:43 EDT 2003


I am trying to write a c interface function to be called with some
optional arguments. In the python documentation section 5.5
"Parsing arguments and building values" it seems you can do it like this:

     PyObject *do_stuff( PyObject *self, PyObject *args){
         PyObject *thing = NULL;
         char *fname;
         if( !PyArg_ParseTuple(args, "s|O", &fname, &thing)){
             PyErr_SetString(PyExc_RuntimeError, "error parsing input");
             return NULL;
         }
     ...

The "|" in "s|O" is supposed to let you call the function with only
one argument. But, I get an exception:

TypeError: do_stuff() takes exactly 2 arguments (1 given)

What am I missing?

Paul Probert
University of Wisconsin





More information about the Python-list mailing list