error handling in multithreaded extension callbacks

Connor maikwo at gmail.com
Fri Feb 14 11:09:56 EST 2014


Hi,

In my extension I'm calling python functions as callbacks from a thread 
generated in an external module.
This works very well, but I'm not sure about the error handling.

1. Normally the python interpreter exits if it runs on an unhandled 
error. Is this the preferred standard behavior for this case too ?
2. How can I implement this in my error handling ?

Here is my code so far. If an error happens in the python code it is 
printed to stderr, but the python script is still running in the main 
thread.

void FUNC_C_DECL coreCommandReadyCallback(TML_COMMAND_HANDLE cmd, 
TML_POINTER data)
{
   // Calling Python out of a non- python created thread
   PyGILState_STATE gstate;
   try {
     gstate = PyGILState_Ensure();

     PythonCallbackData* callbackData = (PythonCallbackData*) data;

     PyObject *arglist;

     PyObject *pythonCBFunc = callbackData->pCBFunc;
     /* Time to call the callback */
     arglist = Py_BuildValue("(LO)", cmd, callbackData->pCBData);
     PyObject* result = PyEval_CallObject(pythonCBFunc, arglist);
     Py_DECREF(arglist);

     if ( PyErr_Occurred() ) {
       PyErr_Print();
       PyErr_SetString(PyExc_TypeError, "PyErr_Occurred");
     }


     // Release calling Python out of a non- python created thread
     PyGILState_Release(gstate);
   }
   catch (...) {
     printf ("An Exception Happened\n");
   }
}

Cheers,
Connor




More information about the Python-list mailing list