Problem with callbacks from C to Python

Tyler W. Wilson tyler.w.wilson at gte.net
Fri Jan 4 20:08:11 EST 2002


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