Commit 68a62241 authored by Fuchsi*'s avatar Fuchsi*

export misc::generic_error to python (closes #199)

parent 4a5f5726
Pipeline #728 passed with stages
in 8 minutes and 9 seconds
......@@ -77,8 +77,8 @@ __tabsMid
~~~ python
try:
c(j) << A(i,j) * b(j) # runtime error!
except xerus.misc.generic_error as err:
print("something went wrong:", err.what())
except xerus.generic_error as err:
print("something went wrong:", err)
~~~
__tabsEnd
......
// Xerus - A General Purpose Tensor Library
// Copyright (C) 2014-2016 Benjamin Huber and Sebastian Wolf.
//
// Xerus is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License,
// or (at your option) any later version.
//
// Xerus is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with Xerus. If not, see <http://www.gnu.org/licenses/>.
//
// For further information on Xerus visit https://libXerus.org
// or contact us at contact@libXerus.org.
#include<xerus.h>
#include "../../include/xerus/test/test.h"
#include "../../include/xerus/misc/internal.h"
using namespace xerus;
static misc::UnitTest failtest("Index", "invalid_spans", [](){
#ifdef XERUS_DISABLE_RUNTIME_CHECKS
LOG(warning, "failtests skipped due to XERUS_DISABLE_RUNTIME_CHECKS");
#else
Index i,j;
Tensor c;
Tensor A = Tensor::random({10,10});
Tensor b = Tensor::random({10});
try {
A(i,j^2) = A(j^2,i);
MTEST(false, "A(j^2,i) did not throw!");
} catch (misc::generic_error &_e) {
TEST(true);
} catch (...) {
MTEST(false, "threw a wrong exception class!");
}
try {
A(i,j) = A(j,i&0);
MTEST(false, "A(j^2,i) did not throw!");
} catch (misc::generic_error &_e) {
TEST(true);
} catch (...) {
MTEST(false, "threw a wrong exception class!");
}
try {
A(i,j) = A(j,i/1);
MTEST(false, "A(j^2,i) did not throw!");
} catch (misc::generic_error &_e) {
TEST(true);
} catch (...) {
MTEST(false, "threw a wrong exception class!");
}
try {
A(i,j) = A(j,i/3);
MTEST(false, "A(j^2,i) did not throw!");
} catch (misc::generic_error &_e) {
TEST(true);
} catch (...) {
MTEST(false, "threw a wrong exception class!");
}
try {
c(j) = A(i,j) * b(j);
MTEST(false, "c(j)=... did not throw!");
} catch (misc::generic_error &_e) {
TEST(true);
} catch (...) {
MTEST(false, "threw a wrong exception class!");
}
#endif
});
......@@ -288,3 +288,4 @@ static misc::UnitTest tensor_sparse_dense("Tensor", "Sparse_Dense_Conversions",
});
......@@ -1206,9 +1206,15 @@ BOOST_PYTHON_MODULE(xerus) {
// identity returns the cpp name to a python object
// def("identity", identity_);
// the following is probably not necessary because generic_error inherits from std::exception
// register_exception_translator<misc::generic_error>([](const misc::generic_error &_e){
// LOG(pydebug, "custom exception handler called with " << _e.what());
// PyErr_SetString(PyExc_UserWarning, _e.what());
// });
def("xethrow", +[](){XERUS_THROW(misc::generic_error() << misc::get_call_stack());});
// translate all exceptions thrown inside xerus to own python exception class
static char fully_qualified_gen_error_name[] = "xerus.generic_error";
static PyObject* py_gen_error = PyErr_NewException(fully_qualified_gen_error_name, PyExc_Exception, 0);
py::scope().attr("generic_error") = py::handle<>(py::borrowed(py_gen_error));
register_exception_translator<misc::generic_error>([](const misc::generic_error &_e){
LOG(pydebug, "custom exception handler called with " << _e.what());
PyErr_SetString(py_gen_error, _e.what());
});
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment