Exception in callback => GPF?

Francois De Serres fdeserres at gmx.net
Fri Jul 15 12:01:04 EDT 2005


Hiho,

When there's an unhandled exception in my 
extension-module's-callback-into-Python-function-object, I get a GPF and 
Python exits.
When the exception is being handled within the callback (hence in 
Python), I get a very significant hiccup (1 to 5 seconds freeze).

Question: is there a specific way to deal with exceptions when they 
happen inside such a callback? I searched the web with no avail. All 
callbacks examples I've seen seem not to care about exceptions.

Maybe of importance: the callback is initiated within a native thread.
My C extension module is calling back into Python like this:

/* Python callback function object. */
static PyObject * my_callback = NULL;

/* C callback function passed to the implementation. */
static void external_callback(const MidiData * const data) {
    
    if (my_callback && (my_callback != Py_None)) {

        if (! data) {
            PyErr_SetString(PyExc_IndexError, getLastErrorMessage());
        } else {
   
            PyObject * arglist = NULL;
            PyObject * result = NULL;
   
            PyGILState_STATE gil = PyGILState_Ensure();

            arglist = Py_BuildValue("(i,i,s#)", data->deviceIndex, 
data->timestamp, data->buffer, data->size);// 0, 0, "test", 4);//
            result = PyEval_CallObject(my_callback, arglist);   
                   
            Py_DECREF(arglist);
            Py_DECREF(result);
   
            PyGILState_Release(gil);
        }

    }
}

TIA,
Francois




More information about the Python-list mailing list