[pygtk] gtk threads with a C extension module and callback

Gustavo J. A. M. Carneiro gjc at inescporto.pt
Thu Feb 2 03:31:33 WST 2006


On Wed, 2006-02-01 at 10:19 -0600, Kim Novak wrote:
> I am having an issue when I enable gtk threading (call 
> gtk.threads_init() then gtk.main()). I'm not actually running any other 
> threads yet but would like to, thus I have to call gtk.threads_init(). I 
> don't know if this is a pygtk, gtk, python issue or user ignorance.
> 
> I've narrowed this down to a small example. The python code registers a 
> callback with the C extension module then calls gtk.main. The C 
> extension module registers a glib timeout event. When the timeout occurs 
> the C code invokes the Python callback. It works if I don't call 
> gtk.threads_init() and seg faults if I do.
> 


  Check my pseudo-diff to your python callback wrapper for the fix:


> /* The timeout event handler for gtk event loop */
> gint
> on_timeout(gpointer data)
> {
>        PyObject *arglist;
>        PyObject *result;
+    PyGILState_STATE state;
+
+    state = pyg_gil_state_ensure();

> 
>        /* Time to call the callback */
>        arglist = Py_BuildValue("(i)", ++counter);
>        result = PyEval_CallObject(my_callback, arglist);
>        Py_DECREF(arglist);

+    if (!result)
+	PyErr_Print();
-    Py_DECREF(result);
+    Py_XDECREF(result);
+    pyg_gil_state_release(state);

> 
>        return 1;
> }
> 

-- 
Gustavo J. A. M. Carneiro
<gjc at inescporto.pt> <gustavo at users.sourceforge.net>
The universe is always one step beyond logic



More information about the pygtk mailing list