From 64fee605ec84d0923384f9a709d4471b6030862f Mon Sep 17 00:00:00 2001 From: Diane Trout Date: Thu, 30 Aug 2007 22:41:01 +0000 Subject: [PATCH] translate KeyError too unfortunately the template register_exception_handler had trouble with function overloading of the translate functions, so I needed to name each exception translator differently. --- py/module.cpp | 7 +++---- py/stl_container_adapter.cpp | 17 +++++++++++++++-- py/stl_container_adapter.hpp | 5 ++--- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/py/module.cpp b/py/module.cpp index 8456427..f9e6b4f 100644 --- a/py/module.cpp +++ b/py/module.cpp @@ -1,10 +1,10 @@ -#include #include "stl_container_adapter.hpp" -using namespace boost::python; +#include void export_alphabet(); void export_annot(); void export_annotation_colors(); +void export_container_exceptions(); void export_conserved_path(); void export_flps(); void export_glsequence(); @@ -16,8 +16,7 @@ void export_mussa_window(); BOOST_PYTHON_MODULE(mussa) { - boost::python::register_exception_translator(&translate); - + export_container_exceptions(); export_alphabet(); export_annot(); export_annotation_colors(); diff --git a/py/stl_container_adapter.cpp b/py/stl_container_adapter.cpp index 5135b44..ab1ab36 100644 --- a/py/stl_container_adapter.cpp +++ b/py/stl_container_adapter.cpp @@ -1,12 +1,25 @@ #include "stl_container_adapter.hpp" -void translate(IndexError const& e) +#include +using namespace boost::python; + +void translate_index_error(IndexError const& e) { PyErr_SetString(PyExc_IndexError, "Index out of range"); } -void translate(KeyError const& e) +void translate_key_error(KeyError const& e) { PyErr_SetString(PyExc_KeyError, "Key not found"); } +void export_container_exceptions() +{ + boost::python::register_exception_translator( + &translate_index_error + ); + + boost::python::register_exception_translator( + &translate_key_error + ); +} diff --git a/py/stl_container_adapter.hpp b/py/stl_container_adapter.hpp index a298909..f60c996 100644 --- a/py/stl_container_adapter.hpp +++ b/py/stl_container_adapter.hpp @@ -11,11 +11,10 @@ struct IndexError : std::exception { -public: explicit IndexError(): std::exception() {}; }; -void translate(IndexError const &e); +void translate_index_error(IndexError const &e); template @@ -75,7 +74,7 @@ public: explicit KeyError(): std::exception() {}; }; -void translate(KeyError const &e); +void translate_key_error(KeyError const &e); template struct map_item -- 2.30.2