Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/libs/python/src/errors.cpp @ 29

Last change on this file since 29 was 29, checked in by landauf, 16 years ago

updated boost from 1_33_1 to 1_34_1

File size: 2.3 KB
Line 
1// Copyright David Abrahams 2001.
2// Distributed under the Boost Software License, Version 1.0. (See
3// accompanying file LICENSE_1_0.txt or copy at
4// http://www.boost.org/LICENSE_1_0.txt)
5
6#ifndef BOOST_PYTHON_SOURCE
7# define BOOST_PYTHON_SOURCE
8#endif
9
10#include <boost/python/errors.hpp>
11#include <boost/cast.hpp>
12#include <boost/python/detail/exception_handler.hpp>
13
14namespace boost { namespace python {
15
16error_already_set::~error_already_set() {}
17
18// IMPORTANT: this function may only be called from within a catch block!
19BOOST_PYTHON_DECL bool handle_exception_impl(function0<void> f)
20{
21    try
22    {
23        if (detail::exception_handler::chain)
24            return detail::exception_handler::chain->handle(f);
25        f();
26        return false;
27    }
28    catch(const boost::python::error_already_set&)
29    {
30        // The python error reporting has already been handled.
31    }
32    catch(const std::bad_alloc&)
33    {
34        PyErr_NoMemory();
35    }
36    catch(const bad_numeric_cast& x)
37    {
38        PyErr_SetString(PyExc_OverflowError, x.what());
39    }
40    catch(const std::out_of_range& x)
41    {
42        PyErr_SetString(PyExc_IndexError, x.what());
43    }
44    catch(const std::exception& x)
45    {
46        PyErr_SetString(PyExc_RuntimeError, x.what());
47    }
48    catch(...)
49    {
50        PyErr_SetString(PyExc_RuntimeError, "unidentifiable C++ exception");
51    }
52    return true;
53}
54
55void BOOST_PYTHON_DECL throw_error_already_set()
56{
57    throw error_already_set();
58}
59
60namespace detail {
61
62bool exception_handler::operator()(function0<void> const& f) const
63{
64    if (m_next)
65    {
66        return m_next->handle(f);
67    }
68    else
69    {
70        f();
71        return false;
72    }
73}
74
75exception_handler::exception_handler(handler_function const& impl)
76    : m_impl(impl)
77    , m_next(0)
78{
79    if (chain != 0)
80        tail->m_next = this;
81    else
82        chain = this;
83    tail = this;
84}
85
86exception_handler* exception_handler::chain;
87exception_handler* exception_handler::tail;
88
89BOOST_PYTHON_DECL void register_exception_handler(handler_function const& f)
90{
91    // the constructor links the new object into a handler chain, so
92    // this object isn't actaully leaked (until, of course, the
93    // interpreter exits).
94    new exception_handler(f);
95}
96
97} // namespace boost::python::detail
98
99}} // namespace boost::python
100
101
Note: See TracBrowser for help on using the repository browser.