[C++-sig] Re: Wrapper for exception translation

Gottfried.Ganssauge at HAUFE.DE Gottfried.Ganssauge at HAUFE.DE
Thu Jul 10 13:59:07 CEST 2003


...
> 
> This looks like a question about integration with Boost.Python
> itself to me.  Isn't it?
You're right, of course
What I meant to say was:
- I would be delighted to have this facility integrated with the library
- I would be even more delighted if it was usable through some pyste
enhancement

> 
> Could you explain how your facility improves on what's already in the
> library?  I'm sorry to be dense, but the answer's not jumping out at
> me.
Currently, in order to wrap user defined exceptions you need to
- allocate a python exception using PyErr_NewException
  This function needs to be parametrized using a specific naming convention
  you also need to find a place where this exception instance lives and is
  accessible to the exception translator
- define an exception translator which signals the exception defined above
- register the exception translator with the library

My facility allows to do this with only one function call
"exception_<Exception type> (name)" within the module init function.
It will allocate a python exception and store it within an instance of the
translated_exception_handler<> class. The name for that exception is taken
from the name parameter and the current scope according to the naming
convention documented in the manual page for PyErr_NewException.
A reference to the translated_exception_handler is stored within the class
dictionary of the newly allocated python exception in order to keep the
translator alive.
The translated_exception_handler provides the exception translator to be
registered with the library. It uses a generic method to obtain an exception
description by calling a function get_exception_description<>() which has a
default implementation in the detail namespace.
Finally the python exception itself is made available in the module's
namespace under the name given.

get_exception_description<>() currently returns a const char * which is
passed to PyErr_SetString(). It might be worthwile to make it return an
object which then could be passed to PyErr_SetObject(). This modification
would make it possible to pass an instance of a class which wraps the
exception caught, which again would make the exception instance itself
available to python programs.

Another enhancement could be the generation of derived python exceptions, an
area in which I have no experience yet. This could be by an additional
template parameter to the exception_<>() function which defaults to the
allocation with PyErr_NewException().

What do you think?

> 
> -- 
> Dave Abrahams
> Boost Consulting
> www.boost-consulting.com

Cheers,

Gottfried
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20030710/ac250778/attachment.htm>


More information about the Cplusplus-sig mailing list