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

Berthold Höllmann hoel at germanlloyd.org
Fri Jul 19 16:43:50 CEST 2002


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

> In Boost.Python v1, there's no special facility. You can use the
regular
> Python 'C' API, of course. We are planning some support in
Boost.Python v2.
> I'd like to try to serve your needs; what kind of facilities did you
have
> in mind?
> 
> Thanks,
> Dave
> 
> > ----- Original Message -----
> > From: "Berthold Hllmann" <hoel at germanlloyd.org>
> > Hello,
> > 
> > Is there any code for handling Python exceptions in C++? I only
found
> > report_ignored_exception in classes.cpp, but it's in anonymous
> > namespace and though inaccessable in my code.
> > 
> > Greetings
> > 
> > Berthold

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. The attached code is
untested, it is just posted to share the idea.

except.hpp:
--8<------------------------schnipp------------------------->8---
#ifndef EXCEPT_BH20020719_H_
#define EXCEPT_BH20020719_H_

#include <Python.h>
#include <string>

namespace boost {
  namespace python {
    namespace Error {
      class PyErrHandler {
      public:
	PyErrHandler();
	virtual void printOn(std::ostream&) const;
      private:
	PyObject *stype, *svalue, *straceback;
      };
      std::ostream& operator<<(std::ostream& os,
			       const PyErrHandler& el);
    }
  }
}

#endif // EXCEPT_BH20020719_H_
--8<------------------------schnapp------------------------->8---

and except.cpp
--8<------------------------schnipp------------------------->8---
#include <iostream>
#include "except.hpp"

boost::python::Error::PyErrHandler::PyErrHandler()
{
  PyObject *t, *v, *tb;
  PyErr_Fetch(&t, &v, &tb);
  if (t != NULL)
    stype = PyObject_Str(t);
  else
    stype = PyString_FromString("");

  if (v != NULL)
    svalue = PyObject_Str(v);
  else
    svalue = PyString_FromString("");

  if (tb != NULL)
    straceback = PyObject_Repr(tb);
  else
    straceback = PyString_FromString("");

  Py_XDECREF(t);
  Py_XDECREF(v);
  Py_XDECREF(tb);
}

void boost::python::Error::PyErrHandler::printOn(std::ostream& strm)
const
{
  strm << std::string(PyString_AsString(stype)) << "\n"
       << std::string(PyString_AsString(svalue)) << "\n"
       << std::string(PyString_AsString(straceback));
}

PyObject *boost::python::Error::PyErrHandler::operator() ()
{
  PyErr_Restore(t, v, tb)
  return NULL;
}

std::ostream&
boost::python::Error::operator<<(std::ostream& os,
	   const boost::python::Error::PyErrHandler& el)
{
  el.printOn(os);
  return os;
}
--8<------------------------schnapp------------------------->8---

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