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 );
} 4 2740
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
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.
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
here are modest; I only need some sort of asyncronous notification of
VB from my Python COM...
|
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 with is the ease with which I can explore a idea without overplanning and committing to it (during...
|
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 for info. regarding Python, PIL, and
scanners but haven't seen anything related to what I'm looking...
|
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'
|
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 just to deal with PDF files. What do
others do???
| |
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 subsequently
distributed.
What I want to do is have a secure system that lets users log in upon
|
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).
|
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?
--
----------------------------------------------------------------------------
|
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 them.
My problem comes when I have a bug in my program or I don't close the serial object in the...
|
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...
|
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,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |