[C++-sig] Howto expose exception classes using boost.python?

Ryan Gallagher ryan.gallagher at gmail.com
Mon May 8 23:34:37 CEST 2006


David Abrahams <dave <at> boost-consulting.com> writes:

> Officially, it does require that exceptions be derived from
> PyExc_Exception (I don't remember where that's documented), but
> unofficially, you can throw anything :)
> 

Does this really work in practice though?  (With classes exposed through 
Boost.Python at least?) 

I was also working on this problem several months back, translating an 
exception class I exposed through Boost.Python using class_<> to raise an 
instance of the python class.  (I had this all wrapped in an exception_<> 
wrapper which was nice.)  This seemed to work fine if I caught the exact class
type, however, it did not work if I tried to catch a base class or nothing in 
the exception list matched the exception that was raised.  The python 
interpreter would always give me a stack corruption(?) in this case.  
(Windows python 2.4.1 I believe.)

Sorry for the total lack of concrete example code.  I had also tried many
different approaches on this and don't remember which worked and which 
didn't.  I'll dig up my old project that I was using for experimenting with 
this and post it tomorrow along with the exact error I saw in python.  

In the end I think I just wrote a python exception class and translated the 
C++ exception to raise it.  Not ideal though.  

-Ryan





More information about the Cplusplus-sig mailing list