argument type checking before parsing

Skip Montanaro skip at pobox.com
Thu Dec 20 14:40:27 EST 2001


    Benjamin> Based on the stacktype extension from "Programming Python", I
    Benjamin> am trying overload the constructor to either accept nothing,
    Benjamin> or a string (and integer in the future).

You probably need a code structure that looks something like this (untested)
code:

    PyObject *arg = NULL;

    if (!PyArg_ParseTuple(args, "|O", &arg))
        return NULL;

    if (arg == NULL) {
        return (PyObject *)newstackobject();
    }

    if (PyString_Check(arg) {
        return (PyObject *)newstackobject_s(PyString_AsString(arg));
    }

    if (PyInt_Check(arg) {
        return (PyObject *)newstackobject_i(PyInt_ASLONG(arg));
    }

    PyErr_SetString(PyExc_TypeError,
                        "unrecognized argument type for stack initializer");
    return NULL;

-- 
Skip Montanaro (skip at pobox.com - http://www.mojam.com/)




More information about the Python-list mailing list