[C++-sig] bad exception conversion
Wichert Akkerman
wichert at wiggy.net
Wed Jun 13 12:31:46 CEST 2012
I have some glue code that calls a C++ function which can raise an
unsuitable_error exception. I have an exception translator which will
convert that to a more pythonic version (UnsuitableError), but when I
use that I get an "SystemError: 'finally' pops bad exception" error on
the python side. As far as I can see my code looks correct, but I
suspect I am overlooking something trivial somewhere.
using namespace boost::python;
class UnsuitableError : public std::exception {
public:
UnsuitableError() : reasons() { }
~UnsuitableError() throw() {}
boost::python::list reasons;
};
namespace {
PyObject *UnsuitableErrorType = NULL;
void translator(const unsuitable_error &e) {
std::list<const char*>::const_iterator i;
PyObject* unicode;
UnsuitableError error=UnsuitableError();
for (i=e.reasons.begin(); i!=e.reasons.end(); i++) {
unicode = PyUnicode_FromString(*i);
error.reasons.append(boost::python::object(boost::python::handle<>(unicode)));
}
boost::python::object exc(error);
PyErr_SetObject(UnsuitableErrorType, exc.ptr());
}
}
void export() {
object module(handle<>(borrowed(PyImport_AddModule("mypkg.article"))));
scope().attr("article")=module;
scope module_scope = module;
class_<UnsuitableError> UnsuitableErrorClass("UnsuitableError");
UnsuitableErrorClass.def_readonly("reasons",
&UnsuitableError::reasons);
UnsuitableErrorType=UnsuitableErrorClass.ptr();
register_exception_translator<unsuitable_error>(&translator);
}
More information about the Cplusplus-sig
mailing list