473,402 Members | 2,064 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

It is possible to pass a Python callback to C-extensions?

I have a GUI application written in Python (with WxPython) which uses
some low level classes also written in Python. These low level Python
classes are given a pointer to some GUI Python object. The low level
Python classes callback some of the methods of the GUI object. All
works fine.

Now I want to convert the low level Python classes to C++. I will be
using SWIG to create the marshaling so the top level GUI, written in
Python stays that way.

The problem: is it possible to continue using call backs? The C++ code
would need to call the Python methods back. Is there an easy way to do
this?

This is a little different from extending or embedding. I have a Python
program that uses Python extensions written in C++. This C++ code would
need to pass some information back to the Python code (and currently,
the all-round Python code uses call backs).

My quick review of the SWIG documentation did not help me find a
solution to this.

Thanks in advance for any help.

/Pierre

Jul 18 '05 #1
2 4067
In article <OK*****************@news20.bellglobal.com>, Pierre Rouleau
<pr********************@impathnetworks.com> writes
I have a GUI application written in Python (with WxPython) which uses
some low level classes also written in Python. These low level Python
classes are given a pointer to some GUI Python object. The low level
Python classes callback some of the methods of the GUI object. All
works fine.

Now I want to convert the low level Python classes to C++. I will be
using SWIG to create the marshaling so the top level GUI, written in
Python stays that way.

The problem: is it possible to continue using call backs? The C++ code
would need to call the Python methods back. Is there an easy way to do
this?
certainly you can call back into python, I have some typical code that
looks like

PyObject *callback;
........
PyObject *arglist;
PyObject *result;

arglist = Py_BuildValue("(s)",buf);
result = PyEval_CallObject(callback, arglist);
Py_DECREF(arglist);
if(result){
Py_DECREF(result);
/*success*/
}
else {
/*handle error*/
}

Of course you need to get the definition of callback somehow. But that
is easy via argument passing or obtaining a global function.
This is a little different from extending or embedding. I have a Python
program that uses Python extensions written in C++. This C++ code would
need to pass some information back to the Python code (and currently,
the all-round Python code uses call backs).

......Thanks in advance for any help.

/Pierre


--
Robin Becker
Jul 18 '05 #2
Sure. Explanation by example:

Python code:

#v+
def myfunc(): pass

foobar.register_callback(myfunc)
#v-

C code:

#v+
PyObject* register_callback(FooType* self, PyObject* args, PyObject**
kwargs)
{
/* ParseTupleAndKeywords */
/* PyCallable_Check */
self->callback = ...
}

....
function_result = PyObject_CallObject(callback, calling_args);
....
#v-

No idea about SWIG, but I do this a lot in the PySQLite code using
Python's raw C API, which you could use as an example:

http://cvs.sourceforge.net/cgi-bin/v...viewcvs-markup

HTH,

-- Gerhard

Jul 18 '05 #3

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

Similar topics

6
by: Harri Pesonen | last post by:
How do I pass the calling C++ class reference (or anything) to a callback? My code is: static PyObject* emb_Set(PyObject *self, PyObject *args) { char *key, *value; if(!PyArg_ParseTuple(args,...
4
by: ketulp_baroda | last post by:
Hi Does python support MVC architecture? Java has register & notify obsever methods in javax.util . Does python has these functions. If not then how to register the views with the models & how to...
30
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...
3
by: Travis Berg | last post by:
I'm running into a problem when trying to perform a callback to a Python function from a C extension. Specifically, the callback is being made by a pthread that seems to cause the problem. If I...
4
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...
5
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...
267
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...
0
by: Bryan | last post by:
hi, i have a multithreaded c server that calls process_method in a different c thread per each call. process_method calls a python function bar in module foo. function bar calls back into c. ...
0
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...
6
by: Alexandru Mosoi | last post by:
does anyone know a nice implementation of callbacks in python? i have issues mixing named & unamed parameters. i want build a callback over a function such that some parameters are passed when...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.