473,549 Members | 2,243 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Python callbacks & PyGILState_Rele ase()

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_Rele ase() at the bottom of the callback
wrapper is resetting the error state (as I can set a global based on
PyErr_Occurred( ) there, and can catch that up in the exception handler in
Python).

This obviously isn't correct. What should I be doing?

Thanks,

Randall
------------------------------------------------------------------------------
void callback_wrappe r( void *user_data )
{
// Acquire interpreter lock
PyGILState_STAT E gstate = PyGILState_Ensu re();
...
// Call Python
pyresult = PyEval_CallObje ct( pyfunc, pyargs );
...
/*********** At this point, PyErr_Occurred( ) is true **************/
/****** But it's not true when we return through C++ to Python ******/

// Free interpreter lock
PyGILState_Rele ase(gstate);
}

void registerCallbac k( PyObject *pyfunc )
{
...
// Ensure threads inited, so we can use Ensure/Release in callbacks
PyEval_InitThre ads();

// Ref pyfunc
Py_INCREF( pyfunc );

// Call underlying C++ method, registering the above C++ callback
realRegisterCal lback( callback_wrappe r, pyfunc );
}
Jul 19 '05 #1
4 2724
Randall Hopper <vi****@charter .net> writes:
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_Rele ase() at the bottom of the callback
wrapper is resetting the error state (as I can set a global based on
PyErr_Occurred( ) there, and can catch that up in the exception handler in
Python).

This obviously isn't correct. What should I be doing?

Thanks,

Randall
------------------------------------------------------------------------------
void callback_wrappe r( void *user_data )
{
// Acquire interpreter lock
PyGILState_STAT E gstate = PyGILState_Ensu re();
...
// Call Python
pyresult = PyEval_CallObje ct( pyfunc, pyargs );
...
/*********** At this point, PyErr_Occurred( ) is true **************/
/****** But it's not true when we return through C++ to Python ******/
if (pyresult == NULL)
PyErr_Print();
// Free interpreter lock
PyGILState_Rele ase(gstate);
}


PyErr_Print() will do the 'right' thing´s.

Thomas
Jul 19 '05 #2
Thomas Heller:
|> Python -> C++ -> Python Callback
|>
|> (example attached) an exception raised in the callback doesn't make it back
|> across C++ to Python.
....
|> void callback_wrappe r( void *user_data )
|> {
|> // Acquire interpreter lock
|> PyGILState_STAT E gstate = PyGILState_Ensu re();
|> ...
|> // Call Python
|> pyresult = PyEval_CallObje ct( pyfunc, pyargs );
|> ...
|
| if (pyresult == NULL)
| PyErr_Print();
|
|> // Free interpreter lock
|> PyGILState_Rele ase(gstate);
|> }
|
|PyErr_Print() will do the 'right' thing?s.

Thanks for the reply. However, this won't:

a) Stop the main Python script, and
b) Print the full stack trace (including Python and C++ SWIG wrapper)

Is there a clean way to save the full exception state in the callback
before the PyGILState_Rele ase(), and restore it when we return across the
C++ wrapper?

If I knew what the proper "save" and "restore" exception state code bits
were, I could easily implement this with exception typemaps in SWIG.

Thanks,

Randall

P.S. Perhaps PyGILState_Rele ase should take an argument instructing it to
exclude exception state when resetting the interpreter state back to its
original state.
Jul 19 '05 #3
In article <ma************ *************** ***********@pyt hon.org>, Randall Hopper wrote:
Thomas Heller:
|> Python -> C++ -> Python Callback
|>
|> (example attached) an exception raised in the callback doesn't make it back
|> across C++ to Python.
...
|> void callback_wrappe r( void *user_data )
|> {
|> // Acquire interpreter lock
|> PyGILState_STAT E gstate = PyGILState_Ensu re();
|> ...
|> // Call Python
|> pyresult = PyEval_CallObje ct( pyfunc, pyargs );
|> ...
|
| if (pyresult == NULL)
| PyErr_Print();
|
|> // Free interpreter lock
|> PyGILState_Rele ase(gstate);
|> }
|
|PyErr_Print() will do the 'right' thing?s.

Thanks for the reply. However, this won't:

a) Stop the main Python script, and
b) Print the full stack trace (including Python and C++ SWIG wrapper)

