[python] intercepting uncaught exceptions
Hi, I am using Boost.Python quite extensively (even stretching it to the limit in some cases). Now I cave this problem: Is there any way to intercept uncaught exception in embedded Python code? I need to add some information to an exception/traceback in this case. I have full control over environment before embedded code is executed. I thought I'll be able to use sys.excepthook, but it does not seem to be invoked. Alternatively I'd can live if I can somehow retrieve the context of failed call (global/local dict?) in boost::python::error_already_set handler. Please advise, Gennadiy
On 28.2.2011 19:32, Gennadiy Rozental wrote:
I am using Boost.Python quite extensively (even stretching it to the limit in some cases). Now I cave this problem:
Is there any way to intercept uncaught exception in embedded Python code?
I need to add some information to an exception/traceback in this case. I have full control over environment before embedded code is executed.
I thought I'll be able to use sys.excepthook, but it does not seem to be invoked.
Alternatively I'd can live if I can somehow retrieve the context of failed call (global/local dict?) in boost::python::error_already_set handler.
Boost.Python throws error_already_set exception in case PyErr_Occurred() is true, and here PyErr functions kick in. Perhaps PyErr_Fetch() and PyErr_Restore() are what you're looking for? http://docs.python.org/py3k/c-api/exceptions.html HTH
Juraj Ivančić
Boost.Python throws error_already_set exception in case PyErr_Occurred() is true, and here PyErr functions kick in. Perhaps PyErr_Fetch() and PyErr_Restore() are what you're looking for?
These I am using already. The problem is that I am running different pieces of embedded code in different "execution environments" with different global and local dictionaries associated with different Boost.Python object being executed. I need an ability to treat an error slightly differently depending on which "environment" an error occurred in. Thus I need to know what was the global dictionary at the time (or something similar identifying the "environment"). I hoped I can attach something to the traceback object, but I need to somehow intercept it. Gennadiy
On Tue, Mar 1, 2011 at 2:13 AM, Gennadiy Rozental
Juraj Ivančić
writes: Boost.Python throws error_already_set exception in case PyErr_Occurred() is true, and here PyErr functions kick in. Perhaps PyErr_Fetch() and PyErr_Restore() are what you're looking for?
These I am using already. The problem is that I am running different pieces of embedded code in different "execution environments" with different global and local dictionaries associated with different Boost.Python object being executed.
I need an ability to treat an error slightly differently depending on which "environment" an error occurred in. Thus I need to know what was the global dictionary at the time (or something similar identifying the "environment").
I hoped I can attach something to the traceback object, but I need to somehow intercept it.
This sounds like a general python/C question; the fact that Boost.Python is throwing an exception is a minor detail in the picture that doesn't affect the answer. You might try asking on the regular Python user's list. -- Dave Abrahams BoostPro Computing http://www.boostpro.com
Dave Abrahams
This sounds like a general python/C question; the fact that Boost.Python is throwing an exception is a minor detail in the picture that doesn't affect the answer. You might try asking on the regular Python user's list.
Actually I wondered if Boost.Python doing something to prevent sys.excepthook from getting invoked. How does it produce error_already_set? Also I did ask in c++sig - no love there (not sure if many people reading that list at all) I can take any hint. One using pure Python solution or the one using one based on Boost.Python or Python C API. I guess I can try some Python list as well.
On Tue, Mar 1, 2011 at 1:30 PM, Gennadiy Rozental
Dave Abrahams
writes: This sounds like a general python/C question; the fact that Boost.Python is throwing an exception is a minor detail in the picture that doesn't affect the answer. You might try asking on the regular Python user's list.
Actually I wondered if Boost.Python doing something to prevent sys.excepthook from getting invoked.
I don't even know what that is.
How does it produce error_already_set?
By looking at the return code of Python functions it calls and throwing when it sees that there's an error. I.e., manually. -- Dave Abrahams BoostPro Computing http://www.boostpro.com
participants (3)
-
Dave Abrahams
-
Gennadiy Rozental
-
Juraj Ivančić