handling asynchronous callbacks from c++ in a python script

2008-01-23 Thread Tim Spens
I have a c++ program running that has boost python hooks for the c++ api.
I'm running a python client that makes calls into the c++ api.  The problem is 
there are c++
asynchronous callbacks that need to pass information to the python client.  
What I was hoping to
do is call a python function from c++ that resides in the running "main()" 
python client
while in the c++ callback handlers. 
Is this possible and if you can point me to an example or documentation on how 
to do this it would be much appreciated?

NOTE: I've been asking on the c++-sig mailing list about this and David 
Abrahams (very well versed in boost python) said:
"I'm not an expert on asynchronous python and this is really not a Boost.Python 
question. I suggest you ask on the regular Python mailing list.  Sorry."





  

Be a better friend, newshound, and 
know-it-all with Yahoo! Mobile.  Try it now.  
http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ 


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Beginner Pyserial Question

2008-01-24 Thread Tim Spens
COM = 0 #for COM1
BAUD = 115200

class serial_port():
def __init__(self):
self.start_time = None
self.end_time = None
self.asleep_duration = None

self.device = serial.Serial()
self.device.timeout = 1
self.device.baudrate = BAUD
self.device.port = COM

a_serial_port = serial_port()
a_serial_port.device.open()

- Original Message 
From: "[EMAIL PROTECTED]" <[EMAIL PROTECTED]>
To: [email protected]
Sent: Thursday, January 24, 2008 10:13:39 AM
Subject: Re: Beginner Pyserial Question



> My guess is that for whatever reason the 'first' serial port
> (which is what you're asking for by specifying a 0 when
> instantiating the Serial class) doesn't actually exist. Serial
> device names under Windows are broken.

I realize this.  I tried connecting to different port "numbers", but I
have not tried the serial.Serial(COM1).  I wasn't sure if that worked,
but I know a quick way to find out.


> Try using the actual name of the com port (e.g. 'COM3' or
> 'COM5') instead of 0.

The com port used in Hyper Terminal is COM40.  I have tried connecting
to 39/40/41 to no avail.

> Oh, if you end up having to use a com port higher than COM9,
> that's broken in Windows as well, and you've got to sprinkle a
> bunch of backslashes into the device name (I don't remember the
> exact syntax).

That might become an issue when I try to read COM40 for the GPS
bluetooth transmission.  This issue does not relate to why I cannot
open  smaller com ports, though.

-- 
http://mail.python.org/mailman/listinfo/python-list





  

Never miss a thing.  Make Yahoo your home page. 
http://www.yahoo.com/r/hs

-- 
http://mail.python.org/mailman/listinfo/python-list


simplest c python api callback example

2008-06-26 Thread Tim Spens
The following is a simple complete example using the c python api to generate 
callbacks from c to python.  But when I run the c code I get a segfault in 
PyInt_FromLong () (see below).  Most of this example code was taken from pg 
1478 of the 3rd edition python o'reilly book.  I cannot see what I'm doing 
wrong here to get this segfault?  Please help.

//---python-code-//
#! /usr/bin/env python
###
# register for and handle event callbacks from C;
# compile C code, and run with 'python register.py'
###

import time

#
# C calls these Python functions;
# handle an event, return a result
#
def callback1(label, count):
return 'callback1 called with args: label-%s and count-%i' % (label, count)
#
# Python calls a C extension module
# to register handlers, trigger events
#

import cregister
cregister.setpythonHandler(callback1)

print '\nwaiting for callback pythonHandler to be called:'
while 1:
time.sleep(1)

//---compiler commands--//
gcc -Wall -fno-strict-aliasing -g -I /usr/include/python2.5 -c cregister.c;gcc 
-g -Wall -I /usr/include/python2.5 -L /usr/local/lib -lpython2.5 cregister.c -o 
cregister;gcc -shared -fPIC -g -Wall -I /usr/include/python2.5 -L /usr/lib 
-lpython2.5 -o cregister.so cregister.o

//---c-code-//
#include 
#include 

/***/
/* 1) code to route events to Python object*/
/* note that we could run strings here instead */
/***/

