mirror of
https://github.com/boostorg/python.git
synced 2026-01-22 05:22:45 +00:00
Python->C++ exception translation
[SVN r14800]
This commit is contained in:
48
include/boost/python/detail/exception_handler.hpp
Normal file
48
include/boost/python/detail/exception_handler.hpp
Normal 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
|
||||
46
include/boost/python/detail/translate_exception.hpp
Normal file
46
include/boost/python/detail/translate_exception.hpp
Normal 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
|
||||
@@ -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)
|
||||
|
||||
25
include/boost/python/exception_translator.hpp
Normal file
25
include/boost/python/exception_translator.hpp
Normal 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
|
||||
@@ -10,14 +10,21 @@
|
||||
|
||||
#include <boost/python/errors.hpp>
|
||||
#include <boost/cast.hpp>
|
||||
#ifdef BOOST_PYTHON_V2
|
||||
# include <boost/python/detail/exception_handler.hpp>
|
||||
#endif
|
||||
|
||||
namespace boost { namespace python {
|
||||
|
||||
// IMPORTANT: this function may only be called from within a catch block!
|
||||
BOOST_PYTHON_DECL bool handle_exception_impl(function0<void> f)
|
||||
BOOST_PYTHON_DECL bool handle_exception_impl(function0<void> const& f)
|
||||
{
|
||||
try
|
||||
{
|
||||
#ifdef BOOST_PYTHON_V2
|
||||
if (detail::exception_handler::chain)
|
||||
return detail::exception_handler::chain->handle(f);
|
||||
#endif
|
||||
f();
|
||||
return false;
|
||||
}
|
||||
@@ -68,6 +75,42 @@ namespace detail {
|
||||
// needed by void_adaptor (see void_adaptor.hpp)
|
||||
BOOST_PYTHON_DECL PyObject arbitrary_object = { 0 };
|
||||
|
||||
#ifdef BOOST_PYTHON_V2
|
||||
bool exception_handler::operator()(function0<void> const& f) const
|
||||
{
|
||||
if (m_next)
|
||||
{
|
||||
return m_next->handle(f);
|
||||
}
|
||||
else
|
||||
{
|
||||
f();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
exception_handler::exception_handler(handler_function const& impl)
|
||||
: m_impl(impl)
|
||||
, m_next(0)
|
||||
{
|
||||
if (chain != 0)
|
||||
tail->m_next = this;
|
||||
else
|
||||
chain = this;
|
||||
tail = this;
|
||||
}
|
||||
|
||||
exception_handler* exception_handler::chain;
|
||||
exception_handler* exception_handler::tail;
|
||||
|
||||
BOOST_PYTHON_DECL void register_exception_handler(handler_function const& f)
|
||||
{
|
||||
// the constructor links the new object into a handler chain, so
|
||||
// this object isn't actaully leaked (until, of course, the
|
||||
// interpreter exits).
|
||||
new exception_handler(f);
|
||||
}
|
||||
#endif
|
||||
|
||||
} // namespace boost::python::detail
|
||||
|
||||
|
||||
@@ -56,6 +56,7 @@ rule bpl-test ( name ? : files * )
|
||||
|
||||
bpl-test minimal ;
|
||||
bpl-test docstring ;
|
||||
bpl-test exception_translator ;
|
||||
bpl-test pearu1 : test_cltree.py cltree.cpp ;
|
||||
bpl-test try : newtest.py m1.cpp m2.cpp ;
|
||||
bpl-test builtin_converters : test_builtin_converters.py test_builtin_converters.cpp ;
|
||||
|
||||
25
test/exception_translator.cpp
Normal file
25
test/exception_translator.cpp
Normal file
@@ -0,0 +1,25 @@
|
||||
#include <boost/python/module.hpp>
|
||||
#include <boost/python/exception_translator.hpp>
|
||||
|
||||
struct error {};
|
||||
|
||||
void translate(error const& e)
|
||||
{
|
||||
PyErr_SetString(PyExc_RuntimeError, "!!!error!!!");
|
||||
}
|
||||
|
||||
void throw_error()
|
||||
{
|
||||
throw error();
|
||||
|
||||
}
|
||||
|
||||
BOOST_PYTHON_MODULE_INIT(exception_translator_ext)
|
||||
{
|
||||
using namespace boost::python;
|
||||
|
||||
register_exception_translator<error>(&translate);
|
||||
module("exception_translator_ext")
|
||||
.def("throw_error", throw_error);
|
||||
}
|
||||
|
||||
22
test/exception_translator.py
Normal file
22
test/exception_translator.py
Normal file
@@ -0,0 +1,22 @@
|
||||
'''
|
||||
>>> from exception_translator_ext import *
|
||||
>>> try:
|
||||
... throw_error();
|
||||
... except RuntimeError, x:
|
||||
... print x
|
||||
... else:
|
||||
... print 'Expected a RuntimeError!'
|
||||
!!!error!!!
|
||||
'''
|
||||
def run(args = None):
|
||||
import sys
|
||||
import doctest
|
||||
|
||||
if args is not None:
|
||||
sys.argv = args
|
||||
return doctest.testmod(sys.modules.get(__name__))
|
||||
|
||||
if __name__ == '__main__':
|
||||
print "running..."
|
||||
import sys
|
||||
sys.exit(run()[0])
|
||||
Reference in New Issue
Block a user