[C++-sig] BPL v2 and Exceptions

Greg Landrum greglandrum at earthlink.net
Tue Oct 15 23:45:49 CEST 2002


[redhat 8.0, g++ 3.2, python 2.2.1]

Hi,

I'm in the process of porting some wrapped classes from BPL v1 to BPL v2 
(using Boost v1.29 and I've hit a major snag with exceptions causing crashes.

I've read through as much of the docs as I could make sense of as well as 
the last few months of this list and I haven't found much info that helps.

What I'm trying to do is throw python exceptions (e.g. IndexError) from my 
wrapped classes.

Here's the most simple-minded form of a module I can come up with:
//-------------------------------------------------------------------------
#include <boost/python.hpp>

#include <iostream>

namespace python = boost::python;

void throw_arg_error()
{
   std::cerr << "Ach!\n";
   python::throw_argument_error();
}

// A helper function for dealing with errors. Throw a Python ValueError
void throw_value_error(const std::string err)
{
   std::cerr << "Ach!\n";
   PyErr_SetString(PyExc_ValueError, err.c_str());
   python::throw_error_already_set();
}

void killme(){
   throw_arg_error();
}

BOOST_PYTHON_MODULE(except_test)
{
   python::def("throw_arg_error",throw_arg_error);
   python::def("throw_value_error",throw_value_error);
   python::def("killme",killme);
}
//-------------------------------------------------------------------------

when I build this module and try calling any of the three functions, python 
aborts.

Am I doing something horribly stupid, or is this just not ever going to work?

-greg
----
greg Landrum (greglandrum at earthlink.net)
Software Carpenter/Computational Chemist





More information about the Cplusplus-sig mailing list