A GOTO example (was question: numarray c extension...)

Joe Mason joe at notcharles.ca
Fri Mar 12 12:58:43 EST 2004


In article <c2ss14$dp0$2 at news.service.uci.edu>, Josiah Carlson wrote:
> It is arbitrarily trivial to convert it to use an error boolean.

No, it isn't, as evidenced by the fact that you did it wrong.

> static PyObject * Py_Convolve1d(PyObject *obj, PyObject *args)
> {
>      PyObject   *okernel,...;
>      PyArrayObject *kernel = NULL;
>      bool error = 0;
> 
>      if (!PyArg_ParseTuple(args, "OO|O", &okernel, ...)) {
>          PyErr_Format(_convolveError,
>                      "Convolve1d: Invalid parameters.");
>          error = 1;
>      }

       if (!error) {
>      kernel  = NA_IoArray(okernel, tFloat64, C_ARRAY);
>      if (!kernel...) {
>          PyErr_Format( _convolveError,
>                   "Convolve1d: error converting array inputs.");
>          error = 1;
>      }
       }

       if (!error) {
>      ...more stuff...
       }

>      if (!error) {
>          Py_XDECREF(kernel)
>          return the result
>      } else {
>          Py_XDECREF(kernel)
>          return NULL;
>      }
> }

You have to remember to check the error variable at every step in the
function, or use else clauses and deal with heavy nesting.  This is a
perfect situation for GOTO.

Joe



More information about the Python-list mailing list