//python handlers
//keep Python object in C
static PyObject *pythonHandler = NULL;

void Route_Event(){
PyObject *args;
// call Python handler
args = Py_BuildValue("(si)", "c code", 5);
PyEval_CallObject(pythonHandler, args);
}

/*/
/* 2) python extension module to register handlers   */
/* python imports this module to set handler objects */
/*/
static PyObject *
Register_pythonHandler(PyObject *self, PyObject *args){
// save Python callable object
Py_XDECREF(pythonHandler); //called before?
PyArg_Parse(args, "O", &pythonHandler);//one argument?
Py_XINCREF(pythonHandler); //add a reference
Py_INCREF(Py_None);//return 'None': success
return Py_None;
}

//makes these functions available by importing cregister in python
static struct PyMethodDef cregister_methods[] = {
{"setpythonHandler",Register_pythonHandler},
{NULL, NULL}
};

// this is called by Python on first "import cregister"
void initcregister(){
(void) Py_InitModule("cregister", cregister_methods);
}

int main(){
while (1){
PyObject *arglist;
arglist = Py_BuildValue("(si)", "c code", 5);
Py_XINCREF(pythonHandler);
Py_XINCREF(arglist);
PyEval_CallObject(pythonHandler, arglist);
Py_XDECREF(pythonHandler);
Py_XDECREF(arglist);
sleep(1);
}
}

Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0xb7c1c6b0 (LWP 13699)]
0xb7e0bcfb in PyInt_FromLong () from /usr/lib/libpython2.5.so.1.0
(gdb) bt
#0  0xb7e0bcfb in PyInt_FromLong () from /usr/lib/libpython2.5.so.1.0
#1  0xb7e8cae6 in ?? () from /usr/lib/libpython2.5.so.1.0
#2  0x0005 in ?? ()
#3  0x0006 in ?? ()
#4  0xbf89a378 in ?? ()
#5  0xb7e9d095 in _PyObject_GC_Malloc () from /usr/lib/libpython2.5.so.1.0
#6  0xb7e8d1dd in ?? () from /usr/lib/libpython2.5.so.1.0
#7  0x0002 in ?? ()
#8  0x08048320 in ?? ()
#9  0x72d6dafa in ?? ()
#10 0x0029 in ?? ()
#11 0xbf89a474 in ?? ()
#12 0xbf89a478 in ?? ()
#13 0x in ?? ()



  

--
http://mail.python.org/mailman/listinfo/python-list


embedding and extending python C API registering callback handler objects

2008-06-27 Thread Tim Spens
Hello all,

I've been trying to get an example found here 
http://codeidol.com/python/python3/Embedding-Python/Registering-Callback-Handler-Objects/
 
to work.  Every thing works fine except when I try to trigger an event from c 
that will call a python function.  Here is my test code:

//---python code--//
#! /usr/bin/env python
import time
import callback

def callback1(label,count):
print 'callback1 successfully triggered from python via callback.so'
return 'callback1 => %s number %i' % (label, count)

def callback2(label,count):
return 'callback2 => ' +  label * count

print '\nTest1:'
callback.setHandler(callback1)
callback.triggerEvent() # simulate events caught by C layer

print '\nTest2:'
callback.setHandler(callback2)

print 'Waiting for callback2 to be called from c:'
while 1:
time.sleep(.001)

//---c code---//
#include 
#include 

/* 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, count);
pres = PyEval_CallObject(Handler, args);
Py_DECREF(args);
if (pres != NULL){
/* use and decref handler result */
PyArg_Parse(pres, "s", &cres);
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 = PyObject_CallObject(function, args);
Py_XDECREF(args);
return val;
}

