C Extension, Optional Args?

Alex cut_me_out at hotmail.com
Wed Sep 13 09:45:00 CEST 2000


Pete's code, with a comment from me:

> static PyObject* simple_func(PyObject* self, PyObject* args)
> {
>     MyRect r;
>     int val;
>     char* nonecatch;
> 
>     if(PyArg_ParseTuple(args, "(hhhh)i", &r.x, &r.y, &r.w, &r.h, &val))
> 	  return PyInt_FromLong(workrect(&r, val));
> 
>     if(PyArg_ParseTuple(args, "zi", &nonecatch, &color))
> 
> 	  /* Why call with val, here? */
> 
> 	  return PyInt_FromLong(workrect(NULL, val));
> 
>     return NULL;
> }

> i'm trying to figure out a way to change one of the arguments to allow
> "None" to be passed and get some default behaviour

I would use the "O" option.  Something like

  z_coord = h_coord = val = NULL;
  
  if (!PyArg_ParseTuple(args, "OO|OOO",
			&x_coord, &y_coord, &z_coord, &h_coord, &val)) {
    return NULL;
  }

  if (x_coord == NULL) {
    /* Convert val to a short int, return workrect(NULL, val) */
  }

  /* Convert everything to a short int, plug values into r, call
     workrect(&r, val) */

It's tedious, though, I suppose.

Alex.

-- 
Speak softly but carry a big carrot.




More information about the Python-list mailing list