mirror of
https://github.com/pybind/pybind11.git
synced 2024-11-14 09:34:46 +00:00
52f4be8946
Adding or removing tests is a little bit cumbersome currently: the test needs to be added to CMakeLists.txt, the init function needs to be predeclared in pybind11_tests.cpp, then called in the plugin initialization. While this isn't a big deal for tests that are being committed, it's more of a hassle when working on some new feature or test code for which I temporarily only care about building and linking the test being worked on rather than the entire test suite. This commit changes tests to self-register their initialization by having each test initialize a local object (which stores the initialization function in a static variable). This makes changing the set of tests being build easy: one only needs to add or comment out test names in tests/CMakeLists.txt. A couple other minor changes that go along with this: - test_eigen.cpp is now included in the test list, then removed if eigen isn't available. This lets you disable the eigen tests by commenting it out, just like all the other tests, but keeps the build working without eigen eigen isn't available. (Also, if it's commented out, we don't even bother looking for and reporting the building with/without eigen status message). - pytest is now invoked with all the built test names (with .cpp changed to .py) so that it doesn't try to run tests that weren't built.
109 lines
3.3 KiB
C++
109 lines
3.3 KiB
C++
/*
|
|
tests/test_custom-exceptions.cpp -- exception translation
|
|
|
|
Copyright (c) 2016 Pim Schellart <P.Schellart@princeton.edu>
|
|
|
|
All rights reserved. Use of this source code is governed by a
|
|
BSD-style license that can be found in the LICENSE file.
|
|
*/
|
|
|
|
#include "pybind11_tests.h"
|
|
|
|
// A type that should be raised as an exeption in Python
|
|
class MyException : public std::exception {
|
|
public:
|
|
explicit MyException(const char * m) : message{m} {}
|
|
virtual const char * what() const noexcept override {return message.c_str();}
|
|
private:
|
|
std::string message = "";
|
|
};
|
|
|
|
// A type that should be translated to a standard Python exception
|
|
class MyException2 : public std::exception {
|
|
public:
|
|
explicit MyException2(const char * m) : message{m} {}
|
|
virtual const char * what() const noexcept override {return message.c_str();}
|
|
private:
|
|
std::string message = "";
|
|
};
|
|
|
|
// A type that is not derived from std::exception (and is thus unknown)
|
|
class MyException3 {
|
|
public:
|
|
explicit MyException3(const char * m) : message{m} {}
|
|
virtual const char * what() const noexcept {return message.c_str();}
|
|
private:
|
|
std::string message = "";
|
|
};
|
|
|
|
// A type that should be translated to MyException
|
|
// and delegated to its exception translator
|
|
class MyException4 : public std::exception {
|
|
public:
|
|
explicit MyException4(const char * m) : message{m} {}
|
|
virtual const char * what() const noexcept override {return message.c_str();}
|
|
private:
|
|
std::string message = "";
|
|
};
|
|
|
|
void throws1() {
|
|
throw MyException("this error should go to a custom type");
|
|
}
|
|
|
|
void throws2() {
|
|
throw MyException2("this error should go to a standard Python exception");
|
|
}
|
|
|
|
void throws3() {
|
|
throw MyException3("this error cannot be translated");
|
|
}
|
|
|
|
void throws4() {
|
|
throw MyException4("this error is rethrown");
|
|
}
|
|
|
|
void throws_logic_error() {
|
|
throw std::logic_error("this error should fall through to the standard handler");
|
|
}
|
|
|
|
test_initializer custom_exceptions([](py::module &m) {
|
|
// make a new custom exception and use it as a translation target
|
|
static py::exception<MyException> ex(m, "MyException");
|
|
py::register_exception_translator([](std::exception_ptr p) {
|
|
try {
|
|
if (p) std::rethrow_exception(p);
|
|
} catch (const MyException &e) {
|
|
PyErr_SetString(ex.ptr(), e.what());
|
|
}
|
|
});
|
|
|
|
// register new translator for MyException2
|
|
// no need to store anything here because this type will
|
|
// never by visible from Python
|
|
py::register_exception_translator([](std::exception_ptr p) {
|
|
try {
|
|
if (p) std::rethrow_exception(p);
|
|
} catch (const MyException2 &e) {
|
|
PyErr_SetString(PyExc_RuntimeError, e.what());
|
|
}
|
|
});
|
|
|
|
// register new translator for MyException4
|
|
// which will catch it and delegate to the previously registered
|
|
// translator for MyException by throwing a new exception
|
|
py::register_exception_translator([](std::exception_ptr p) {
|
|
try {
|
|
if (p) std::rethrow_exception(p);
|
|
} catch (const MyException4 &e) {
|
|
throw MyException(e.what());
|
|
}
|
|
});
|
|
|
|
m.def("throws1", &throws1);
|
|
m.def("throws2", &throws2);
|
|
m.def("throws3", &throws3);
|
|
m.def("throws4", &throws4);
|
|
m.def("throws_logic_error", &throws_logic_error);
|
|
});
|
|
|