static PyObject *
Register_Handler(PyObject *self, PyObject *args){
/* save Python callable object */
Py_XDECREF(Handler);
PyArg_Parse(args, "O", &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",Register_Handler},   /* name, address */
{"triggerEvent",  Trigger_Event},
{NULL, NULL}
};
  /* on first "import callback" */
void initcallback(){  /* this is called by Python  */
(void) Py_InitModule("callback", callback_methods);
}

int main(){
while (1){
printf("1\n");
//attempting to call callback2 which is registered to Handler
//i've also tried args = Py_BuildValue("(si)", label, count); 
here but I get a segfault.
PyObject *args = Py_BuildValue("s","c code");
printf("2\n");
PyObject* val = make_call(Handler,args);
printf("3\n");
Py_XDECREF (val);
printf("4\n");
sleep(1);
}}

//compiler stuff--//
gcc callback.c -c -g -Wall -fpic -I /usr/include/python2.5 -o callback.o
gcc callback.c -g -Wall -I /usr/include/python2.5 -L /usr/local/lib -lpython2.5 
-o callback
gcc -shared -Wall callback.o -o callback.so

//test code results---//
../callback.py 
Test1:
callback1 successfully triggered from python via callback.so
callback1 => spam number 0

Test2:
Waiting for callback2 to be called from c:
#NOTHING EVER GETS PRINTED HERE CALLBACK NEVER GETS CALLED?

../callback
1
2
3
4


Thanks,
Tim


  

--
http://mail.python.org/mailman/listinfo/python-list


Re: embedding and extending python C API registering callback handler objects

2008-06-27 Thread Tim Spens
thanks, but didn't fix the problem.


--- On Fri, 6/27/08, Matimus <[EMAIL PROTECTED]> wrote:

> From: Matimus <[EMAIL PROTECTED]>
> Subject: Re: embedding and extending python C API registering callback 
> handler objects
> To: [email protected]
> Date: Friday, June 27, 2008, 9:03 AM
> On Jun 27, 8:22 am, Tim Spens <[EMAIL PROTECTED]>
> wrote:
> > Hello all,
> >
> > I've been trying to get an example found
> herehttp://codeidol.com/python/python3/Embedding-Python/Registering-Callb...
> > to work.  Every thing works fine except when I try to
> trigger an event from c that will call a python function.
>  Here is my test code:
> >
> > //---python
> code--//
> > #! /usr/bin/env python
> > import time
> > import callback
> >
> > def callback1(label,count):
> > print 'callback1 successfully
> triggered from python via callback.so'
> > return 'callback1 => %s number
> %i' % (label, count)
> >
> > def callback2(label,count):
> > return 'callback2 => ' +
>  label * count
> >
> > print '\nTest1:'
> > callback.setHandler(callback1)
> > callback.triggerEvent() # simulate events
> caught by C layer
> >
> > print '\nTest2:'
> > callback.setHandler(callback2)
> >
> > print 'Waiting for callback2 to be called from
> c:'
> > while 1:
> > time.sleep(.001)
> >
> > //---c
> code---//
> > #include 
> > #include 
> >
> > /* 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,
> count);
> > pres = PyEval_CallObject(Handler, args);
> > Py_DECREF(args);
> > if (pres != NULL){
> > /* use and decref handler result */
> > PyArg_Parse(pres, "s",
> &cres);
> > 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 = PyObject_CallObject(function,
> args);
> > Py_XDECREF(args);
> > return val;
> >
> > }
> >
> > static PyObject *
> > Register_Handler(PyObject *self, PyObject *args){
> > /* save Python callable object */
> > Py_XDECREF(Handler);
> > PyArg_Parse(args, "O", &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",Register_Handler},
>   /* name, address */
> > {"triggerEvent",  Trigger_Event},
> > {NULL, NULL}};
> >
> >
>   /* on first "import callback" */
> > void initcallback(){  /* this
> is called by Python  */
> > (void) Py_InitModule("callback",
> callback_methods);
> >
> > }
> >
> > int main(){
> > while (1){
> > printf("1\n");
> > //attempting to call callback2
> which is registered to Handler
> > //i've also tried args =
> Py_BuildValue("(si)", label, count); here but I
> get a segfault.
> > PyObject *args =
> Py_BuildValue("s","c code");
> > printf("2\n");
> > PyObject* val =
> make_call(Handler,args);
> > printf("3\n");
> > Py_XDECREF (val);
> > printf("4\n");
> > sleep(1);
> >
> > }}
> >
> > //compiler
> stuff--//
> > gcc callback.c -c -g -Wall -fpic -I
> /usr/include/python2.5 -o callback.o
> > gcc callback.c -g -Wall -I /usr/include/python2.5 -L
> /usr/local/lib -lpython2.5 -o callback
> > gcc -shared -Wall callback.o -o callback.so
> >
> > //test code
> results---//
> > ../callback.py
> > Test1:
> > callback1 successfully triggered from python via
> callback.so
> > callback1 => spam number 0
> >
> > Test2:
> > Waiting for callback2 to be called from c:
> > #NOTHING EVER GETS PRINTED HERE CALLBACK NEVER GETS
> CALLED?
> >
> > ../callback
> > 1
> > 2
> > 3
> > 4
> > 
> >
> > Thanks,
> > Tim
> 
> Maybe you just need to flush the stdout buffer in python.
> `sys.stdout.flush()`
> 
> Matt
> --
> http://mail.python.org/mailman/listinfo/python-list


  

--
http://mail.python.org/mailman/listinfo/python-list


Re: embedding and extending python C API registering callback handler objects

2008-06-27 Thread Tim Spens



--- On Fri, 6/27/08, Tim Spens <[EMAIL PROTECTED]> wrote:

> From: Tim Spens <[EMAIL PROTECTED]>
> Subject: Re: embedding and extending python C API registering callback 
> handler objects
> To: [email protected], "Matimus" <[EMAIL PROTECTED]>
> Date: Friday, June 27, 2008, 9:16 AM
> thanks, but didn't fix the problem.
> 
> 
> --- On Fri, 6/27/08, Matimus <[EMAIL PROTECTED]>
> wrote:
> 
> > From: Matimus <[EMAIL PROTECTED]>
> > Subject: Re: embedding and extending python C API
> registering callback handler objects
> > To: [email protected]
> > Date: Friday, June 27, 2008, 9:03 AM
> > On Jun 27, 8:22 am, Tim Spens
> <[EMAIL PROTECTED]>
> > wrote:
> > > Hello all,
> > >
> > > I've been trying to get an example found
> >
> herehttp://codeidol.com/python/python3/Embedding-Python/Registering-Callb...
> > > to work.  Every thing works fine except when I
> try to
> > trigger an event from c that will call a python
> function.
> >  Here is my test code:
> > >
> > > //---python
> > code--//
> > > #! /usr/bin/env python
> > > import time
> > > import callback
> > >
> > > def callback1(label,count):
> > > print 'callback1 successfully
> > triggered from python via callback.so'
> > > return 'callback1 => %s number
> > %i' % (label, count)
> > >
> > > def callback2(label,count):
> > > return 'callback2 => ' +
> >  label * count
> > >
> > > print '\nTest1:'
> > > callback.setHandler(callback1)
> > > callback.triggerEvent() # simulate events
> > caught by C layer
> > >
> > > print '\nTest2:'
> > > callback.setHandler(callback2)
> > >
> > > print 'Waiting for callback2 to be called
> from
> > c:'
> > > while 1:
> > > time.sleep(.001)
> > >
> > > //---c
> > code---//
> > > #include 
> > > #include 
> > >
> > > /* 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,
> > count);
> > > pres = PyEval_CallObject(Handler, args);
> > > Py_DECREF(args);
> > > if (pres != NULL){
> > > /* use and decref handler result */
> > > PyArg_Parse(pres, "s",
> > &cres);
> > > 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 =
> PyObject_CallObject(function,
> > args);
> > > Py_XDECREF(args);
> > > return val;
> > >
> > > }
> > >
> > > static PyObject *
> > > Register_Handler(PyObject *self, PyObject *args){
> > > /* save Python callable object */
> > > Py_XDECREF(Handler);
> > > PyArg_Parse(args, "O",
> &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",   
> Register_Handler},
> >   /* name, address */
> > > {"triggerEvent",  Trigger_Event},
> > > {NULL, NULL}};
> > >
> > >
> >   /* on first "import callback" */
> > > void initcallback(){  /* this
> > is called by Python  */
> > > (void) Py_InitModule("callback",
> > callback_methods);
> > >