--- On Fri, 6/27/08, Tim Spens <t_*****@yahoo.comwrote:
From: Tim Spens <t_*****@yahoo.com>I think I know where the problem is but I'm unsure how to fix it. When I call Register_Handler(...) from python via callback.setHandler1(callback1) this only seems to affect pythons ability to trigger an "event" in c. PyObject *Handler is always NULL even after I call Register_Handler(...). I thought there was some magic here that was assigning the pointer *Handler to my python callback1 handler so it could be triggered from c?
Subject: Re: embedding and extending python C API registering callback handler objects
To: py*********@python.org, "Matimus" <mc******@gmail.com>
Date: Friday, June 27, 2008, 9:16 AM
thanks, but didn't fix the problem.
--- On Fri, 6/27/08, Matimus <mc******@gmail.com>
wrote:
From: Matimus <mc******@gmail.com>registering callback handler objects
Subject: Re: embedding and extending python C APITo: py*********@python.org<t_sp...@yahoo.com>
Date: Friday, June 27, 2008, 9:03 AM
On Jun 27, 8:22 am, Tim Spenswrote:herehttp://codeidol.com/python/python3/Embedding-Python/Registering-Callb...Hello all,
>
I've been trying to get an example foundtry toto work. Every thing works fine except when Itrigger an event from c that will call a pythonfunction.Here is my test code:from>code--------------------------//
//-----------------------python#! /usr/bin/env pythontriggered from python via callback.so'
import time
import callback
>
def callback1(label,count):
print 'callback1 successfullyreturn 'callback1 =%s number%i' % (label, count)>label * count
def callback2(label,count):
return 'callback2 =' +>caught by C layer
print '\nTest1:'
callback.setHandler(callback1)
callback.triggerEvent() # simulate events>
print '\nTest2:'
callback.setHandler(callback2)
>
print 'Waiting for callback2 to be calledc:'PyObject_CallObject(function,while 1:code-------------------------------//
time.sleep(.001)
>
//-----------------------c#include <Python.h>count);
#include <stdlib.h>
>
/* keep Python object in C */
static PyObject *Handler = NULL;
>
void Route_Event(char *label, int count){
char *cres;
PyObject *args, *pres;
/* call Python handler */
args = Py_BuildValue("(si)", label,pres = PyEval_CallObject(Handler, args);&cres);
Py_DECREF(args);
if (pres != NULL){
/* use and decref handler result */
PyArg_Parse(pres, "s",printf("%s\n", cres);
Py_DECREF(pres);
>
}}
>
// the actual python callback call
static PyObject *
make_call(PyObject *function, PyObject *args){
if (function == NULL) return NULL;
PyObject * val =args);&Handler);Py_XDECREF(args);
return val;
>
}
>
static PyObject *
Register_Handler(PyObject *self, PyObject *args){
/* save Python callable object */
Py_XDECREF(Handler);
PyArg_Parse(args, "O",Register_Handler},Py_XINCREF(Handler);
Py_INCREF(Py_None);
return Py_None;
>
}
>
static PyObject *
Trigger_Event(PyObject *self, PyObject *args){
/* let Python simulate event caught by C */
static int count = 0;
Route_Event("spam", count++);
Py_INCREF(Py_None);
return Py_None;
>
}
>
static struct PyMethodDef callback_methods[] = {
{"setHandler",/* name, address */but I{"triggerEvent", Trigger_Event},/* on first "import callback" */
{NULL, NULL}};
>
>void initcallback(){ /* thisis called by Python */(void) Py_InitModule("callback",callback_methods);>which is registered to Handler
}
>
int main(){
while (1){
printf("1\n");
//attempting to call callback2//i've also tried args =Py_BuildValue("(si)", label, count); hereget a segfault.-LPyObject *args =Py_BuildValue("s","c code");printf("2\n");make_call(Handler,args);
PyObject* val =printf("3\n");stuff----------------------//
Py_XDECREF (val);
printf("4\n");
sleep(1);
>
}}
>
//------------------------compilergcc callback.c -c -g -Wall -fpic -I/usr/include/python2.5 -o callback.ogcc callback.c -g -Wall -I /usr/include/python2.5/usr/local/lib -lpython2.5 -o callbackGETSgcc -shared -Wall callback.o -o callback.soresults-------------------//
>
//------------------------test code../callback.pycallback.so
Test1:
callback1 successfully triggered from python viacallback1 =spam number 0
>
Test2:
Waiting for callback2 to be called from c:
#NOTHING EVER GETS PRINTED HERE CALLBACK NEVERCALLED?python.>Maybe you just need to flush the stdout buffer in
../callback
1
2
3
4
....
>
Thanks,
Tim`sys.stdout.flush()`
Matt
--
http://mail.python.org/mailman/listinfo/python-list
--
http://mail.python.org/mailman/listinfo/python-list
-Tim