[C++-sig] Boost.Python v2: C++->Python exception translation

David Abrahams dave at boost-consulting.com
Tue Aug 13 02:34:58 CEST 2002


I have just checked into Boost CVS some facilities for translating C++
exceptions to Python exceptions. See
libs/python/test/exception_translator.[cpp/py] for details.

The basic idiom is:

    register_exception_translator<CppExceptionType>(translate);

where the expression:

    translate(x)

is legal for an object x of type CppExceptionType, and is expected to call
PyErr_SetString or PyErr_SetObject to set up the Python exception.

Notes:

    1. This is obviously a somewhat minimalist implementation. We could
think about all sorts of additional nice facilities for creating new Python
exception types, for example.

    2. Some systems (Linux GCC 3.0.x in particular) have some pretty nasty
requirements in terms of exception-catching across shared library
boundaries. The issues are subtle, though you can read about them in the
threads referenced in the "Shared Libraries" section here:

http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/*checkout*/boost/boost/libs/
python/doc/v2/May2002.html#shared

My advice to you if you need to use exception translation on these systems
is to be sure that each place that throws a given C++ exception type and
that registers a translator for that type be located in the same shared
library.

Cheers,
Dave

-----------------------------------------------------------
           David Abrahams * Boost Consulting
dave at boost-consulting.com * http://www.boost-consulting.com







More information about the Cplusplus-sig mailing list