--- On Fri, 6/27/08, Tim Spens <t_spens@yahoo. comwrote:
From: Tim Spens <t_spens@yahoo. com>
Subject: Re: embedding and extending python C API registering callback handler objects
To: python-list@python.org, "Matimus" <mccredie@gmail .com>
Date: Friday, June 27, 2008, 9:16 AM
thanks, but didn't fix the problem.
>
>
--- On Fri, 6/27/08, Matimus <mccredie@gmail .com>
wrote:
>
registering callback handler objects
<t_sp...@yahoo. com>
herehttp://codeidol.com/python/python3/Embedding-Python/Registering-Callb...
try to
function.
from
PyObject_CallOb ject(function,
&Handler);
Register_Handle r},
but I
-L
GETS
python.
>
>
>
>
--
http://mail.python.org/mailman/listinfo/python-list
Subject: Re: embedding and extending python C API registering callback handler objects
To: python-list@python.org, "Matimus" <mccredie@gmail .com>
Date: Friday, June 27, 2008, 9:16 AM
thanks, but didn't fix the problem.
>
>
--- On Fri, 6/27/08, Matimus <mccredie@gmail .com>
wrote:
>
From: Matimus <mccredie@gmail .com>
Subject: Re: embedding and extending python C API
Subject: Re: embedding and extending python C API
wrote:
Hello all,
>
I've been trying to get an example found
>
I've been trying to get an example found
to work. Every thing works fine except when I
trigger an event from c that will call a python
Here is my test code:
code--------------------------//
triggered from python via callback.so'
%i' % (label, count)
label * count
caught by C layer
>
//-----------------------python
//-----------------------python
#! /usr/bin/env python
import time
import callback
>
def callback1(label ,count):
print 'callback1 successfully
import time
import callback
>
def callback1(label ,count):
print 'callback1 successfully
return 'callback1 =%s number
>
def callback2(label ,count):
return 'callback2 =' +
def callback2(label ,count):
return 'callback2 =' +
>
print '\nTest1:'
callback.setHan dler(callback1)
callback.trigge rEvent() # simulate events
print '\nTest1:'
callback.setHan dler(callback1)
callback.trigge rEvent() # simulate events
>
print '\nTest2:'
callback.setHan dler(callback2)
>
print 'Waiting for callback2 to be called
print '\nTest2:'
callback.setHan dler(callback2)
>
print 'Waiting for callback2 to be called
c:'
code-------------------------------//
count);
&cres);
while 1:
time.sleep(.001 )
>
//-----------------------c
time.sleep(.001 )
>
//-----------------------c
#include <Python.h>
#include <stdlib.h>
>
/* keep Python object in C */
static PyObject *Handler = NULL;
>
void Route_Event(cha r *label, int count){
char *cres;
PyObject *args, *pres;
/* call Python handler */
args = Py_BuildValue(" (si)", label,
#include <stdlib.h>
>
/* keep Python object in C */
static PyObject *Handler = NULL;
>
void Route_Event(cha r *label, int count){
char *cres;
PyObject *args, *pres;
/* call Python handler */
args = Py_BuildValue(" (si)", label,
pres = PyEval_CallObje ct(Handler, args);
Py_DECREF(args) ;
if (pres != NULL){
/* use and decref handler result */
PyArg_Parse(pre s, "s",
Py_DECREF(args) ;
if (pres != NULL){
/* use and decref handler result */
PyArg_Parse(pre s, "s",
printf("%s\n", cres);
Py_DECREF(pres) ;
>
}}
>
// the actual python callback call
static PyObject *
make_call(PyObj ect *function, PyObject *args){
if (function == NULL) return NULL;
PyObject * val =
Py_DECREF(pres) ;
>
}}
>
// the actual python callback call
static PyObject *
make_call(PyObj ect *function, PyObject *args){
if (function == NULL) return NULL;
PyObject * val =
args);
Py_XDECREF(args );
return val;
>
}
>
static PyObject *
Register_Handle r(PyObject *self, PyObject *args){
/* save Python callable object */
Py_XDECREF(Hand ler);
PyArg_Parse(arg s, "O",
return val;
>
}
>
static PyObject *
Register_Handle r(PyObject *self, PyObject *args){
/* save Python callable object */
Py_XDECREF(Hand ler);
PyArg_Parse(arg s, "O",
Py_XINCREF(Hand ler);
Py_INCREF(Py_No ne);
return Py_None;
>
}
>
static PyObject *
Trigger_Event(P yObject *self, PyObject *args){
/* let Python simulate event caught by C */
static int count = 0;
Route_Event("sp am", count++);
Py_INCREF(Py_No ne);
return Py_None;
>
}
>
static struct PyMethodDef callback_method s[] = {
{"setHandler ",
Py_INCREF(Py_No ne);
return Py_None;
>
}
>
static PyObject *
Trigger_Event(P yObject *self, PyObject *args){
/* let Python simulate event caught by C */
static int count = 0;
Route_Event("sp am", count++);
Py_INCREF(Py_No ne);
return Py_None;
>
}
>
static struct PyMethodDef callback_method s[] = {
{"setHandler ",
/* name, address */
/* on first "import callback" */
is called by Python */
callback_method s);
which is registered to Handler
Py_BuildValue(" (si)", label, count); here
{"triggerEvent" , Trigger_Event},
{NULL, NULL}};
>
>
{NULL, NULL}};
>
>
void initcallback(){ /* this
(void) Py_InitModule(" callback",
>
}
>
int main(){
while (1){
printf("1\n");
//attempting to call callback2
}
>
int main(){
while (1){
printf("1\n");
//attempting to call callback2
//i've also tried args =
get a segfault.
Py_BuildValue(" s","c code");
make_call(Handl er,args);
stuff----------------------//
/usr/include/python2.5 -o callback.o
PyObject *args =
printf("2\n");
PyObject* val =
PyObject* val =
printf("3\n");
Py_XDECREF (val);
printf("4\n");
sleep(1);
>
}}
>
//------------------------compiler
Py_XDECREF (val);
printf("4\n");
sleep(1);
>
}}
>
//------------------------compiler
gcc callback.c -c -g -Wall -fpic -I
gcc callback.c -g -Wall -I /usr/include/python2.5
/usr/local/lib -lpython2.5 -o callback
results-------------------//
callback.so
gcc -shared -Wall callback.o -o callback.so
>
//------------------------test code
>
//------------------------test code
../callback.py
Test1:
callback1 successfully triggered from python via
Test1:
callback1 successfully triggered from python via
callback1 =spam number 0
>
Test2:
Waiting for callback2 to be called from c:
#NOTHING EVER GETS PRINTED HERE CALLBACK NEVER
>
Test2:
Waiting for callback2 to be called from c:
#NOTHING EVER GETS PRINTED HERE CALLBACK NEVER
CALLED?
Maybe you just need to flush the stdout buffer in
>
../callback
1
2
3
4
....
>
Thanks,
Tim
../callback
1
2
3
4
....
>
Thanks,
Tim
>
>
>
--
http://mail.python.org/mailman/listinfo/python-list
-Tim