Python 2.2.1 C API, extension modules, Boost

cseymour at thrush.eos.ubc.ca cseymour at thrush.eos.ubc.ca
Mon Aug 19 19:22:24 EDT 2002


Here is a sample program which shows the problem i've been having 
(posted in an earlier message). When I import the module "sample" and 
run the function test(), I get the following output:

This is a test
Raising an error
Aborted

When I should get a message saying it caught the error, and then a 
Python error, instead of it Aborting and quitting Python.



/* sample.c */

#include "Python.h"
#include "boost/python/class_builder.hpp"

namespace py = boost::python;

void test()
{
  std::cout << "This is a test" << std::endl;
  std::cout << "Raising an error" << std::endl;

  PyErr_SetString(PyExc_TypeError, "This is the error");
  throw py::argument_error();
}

BOOST_PYTHON_MODULE_INIT(sample)
{
  try {
    py::module_builder this_module("sample");
    this_module.def(test, "test");
  }
  catch (...) {
    std::cout << "Caught an error" << std::endl;
    py::handle_exception();
  }
}



More information about the Python-list mailing list