472,958 Members | 2,188 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

Re: embedding and extending python C API registering callback handlerobjects


--- On Fri, 6/27/08, Tim Spens <t_*****@yahoo.comwrote:
From: Tim Spens <t_*****@yahoo.com>
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>
Subject: Re: embedding and extending python C API
registering callback handler objects
To: py*********@python.org
Date: Friday, June 27, 2008, 9:03 AM
On Jun 27, 8:22 am, Tim Spens
<t_sp...@yahoo.com>
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 <Python.h>
#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,
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
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?

-Tim


Jun 27 '08 #1
0 2037

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: jordi | last post by:
Hi, I'm starting to use Python embedded in a C program. I'm using Python to execute several scripts using as a variables information retrieved for several multithread "agents" written in C. ...
4
by: Alicia Haumann | last post by:
I accidentally sent this to webmaster@python.org, so this could be a duplicate if "webmaster" forwards it to this list. :{ Hi, there. Thanks for any help that can be offered. I've been...
2
by: Roose | last post by:
With some googling I have found these resources: http://docs.python.org/ext/win-dlls.html http://www.python.org/doc/faq/windows.html I have a large Win32/MFC/C/C++ application that has an...
1
by: Tommy Nordgren | last post by:
I want to write an application that embeds and extends (at least) the Python and Perl interpreters. Now i want to find as much as possible about the Python tools used for extending and embedding...
3
by: Marco Meoni | last post by:
Hi all! I've a problem with a C++ class that has to be included in a python application. One way to do it is Extending and Embedding the Python Interpreter Now i have 2 questions 1) Is there a...
1
by: jeremito | last post by:
I am trying to learn how to extend and/or embed Python. I have looked at the document "Extending and Embedding the Python Interpreter" and also "Python/C API Reference Manual. In the examples...
6
by: Qun Cao | last post by:
Hi Everyone, I am a beginner on cross language development. My problem at hand is to build a python interface for a C++ application built on top of a 3D game engine. The purpose of this python...
3
by: anonymisiert85 | last post by:
At the moment i can run python-string-code from C (MinGW, WinXP) But how can i register a C-function in python-RUNTIME and call this C function from python - without wrapper dll's or libs??? ...
1
by: Thomas Troeger | last post by:
Dear all, I've successfully embedded the Python interpreter into a set of C/C++ application programs that use a larger library project with information from http://docs.python.org/api/api.html...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.