Problem with callbacks from C to Python

Tyler W. Wilson tyler.w.wilson at gte.net
Sat Jan 5 17:33:05 EST 2002


Okay, to partly answer my own question, but look for more answers, this is
what I have determined: it has something to do with a threading issue, I
think.

I tried the CallObject from with the set_callback method, and everything
worked fine. So, given that I am trying to callback into the Python
interpreter in response to a Windows message, it seems likely that Pythin is
being re-entered, and does not like it much.

I looked over the threading and locking calls available, and tried a few
things. I got to the point where it does crash, but the member never really
gets called.

So, I need a way to call back into the Python interpreter in the proper
state. Any ideas? I am going to get the tkinter source, and see what they do
(I am working off the binary distribution, since the source ball is too
large).

Thanks,
Tyler

"Tyler W. Wilson" <tyler.w.wilson at gte.net> wrote in message
news:_lsZ7.1089$q46.118494 at dfiatx1-snr1.gtei.net...
> I am working on an extension in module in C, and there is a place where i
> need to call back into the interpreter. I have set up the interface like
> this:
>
> object.set_callback(self.method)
>
> I get the function pointer okay on the C side, but later, when I try to
call
> it, I crash in Python22.dll (Python.exe). Here is the code on the C side:
>
> static PyObject *ds_Media_set_callback(PyObject *self, PyObject *args)
> {
>  ds_MediaObject *mo = (ds_MediaObject *)self;
>
>  PyObject *callback=0;
>
>     if (!PyArg_ParseTuple(args, "O", &callback))
>  {
>         return 0;
>  }
>
>  if (!PyCallable_Check(callback))
>  {
>   return 0;
>  }
>
>     Py_XINCREF(callback);
>  Py_XDECREF(mo->callback);
>
>  mo->callback = callback;
>
>  Py_INCREF(Py_None);
>  return Py_None;
> }
>
> // This is called indirectly from DirectShow, through the window proc,
then
> through the function
> // down below
> //
> static void ds_Media_on_callback(ds_MediaObject *self, long event, long
> param1, long param2)
> {
>  switch (event)
>  {
>  case EC_COMPLETE:
>   if (self->callback)
>   {
>    PyObject *arglist = Py_BuildValue("(i)", EC_COMPLETE);
>    PyObject *result = PyEval_CallObject(self->callback, arglist);    //
> <<<<<--- CRASH HERE!
>
>    Py_XDECREF(result);
>    Py_XDECREF(arglist);
>   }
>   break;
>
>  default:
>   break;
>  }
> }
>
>
> I tried the other PyEval functions - PyEval_CallFuntion and
> PyEval_CallMethod - but they crash too.
>
>
> Any ideas? Additional info?
>
> Thanks,
> Tyler
>
>





More information about the Python-list mailing list