2
0
mirror of https://github.com/boostorg/python.git synced 2026-01-24 06:02:14 +00:00

Python->C++ exception translation

[SVN r14800]
This commit is contained in:
Dave Abrahams
2002-08-13 00:45:09 +00:00
parent 0a6a213891
commit fe3cf386c3
8 changed files with 212 additions and 2 deletions

View File

@@ -0,0 +1,48 @@
// Copyright David Abrahams 2002. Permission to copy, use,
// modify, sell and distribute this software is granted provided this
// copyright notice appears in all copies. This software is provided
// "as is" without express or implied warranty, and with no claim as
// to its suitability for any purpose.
#ifndef EXCEPTION_HANDLER_DWA2002810_HPP
# define EXCEPTION_HANDLER_DWA2002810_HPP
# include <boost/function/function0.hpp>
# include <boost/function/function2.hpp>
namespace boost { namespace python { namespace detail {
struct BOOST_PYTHON_DECL exception_handler;
typedef function2<bool, exception_handler const&, function0<void> const&> handler_function;
struct BOOST_PYTHON_DECL exception_handler
{
private: // types
public:
explicit exception_handler(handler_function const& impl);
inline bool handle(function0<void> const& f) const;
bool operator()(function0<void> const& f) const;
static exception_handler* chain;
private:
static exception_handler* tail;
handler_function m_impl;
exception_handler* m_next;
};
inline bool exception_handler::handle(function0<void> const& f) const
{
return this->m_impl(*this, f);
}
BOOST_PYTHON_DECL void register_exception_handler(handler_function const& f);
}}} // namespace boost::python::detail
#endif // EXCEPTION_HANDLER_DWA2002810_HPP

View File

@@ -0,0 +1,46 @@
// Copyright David Abrahams 2002. Permission to copy, use,
// modify, sell and distribute this software is granted provided this
// copyright notice appears in all copies. This software is provided
// "as is" without express or implied warranty, and with no claim as
// to its suitability for any purpose.
#ifndef TRANSLATE_EXCEPTION_DWA2002810_HPP
# define TRANSLATE_EXCEPTION_DWA2002810_HPP
# include <boost/function/function0.hpp>
# include <boost/call_traits.hpp>
namespace boost { namespace python { namespace detail {
struct exception_handler;
// A ternary function object used to translate C++ exceptions of type
// ExceptionType into Python exceptions by invoking an object of type
// Translate. Typically the translate function will be curried with
// boost::bind().
template <class ExceptionType, class Translate>
struct translate_exception
{
typedef typename add_reference<
typename add_const<ExceptionType>::type
>::type exception_cref;
inline bool operator()(
exception_handler const& handler
, function0<void> const& f
, typename call_traits<Translate>::param_type translate) const
{
try
{
return handler(f);
}
catch(exception_cref e)
{
translate(e);
return true;
}
}
};
}}} // namespace boost::python::detail
#endif // TRANSLATE_EXCEPTION_DWA2002810_HPP

View File

@@ -20,7 +20,7 @@ struct BOOST_PYTHON_DECL argument_error : error_already_set {};
// Handles exceptions caught just before returning to Python code.
// Returns true iff an exception was caught.
BOOST_PYTHON_DECL bool handle_exception_impl(function0<void>);
BOOST_PYTHON_DECL bool handle_exception_impl(function0<void> const&);
template <class T>
bool handle_exception(T f)

View File

@@ -0,0 +1,25 @@
// Copyright David Abrahams 2002. Permission to copy, use,
// modify, sell and distribute this software is granted provided this
// copyright notice appears in all copies. This software is provided
// "as is" without express or implied warranty, and with no claim as
// to its suitability for any purpose.
#ifndef EXCEPTION_TRANSLATOR_DWA2002810_HPP
# define EXCEPTION_TRANSLATOR_DWA2002810_HPP
# include <boost/python/detail/translate_exception.hpp>
# include <boost/python/detail/exception_handler.hpp>
# include <boost/type.hpp>
# include <boost/bind.hpp>
namespace boost { namespace python {
template <class ExceptionType, class Translate>
void register_exception_translator(Translate const& translate, boost::type<ExceptionType>* = 0)
{
detail::register_exception_handler(
bind<bool>(detail::translate_exception<ExceptionType,Translate>(), _1, _2, translate)
);
}
}} // namespace boost::python
#endif // EXCEPTION_TRANSLATOR_DWA2002810_HPP