Python: Wrapped exception

This commit is contained in:
Philippe Tillet
2015-05-01 11:29:39 -04:00
parent 86ffc7f6a6
commit 1a3923c562
5 changed files with 96 additions and 2 deletions

View File

@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 2.8)
cmake_minimum_required(VERSION 2.8.10)
# Add visibility of headers
file( GLOB_RECURSE MAKE_HEADERS_VISIBLE_SRC *.hpp *.h)

View File

@@ -58,7 +58,7 @@ def main():
'${CUDA_INCLUDE_DIRS}']
LIBRARY_DIRS = ['${CMAKE_BINARY_DIR}/lib']
src = [os.path.join('${CMAKE_CURRENT_SOURCE_DIR}', 'src', sf) for sf in ['_isaac.cpp']]
src = [os.path.join('${CMAKE_CURRENT_SOURCE_DIR}', 'src', sf) for sf in ['_isaac.cpp', 'core.cpp', 'driver.cpp', 'model.cpp', 'exceptions.cpp']]
boostsrc = '${CMAKE_CURRENT_SOURCE_DIR}/external/boost/libs/'
for s in ['numpy','python','smart_ptr','system','thread']:

View File

@@ -1,5 +1,6 @@
#include "core.h"
#include "driver.h"
#include "exceptions.h"
#include "model.h"
#include <boost/python.hpp>
@@ -18,6 +19,7 @@ BOOST_PYTHON_MODULE(_isaac)
package.attr("__path__") = "_isaac";
export_driver();
export_exceptions();
export_model();
export_core();
}

86
python/src/exceptions.cpp Normal file
View File

@@ -0,0 +1,86 @@
#include <boost/bind.hpp>
#include <boost/python.hpp>
#include "isaac/exception/operation_not_supported.h"
#include "common.hpp"
#include "exceptions.h"
namespace wrap
{
//Code taken from https://mail.python.org/pipermail/cplusplus-sig/2006-May/010347.html
template< typename CPP_ExceptionType
, typename X1 = bp::detail::not_specified
, typename X2 = bp::detail::not_specified
, typename X3 = bp::detail::not_specified
>
class exception
: public bp::class_<CPP_ExceptionType, X1, X2, X3>
{
public:
typedef bp::class_<CPP_ExceptionType, X1, X2, X3> base_type;
typedef typename base_type::wrapped_type wrapped_type;
typedef exception<CPP_ExceptionType, X1, X2, X3> self;
// Construct with the class name, with or without docstring, and default
// __init__() function
exception(char const* name, char const* doc = 0) : base_type(name, doc)
{
init();
}
// Construct with class name, no docstring, and an uncallable
// __init__ function
exception(char const* name, bp::no_init_t const& no_init_tag): base_type(name, no_init_tag)
{
init();
}
// Construct with class name, docstring, and an uncallable
// __init__ function
exception(char const* name, char const* doc, bp::no_init_t const& no_init_tag): base_type(name, doc, no_init_tag)
{
init();
}
// Construct with class name and init<> function
template <class DerivedT>
inline exception(char const* name, bp::init_base<DerivedT> const& i): base_type(name, i)
{
init();
}
// Construct with class name, docstring and init<> function
template <class DerivedT>
inline exception( char const* name
, char const* doc
, bp::init_base<DerivedT> const& i): base_type(name, doc, i)
{
init();
}
private:
static void to_python_exception(bp::object const& exn_type, wrapped_type const& exn)
{
static const bp::to_python_value<wrapped_type> convert_argument;
PyErr_SetObject(exn_type.ptr(), convert_argument(exn));
bp::throw_error_already_set();
}
void init() const
{
bp::register_exception_translator<wrapped_type>( std::bind(&to_python_exception, *this, std::placeholders::_1));
}
};
}
void export_exceptions()
{
wrap::exception<isaac::operation_not_supported_exception>("OperationNotSupported", bp::init<std::string>())
.def("__str__", &isaac::operation_not_supported_exception::what)
;
}

6
python/src/exceptions.h Normal file
View File

@@ -0,0 +1,6 @@
#ifndef ISAAC_PYTHON_EXCEPTIONS_HPP
#define ISAAC_PYTHON_EXCEPTIONS_HPP
void export_exceptions();
#endif