[Pythonmac-SIG] Callback function problems
Matthew Smith
mps@utas.edu.au
Mon, 12 Aug 2002 23:07:35 +1000
Hello
If this is better suited to a general python list then sorry...
I'm having problems setting up and using a callback function in my program
that embeds python. I am using the following code:
static PyObject *setTempCallback(PyObject *self, PyObject* args)
{
PyObject *result = NULL;
PyObject *temp;
if (PyArg_ParseTuple(args, "O:set_callback", &temp))
{
if (!PyCallable_Check(temp))
{
PyErr_SetString(PyExc_TypeError, "parameter must be callable");
return NULL;
}
Py_XINCREF(temp); /* Add a reference to new callback */
Py_XDECREF(pythonCallback); /* Dispose of previous callback */
pythonCallback = temp; /* Remember new callback */
// return none
Py_INCREF(Py_None);
result = Py_None;
}
return result;
}
Then when I want to use the python function:
void callPythonFunc(long x, long y)
{
PyObject *arglist;
PyObject *result;
long smaller;
arglist = Py_BuildValue("[l,l]", x, y);
result = PyEval_CallObject(pythonCallback, arglist);
Py_DECREF(arglist);
if (result == NULL)
return;
// use result
// get the arguments
if (!PyArg_ParseTuple(result, "l", &smaller))
return;
printf("the smaller number %d\n", smaller);
Py_DECREF(result);
}
Only "if (result == NULL)" always fails... I tried to put the code in
callPythonFunc into setTempCallback to see if the PyObject, pythonCallback
is having trouble but to no avail. Any ideas??
I'm also attempting to load a module (myModule.py) from another (main.py)
that is run using the following:
err = !PyRun_SimpleString("execfile(\"main.py\")\n");
But I get this error:
Traceback (most recent call last):
File "<string>", line 1, in ?
File "main.py", line 12, in ?
import myModule # import the python module
ImportError: No module named myModule
PythonBinder.app has exited with status 0.
Both main.py and myModule.py are in the same directory as the executable...
Thanks for any help,
Matt Smith