question: numarray c extension error handling?

Russell E. Owen no at spam.invalid
Wed Mar 10 16:04:07 EST 2004


I'm writing a C extension for numarray and am puzzled about the idiom 
for error handling.

The documentation seems to say one should always decref an array after 
calling  NA_InputArray, etc., to convert numarray args to C arrays. 

However, the example and also the numarray code suggests that it's OK to 
return early via (for example) PyErr_Format without doing the DECREF. 
For example, I have appended a very abbreviated version of the sample 
code.

Comments? I'll plow ahead and follow the example (i.e. with early 
return) but I was wondering if anyone knew why this was OK. Perhaps I 
should put in a request to the numarray project for a clarification in 
the documentation.

-- Russell

static PyObject * Py_Convolve1d(PyObject *obj, PyObject *args)
{
    PyObject   *okernel,...;
    PyArrayObject *kernel...;

    if (!PyArg_ParseTuple(args, "OO|O", &okernel, ...))
        return PyErr_Format(_convolveError, 
                    "Convolve1d: Invalid parameters.");

    /* Align, Byteswap, Contiguous, Typeconvert */
    kernel  = NA_IoArray(okernel, tFloat64, C_ARRAY);
...
    if (!kernel...)
        return PyErr_Format( _convolveError, 
                 "Convolve1d: error converting array inputs.");
...
    Py_XDECREF(kernel);
...
}

The relevant section of the docs (section 12.3 High-level API):

The return value of each function (NA_InputArray, NA_OutputArray, or 
NA_IoArray) is either a reference to the original numarray object, or a 
reference to a temporary numarray. Following execution of the C-code in 
the extension function body this pointer should *always* be DECREFed.



More information about the Python-list mailing list