473,659 Members | 2,666 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

simplest c python api callback example

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.setpy thonHandler(cal lback1)

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 <Python.h>
#include <stdlib.h>

/*************** *************** *************** **/
/* 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_CallObje ct(pythonHandle r, args);
}

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

//makes these functions available by importing cregister in python
static struct PyMethodDef cregister_metho ds[] = {
{"setpythonHand ler", Register_python Handler},
{NULL, NULL}
};

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

int main(){
while (1){
PyObject *arglist;
arglist = Py_BuildValue(" (si)", "c code", 5);
Py_XINCREF(pyth onHandler);
Py_XINCREF(argl ist);
PyEval_CallObje ct(pythonHandle r, arglist);
Py_XDECREF(pyth onHandler);
Py_XDECREF(argl ist);
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 0x00000005 in ?? ()
#3 0x00000006 in ?? ()
#4 0xbf89a378 in ?? ()
#5 0xb7e9d095 in _PyObject_GC_Ma lloc () from /usr/lib/libpython2.5.so .1.0
#6 0xb7e8d1dd in ?? () from /usr/lib/libpython2.5.so .1.0
#7 0x00000002 in ?? ()
#8 0x08048320 in ?? ()
#9 0x72d6dafa in ?? ()
#10 0x00000029 in ?? ()
#11 0xbf89a474 in ?? ()
#12 0xbf89a478 in ?? ()
#13 0x00000000 in ?? ()

Jun 27 '08 #1
0 1108

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

Similar topics

30
3436
by: Christian Seberino | last post by:
How does Ruby compare to Python?? How good is DESIGN of Ruby compared to Python? Python's design is godly. I'm wondering if Ruby's is godly too. I've heard it has solid OOP design but then I've also heard there are lots of weird ways to do some things kinda like Perl which is bad for me. Any other ideas?
4
2729
by: Randall Hopper | last post by:
What is the correct way to propagate exceptions from Python callbacks? When I do this: Python -> C++ -> Python Callback (example attached) an exception raised in the callback doesn't make it back across C++ to Python. It appears that PyGILState_Release() at the bottom of the callback
0
2708
by: Bill Davy | last post by:
I am working with MSVC6 on Windows XP. I have created an MSVC project called SHIP I have a file SHIP.i with "%module SHIP" as the first line (file is below). I run SHIP.i through SWIG 1.3.24 to obtain SHIP_wrap.cpp and SHIP.py; the latter contains the line "import _SHIP". I compile SHIP_wrap.cpp and a bunch of files into a DLL which I have the
5
4171
by: Francois De Serres | last post by:
Hiho, could somebody please enlighten me about the mechanics of C callbacks to Python? My domain is more specifically callbacks from the win32 API, but I'm not sure that's where the problem lies. Here's a description... I want a callback-based MIDI input/processing, so PortMidi was not an alternative. I have written a C extension module that links to the mmsys MIDI API. I separated the win32-dependant code from the Python extension...
4
1921
by: davidstummer | last post by:
I was wondering if anyone could point me to an example. Currently i have a c++ program which calls and c++ dll (i created both). The dll uses SendMessage to pass messages back to the calling .exe, and the ..exe process the messages in it's Windows Procedure function WindProc(). What i want is to receive these messages ( the contents of each message will be plain text), in python using a callback (i think this is what i need).
0
1795
by: Robert | last post by:
After failing on a yield/iterator-continuation problem in Python (see below) I tried the Ruby (1.8.2) language first time on that construct: The example tries to convert a block callback interface (Net::FTP.retrbinary) into a read()-like iterator function in order to virtualize the existing FTP class as kind of file system. 4 bytes max per read in this first simple test below. But it fails on the second continuation with ThreadError after...
267
10679
by: Xah Lee | last post by:
Python, Lambda, and Guido van Rossum Xah Lee, 2006-05-05 In this post, i'd like to deconstruct one of Guido's recent blog about lambda in Python. In Guido's blog written in 2006-02-10 at http://www.artima.com/weblogs/viewpost.jsp?thread=147358
0
2106
by: Tim Spens | last post by:
--- On Fri, 6/27/08, Tim Spens <t_spens@yahoo.comwrote: 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...
0
8427
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8746
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8523
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8626
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6178
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4334
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2749
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1975
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1737
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.