Is there a clean way to save the full exception state in the callback
before the PyGILState_Rele ase(), and restore it when we return across the
C++ wrapper?

If I knew what the proper "save" and "restore" exception state code bits
were, I could easily implement this with exception typemaps in SWIG.

Thanks,

Randall

P.S. Perhaps PyGILState_Rele ase should take an argument instructing it to
exclude exception state when resetting the interpreter state back to its
original state.


Randall:

It's not the job of the PyGILState_* functions to manage exception details for you.

I always solved this problem a different way, by saving the exception in an instance variable
within the Python callback, and using a condition variable so that the main thread could abort on a callback's
failure. Notably, our callsbacks are always invoked from a C++ thread that the main Python interepreter didn't
create, and our main Python intepreter is blocked on a mutex most of the time.

I saved the exception state by retrieveing it from sys.exc_info(), which contains all the traceback object data as Python variable.
I think you can get the same info from your C++ callback wrapper, and use PyErr_Fetch and PyErr_Restore to save and restore the
exception sate.

Dave

Jul 19 '05 #4
David E. Konerding DSD staff:
|Randall Hopper wrote:
|> Is there a clean way to save the full exception state in the callback
|> before the PyGILState_Rele ase(), and restore it when we return across the
|> C++ wrapper?
....
|I saved the exception state by retrieveing it from sys.exc_info(), which
|contains all the traceback object data as Python variable. I think you can
|get the same info from your C++ callback wrapper, and use PyErr_Fetch and
|PyErr_Restore to save and restore the exception sate.

Ok, thanks. I'll give this a shot!

Randy
Jul 19 '05 #5

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

Similar topics

0
987
by: jahurt | last post by:
I've written a COM server in Python for use with a VB app. So far things work very well... I'm able to call all my Python functions from VB with no problems. However, now I need to notify the VB client when data is available in the server. I've been trying to implement a callback function in the server, but I haven't had any luck. My needs...
2
2073
by: Eric | last post by:
Greetings Comrades, Pythonistas! I am looking for guidance on the quick and easiest path to set up a Python wiki. A wiki application idea popped into my head while I was making morning coffee. It's not what I should be thinking about, but I hate to let an interesting idea die an unexplored death. One of the aspects of Python I am enamored...
1
3572
by: S. David Rose | last post by:
Hello everyone. I am very much enjoying learning python. I am fiddling with a project that I've decided to use on the Win32 platform. I'd like to be able to scan a document as a graphic and save it thru Python to a file. I believe that on Windows, all scanning is done thru the TWAIN interface. I'd like to use a USB scanner. I've googled...
1
3711
by: Xah Lee | last post by:
suppose you want to do find & replace of string of all files in a directory. here's the code: ©# -*- coding: utf-8 -*- ©# Python © ©import os,sys © ©mydir= '/Users/t/web'
25
2410
by: rbt | last post by:
Are there any plans in the near future to support PDF files in Python as thoroughly and completely as Perl does? http://cpan.uwinnipeg.ca/search?query=pdf&mode=dist I love Python's clean syntax and ease of use, etc. But on some things (PDF for example) as barbaric as Perl's syntax is, it does outshine Python... I hate having to use Perl...
0
1190
by: flamesrock | last post by:
First, my problem doesn't make much practical sense so I hope you're up for a challenge. What I have (in concept) is a standalone web client that connects different 'players' to a central host and distributes game files between them. A user claims certain files, plays them, and then automatically uploads them to the site where they are...
4
1915
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...
9
1838
by: Tim Daneliuk | last post by:
So it is claimed: http://www.infoq.com/news/Scala--combing-the-best-of-Ruby-;jsessionid=CC7C8366455E67B04EE5864B7319F5EC Has anyone taken a look at this that can provide a meaningful contrast with Python? -- ----------------------------------------------------------------------------
1
2193
by: turtleRider | last post by:
Hi, I am new to Python and non-web programming. I am using pySerial in OSX 10.5.2 (Python 2.5) to access a bluetooth module that is mapped to a device file. I can access the device with screen /dev/tty.BTdevice 115200 and I correctly get a stream of 3 digit numbers (data from a sensor). I wrote a Python script to read the numbers and print...
0
7546
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...
0
7830
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...
0
6071
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...
1
5387
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...
0
5111
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3517
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...
0
3496
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1962
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
1
1082
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.