[C++-sig] Re: Hnadling Python exceptions in C++

Berthold Höllmann hoel at germanlloyd.org
Fri Jul 19 17:49:31 CEST 2002


"David Abrahams" <david.abrahams at rcn.com> writes:

> From: "Berthold Hllmann" <hoel at germanlloyd.org>
> 
> 
> > OK, after thinking a bit about such a facility here is my idea. It
> > would be nice to have a C++ exception class that reads out
PyErr_Fetch
> > when instantiated and can be converted back into a Python exception
if
> > needed. I tried to start an implementation.
> 
> Hi Berthold,
> 
> Before posting an implementation, could you please explain the
use-case
> you're trying to cover, show the interface, and how it would be used?
> 
> > The attached code is
> > untested, it is just posted to share the idea.
> 
> Please don't use tabs when posting code; as you can see they
disappear.
> 

OK, I dove a little more into the boost python sources. One thing I
did not understand was the purpose of the function
"throw_error_already_set()". Why this indirection instead of calling
"throw error_already_set()"? But anyway, it would be nice to have a
facility to extract the error ocured when error_already_set was
thrown. Some access to the reason a command failed. This is where the
proposed class "error_handler " comes into play. It reads the Python
error state. It has a method "get_type" to get access to the python
exception thrown and it has an operator to put the exception
informations onto a stream. Calling the () operator will set back the
exception status and returns NULL, ready to be returned to Python:

try {
  boost::python::api::object
     fails(boost::python::api::import_import("does.not.exist");
} catch (error_already_set) {
  boost::python::error::error_handler err;
  std::cerr<<err;
  return err();
}

My proposed first interface would look like this:

--8<------------------------schnipp------------------------->8---
#include <Python.h>
namespace boost { namespace python { namespace Error {
  class error_handler {
  public:
    error_handler();
    ~error_handler();
    object get_type();
    PyObject *error_handler::operator() (); // always returns NULL
    virtual void printOn(std::ostream&) const;
  private:
    PyObject *type, *value, *traceback;
  };
  std::ostream& operator<<(std::ostream& os,
                           const error_handler& el);
} } }
--8<------------------------schnapp------------------------->8---

It's time for weekend now

Greetings

Berthold
-- 
Dipl.-Ing. Berthold H llmann   __   Address:
hoel at germanlloyd.org        G /  \ L Germanischer Lloyd
phone: +49-40-36149-7374    -+----+- Vorsetzen 32/35    P.O.Box 111606
fax  : +49-40-36149-7320      \__/   D-20459 Hamburg    D-20416 Hamburg



This email contains confidential information for the exclusive attention
of the intended addressee. Any access of third parties to this email is
unauthorized. Any use of this email by not intended recipients like
copying, distribution, disclosure etc. is prohibited and may be
unlawful. When addressed to our clients the content of this email is
subject to the General Terms and Conditions of GL's Group of Companies
applicable at the date of this email. 

GL's Group of Companies does not warrant and/or guarantee that this
message at the moment of receipt is authentic, correct and its
communication free of errors, interruption etc. 





More information about the Cplusplus-sig mailing list