[C++-sig] Handling Python exceptions from an exposed C++ function

Francesco Biscani bluescarni at gmail.com
Sun Nov 16 20:47:38 CET 2014


Hello Jim,

On 16 November 2014 20:23, Jim Bosch <jbosch at astro.princeton.edu> wrote:

> On Sun, Nov 16, 2014 at 1:51 PM, Francesco Biscani <bluescarni at gmail.com>
> wrote:
>
>>
>> This can result in an exception being thrown from Python (e.g.,
>> comparison of a numpy array with zero). What happens in this case is that
>> a bp::error_already_set is thrown and not caught by anything, and the
>> execution terminates.
>>
>> I can catch the error_already_set in the comparison operator, print
>> information about the Python exception that was thrown, etc. but what I
>> clearly need to do here is to stop the execution of the program and somehow
>> get back to the Python interpreter and translate the exception.
>>
>>
> bp::error_already_set should actually already be doing exactly what you
> want; the reason it exists is so Boost.Python can catch it and translate it
> into a Python exception, and any Boost.Python-wrapped function that throws
> error_already_set should have it caught and translated without having to do
> anything special.
>
> That means your problem is actually one of debugging why that's not
> working properly.  There have been some reports of problems with
> error_already_set in cases where some C++ compilers/linkers don't recognize
> an exception as being the same across dynamic library boundaries.  I'm not
> familiar with the details, but that's my best guess as to what's going on.
> Hopefully that will provide you a starting point for debugging and googling
> for solutions (or maybe someone else on this list can help diagnose it).
>

Thanks for the pointers, I guess I will keep on doing some experiments
trying to understand exactly what is going on. I have a Python 3 install
available as well, maybe I can try to see if anything changes there.

Cheers,

  Francesco.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20141116/78634849/attachment-0001.html>


More information about the Cplusplus-sig mailing list