1 /***************************************************************************
2 * Copyright (C) 2009 by Francesco Biscani *
3 * bluescarni@gmail.com *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 3 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20
21 #ifndef BOOST_PYTHON_P_EXCEPTIONS_H
22 #define BOOST_PYTHON_P_EXCEPTIONS_H
23
24 #include <boost/cast.hpp>
25 #include <boost/python/exception_translator.hpp>
26 #include <exception>
27
ie_translator(const index_error & ie)28 inline void ie_translator(const index_error &ie)
29 {
30 PyErr_SetString(PyExc_IndexError, ie.what());
31 }
32
ve_translator(const value_error & ve)33 inline void ve_translator(const value_error &ve)
34 {
35 PyErr_SetString(PyExc_ValueError, ve.what());
36 }
37
te_translator(const type_error & te)38 inline void te_translator(const type_error &te)
39 {
40 PyErr_SetString(PyExc_TypeError, te.what());
41 }
42
ae_translator(const assertion_error & ae)43 inline void ae_translator(const assertion_error &ae)
44 {
45 PyErr_SetString(PyExc_AssertionError, ae.what());
46 }
47
nie_translator(const not_implemented_error & nie)48 inline void nie_translator(const not_implemented_error &nie)
49 {
50 PyErr_SetString(PyExc_NotImplementedError, nie.what());
51 }
52
me_translator(const memory_error & me)53 inline void me_translator(const memory_error &me)
54 {
55 PyErr_SetString(PyExc_MemoryError, me.what());
56 }
57
zde_translator(const zero_division_error & zde)58 inline void zde_translator(const zero_division_error &zde)
59 {
60 PyErr_SetString(PyExc_ZeroDivisionError, zde.what());
61 }
62
63 // Translators for some standard and Boost exceptions.
64
ba_translator(const std::bad_alloc & ba)65 inline void ba_translator(const std::bad_alloc &ba)
66 {
67 PyErr_SetString(PyExc_MemoryError, ba.what());
68 }
69
oe_translator(const std::overflow_error & oe)70 inline void oe_translator(const std::overflow_error &oe)
71 {
72 PyErr_SetString(PyExc_OverflowError, oe.what());
73 }
74
bnc_translator(const boost::numeric::bad_numeric_cast & bnc)75 inline void bnc_translator(const boost::numeric::bad_numeric_cast &bnc)
76 {
77 PyErr_SetString(PyExc_OverflowError, bnc.what());
78 }
79
80 // Translate our C++ exceptions into Python exceptions.
translate_p_exceptions()81 inline void translate_p_exceptions()
82 {
83 boost::python::register_exception_translator<index_error>(ie_translator);
84 boost::python::register_exception_translator<value_error>(ve_translator);
85 boost::python::register_exception_translator<type_error>(te_translator);
86 boost::python::register_exception_translator<assertion_error>(ae_translator);
87 boost::python::register_exception_translator<not_implemented_error>(nie_translator);
88 boost::python::register_exception_translator<memory_error>(me_translator);
89 boost::python::register_exception_translator<zero_division_error>(zde_translator);
90 boost::python::register_exception_translator<std::bad_alloc>(ba_translator);
91 boost::python::register_exception_translator<std::overflow_error>(oe_translator);
92 boost::python::register_exception_translator<boost::numeric::bad_numeric_cast>(bnc_translator);
93 }
94
95 #endif
96