473,661 Members | 2,457 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4073
In article <OK************ *****@news20.be llglobal.com>, Pierre Rouleau
<pr************ ********@impath networks.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_CallObje ct(callback, arglist);
Py_DECREF(argli st);
if(result){
Py_DECREF(resul t);
/*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(myfun c)
#v-

C code:

#v+
PyObject* register_callba ck(FooType* self, PyObject* args, PyObject**
kwargs)
{
/* ParseTupleAndKe ywords */
/* PyCallable_Chec k */
self->callback = ...
}

....
function_result = PyObject_CallOb ject(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
6627
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, "ss", &key, &value)) return NULL; // do something with the C++ class here
4
2319
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 notify the views that the model has been updated??
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?
3
3371
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 call the callback from the parent process, it works fine. The PyObject is static, and holds the same value in both Parent and thread, so I'm at a loss as to what would be different in the pthread from the parent that would cause a segfault on the...
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
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...
267
10680
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
872
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. i've removed all the type error handling and simplified the code to hopefully show a minimum amount of code. when only one request is hitting the server at a time this works correctly even at fast speeds. but as soon as a second request is made...
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...
6
2008
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 callback is created and the rest are passed when the function is called. example: callback = Callback(function, x=1, y) callback(z, t=4, u)
0
8428
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
8341
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8754
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
8542
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
7362
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4177
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2760
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
1984
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1740
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.