C Extension, Optional Args?

Pete Shinners pshinners at mediaone.net
Wed Sep 13 02:39:43 EDT 2000


okay, i have a C extension already out there. 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've got something in there, but i still get an error
message. here is the simple version...


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))
        return PyInt_FromLong(workrect(NULL, val));

    return NULL;
}

is there a better way to do this? it feels a little hackish
using the "z" flag to get None (since it would accept a string).
perhaps i should be using "O" and checking the type of
the python object i get? (i'm still learning this, so if
someone stir a show quick example, that'd help big time)

perhaps there's just some sort of "clear error" i should
insert before the second attempt at parsing?







More information about the Python-list mailing list