Pim Schellart | 5a7d17f | 2016-06-17 17:35:59 -0400 | [diff] [blame] | 1 | /* |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 2 | tests/test_custom-exceptions.cpp -- exception translation |
Pim Schellart | 5a7d17f | 2016-06-17 17:35:59 -0400 | [diff] [blame] | 3 | |
| 4 | Copyright (c) 2016 Pim Schellart <P.Schellart@princeton.edu> |
| 5 | |
| 6 | All rights reserved. Use of this source code is governed by a |
| 7 | BSD-style license that can be found in the LICENSE file. |
| 8 | */ |
| 9 | |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 10 | #include "pybind11_tests.h" |
Pim Schellart | 5a7d17f | 2016-06-17 17:35:59 -0400 | [diff] [blame] | 11 | |
luz.paz | 28cb676 | 2018-01-09 12:30:19 -0500 | [diff] [blame] | 12 | // A type that should be raised as an exception in Python |
Pim Schellart | 5a7d17f | 2016-06-17 17:35:59 -0400 | [diff] [blame] | 13 | class MyException : public std::exception { |
| 14 | public: |
| 15 | explicit MyException(const char * m) : message{m} {} |
Henry Schreiner | 5dfbe6f | 2020-09-10 22:43:53 -0400 | [diff] [blame] | 16 | const char * what() const noexcept override {return message.c_str();} |
Pim Schellart | 5a7d17f | 2016-06-17 17:35:59 -0400 | [diff] [blame] | 17 | private: |
| 18 | std::string message = ""; |
| 19 | }; |
| 20 | |
| 21 | // A type that should be translated to a standard Python exception |
| 22 | class MyException2 : public std::exception { |
| 23 | public: |
| 24 | explicit MyException2(const char * m) : message{m} {} |
Henry Schreiner | 5dfbe6f | 2020-09-10 22:43:53 -0400 | [diff] [blame] | 25 | const char * what() const noexcept override {return message.c_str();} |
Pim Schellart | 5a7d17f | 2016-06-17 17:35:59 -0400 | [diff] [blame] | 26 | private: |
| 27 | std::string message = ""; |
| 28 | }; |
| 29 | |
| 30 | // A type that is not derived from std::exception (and is thus unknown) |
| 31 | class MyException3 { |
| 32 | public: |
| 33 | explicit MyException3(const char * m) : message{m} {} |
| 34 | virtual const char * what() const noexcept {return message.c_str();} |
| 35 | private: |
| 36 | std::string message = ""; |
| 37 | }; |
| 38 | |
| 39 | // A type that should be translated to MyException |
| 40 | // and delegated to its exception translator |
| 41 | class MyException4 : public std::exception { |
| 42 | public: |
| 43 | explicit MyException4(const char * m) : message{m} {} |
Henry Schreiner | 5dfbe6f | 2020-09-10 22:43:53 -0400 | [diff] [blame] | 44 | const char * what() const noexcept override {return message.c_str();} |
Pim Schellart | 5a7d17f | 2016-06-17 17:35:59 -0400 | [diff] [blame] | 45 | private: |
| 46 | std::string message = ""; |
| 47 | }; |
| 48 | |
Jason Rhinelander | b3794f1 | 2016-09-16 02:04:15 -0400 | [diff] [blame] | 49 | |
| 50 | // Like the above, but declared via the helper function |
| 51 | class MyException5 : public std::logic_error { |
| 52 | public: |
| 53 | explicit MyException5(const std::string &what) : std::logic_error(what) {} |
| 54 | }; |
| 55 | |
| 56 | // Inherits from MyException5 |
| 57 | class MyException5_1 : public MyException5 { |
| 58 | using MyException5::MyException5; |
| 59 | }; |
| 60 | |
Dean Moldovan | 135ba8d | 2016-09-10 11:58:02 +0200 | [diff] [blame] | 61 | struct PythonCallInDestructor { |
| 62 | PythonCallInDestructor(const py::dict &d) : d(d) {} |
Jason Rhinelander | 3f1ff3f | 2016-12-12 17:42:52 -0500 | [diff] [blame] | 63 | ~PythonCallInDestructor() { d["good"] = true; } |
Dean Moldovan | 135ba8d | 2016-09-10 11:58:02 +0200 | [diff] [blame] | 64 | |
| 65 | py::dict d; |
| 66 | }; |
| 67 | |
James R. Barlow | 3618bea | 2020-08-08 03:07:14 -0700 | [diff] [blame] | 68 | |
| 69 | |
| 70 | struct PythonAlreadySetInDestructor { |
| 71 | PythonAlreadySetInDestructor(const py::str &s) : s(s) {} |
| 72 | ~PythonAlreadySetInDestructor() { |
| 73 | py::dict foo; |
| 74 | try { |
| 75 | // Assign to a py::object to force read access of nonexistent dict entry |
| 76 | py::object o = foo["bar"]; |
| 77 | } |
| 78 | catch (py::error_already_set& ex) { |
| 79 | ex.discard_as_unraisable(s); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | py::str s; |
| 84 | }; |
| 85 | |
| 86 | |
Jason Rhinelander | abcf43d | 2017-07-23 12:26:17 -0400 | [diff] [blame] | 87 | TEST_SUBMODULE(exceptions, m) { |
Dean Moldovan | 83e328f | 2017-06-09 00:44:49 +0200 | [diff] [blame] | 88 | m.def("throw_std_exception", []() { |
| 89 | throw std::runtime_error("This exception was intentionally thrown."); |
| 90 | }); |
| 91 | |
Pim Schellart | 5a7d17f | 2016-06-17 17:35:59 -0400 | [diff] [blame] | 92 | // make a new custom exception and use it as a translation target |
| 93 | static py::exception<MyException> ex(m, "MyException"); |
| 94 | py::register_exception_translator([](std::exception_ptr p) { |
| 95 | try { |
| 96 | if (p) std::rethrow_exception(p); |
| 97 | } catch (const MyException &e) { |
Jason Rhinelander | b3794f1 | 2016-09-16 02:04:15 -0400 | [diff] [blame] | 98 | // Set MyException as the active python error |
| 99 | ex(e.what()); |
Pim Schellart | 5a7d17f | 2016-06-17 17:35:59 -0400 | [diff] [blame] | 100 | } |
| 101 | }); |
| 102 | |
| 103 | // register new translator for MyException2 |
| 104 | // no need to store anything here because this type will |
| 105 | // never by visible from Python |
| 106 | py::register_exception_translator([](std::exception_ptr p) { |
| 107 | try { |
| 108 | if (p) std::rethrow_exception(p); |
| 109 | } catch (const MyException2 &e) { |
Jason Rhinelander | b3794f1 | 2016-09-16 02:04:15 -0400 | [diff] [blame] | 110 | // Translate this exception to a standard RuntimeError |
Pim Schellart | 5a7d17f | 2016-06-17 17:35:59 -0400 | [diff] [blame] | 111 | PyErr_SetString(PyExc_RuntimeError, e.what()); |
| 112 | } |
| 113 | }); |
| 114 | |
| 115 | // register new translator for MyException4 |
| 116 | // which will catch it and delegate to the previously registered |
| 117 | // translator for MyException by throwing a new exception |
| 118 | py::register_exception_translator([](std::exception_ptr p) { |
| 119 | try { |
| 120 | if (p) std::rethrow_exception(p); |
| 121 | } catch (const MyException4 &e) { |
| 122 | throw MyException(e.what()); |
| 123 | } |
| 124 | }); |
| 125 | |
Jason Rhinelander | b3794f1 | 2016-09-16 02:04:15 -0400 | [diff] [blame] | 126 | // A simple exception translation: |
| 127 | auto ex5 = py::register_exception<MyException5>(m, "MyException5"); |
| 128 | // A slightly more complicated one that declares MyException5_1 as a subclass of MyException5 |
| 129 | py::register_exception<MyException5_1>(m, "MyException5_1", ex5.ptr()); |
| 130 | |
Jason Rhinelander | abcf43d | 2017-07-23 12:26:17 -0400 | [diff] [blame] | 131 | m.def("throws1", []() { throw MyException("this error should go to a custom type"); }); |
| 132 | m.def("throws2", []() { throw MyException2("this error should go to a standard Python exception"); }); |
| 133 | m.def("throws3", []() { throw MyException3("this error cannot be translated"); }); |
| 134 | m.def("throws4", []() { throw MyException4("this error is rethrown"); }); |
| 135 | m.def("throws5", []() { throw MyException5("this is a helper-defined translated exception"); }); |
| 136 | m.def("throws5_1", []() { throw MyException5_1("MyException5 subclass"); }); |
| 137 | m.def("throws_logic_error", []() { throw std::logic_error("this error should fall through to the standard handler"); }); |
Francesco Biscani | deb3cb2 | 2019-11-14 08:56:58 +0100 | [diff] [blame] | 138 | m.def("throws_overflow_error", []() {throw std::overflow_error(""); }); |
Jason Rhinelander | abcf43d | 2017-07-23 12:26:17 -0400 | [diff] [blame] | 139 | m.def("exception_matches", []() { |
| 140 | py::dict foo; |
Yannick Jadoul | 97784da | 2019-05-12 23:35:49 +0200 | [diff] [blame] | 141 | try { |
| 142 | // Assign to a py::object to force read access of nonexistent dict entry |
| 143 | py::object o = foo["bar"]; |
| 144 | } |
Jason Rhinelander | abcf43d | 2017-07-23 12:26:17 -0400 | [diff] [blame] | 145 | catch (py::error_already_set& ex) { |
Jason Rhinelander | 1682b67 | 2017-07-20 23:14:33 -0400 | [diff] [blame] | 146 | if (!ex.matches(PyExc_KeyError)) throw; |
Yannick Jadoul | 97784da | 2019-05-12 23:35:49 +0200 | [diff] [blame] | 147 | return true; |
Jason Rhinelander | abcf43d | 2017-07-23 12:26:17 -0400 | [diff] [blame] | 148 | } |
Yannick Jadoul | 97784da | 2019-05-12 23:35:49 +0200 | [diff] [blame] | 149 | return false; |
| 150 | }); |
| 151 | m.def("exception_matches_base", []() { |
| 152 | py::dict foo; |
| 153 | try { |
| 154 | // Assign to a py::object to force read access of nonexistent dict entry |
| 155 | py::object o = foo["bar"]; |
| 156 | } |
| 157 | catch (py::error_already_set &ex) { |
| 158 | if (!ex.matches(PyExc_Exception)) throw; |
| 159 | return true; |
| 160 | } |
| 161 | return false; |
| 162 | }); |
| 163 | m.def("modulenotfound_exception_matches_base", []() { |
| 164 | try { |
| 165 | // On Python >= 3.6, this raises a ModuleNotFoundError, a subclass of ImportError |
| 166 | py::module::import("nonexistent"); |
| 167 | } |
| 168 | catch (py::error_already_set &ex) { |
| 169 | if (!ex.matches(PyExc_ImportError)) throw; |
| 170 | return true; |
| 171 | } |
| 172 | return false; |
Jason Rhinelander | abcf43d | 2017-07-23 12:26:17 -0400 | [diff] [blame] | 173 | }); |
Pim Schellart | 5a7d17f | 2016-06-17 17:35:59 -0400 | [diff] [blame] | 174 | |
Ivan Smirnov | 67b5489 | 2016-09-07 21:10:16 +0100 | [diff] [blame] | 175 | m.def("throw_already_set", [](bool err) { |
| 176 | if (err) |
| 177 | PyErr_SetString(PyExc_ValueError, "foo"); |
| 178 | try { |
| 179 | throw py::error_already_set(); |
| 180 | } catch (const std::runtime_error& e) { |
| 181 | if ((err && e.what() != std::string("ValueError: foo")) || |
| 182 | (!err && e.what() != std::string("Unknown internal error occurred"))) |
| 183 | { |
| 184 | PyErr_Clear(); |
| 185 | throw std::runtime_error("error message mismatch"); |
| 186 | } |
| 187 | } |
| 188 | PyErr_Clear(); |
| 189 | if (err) |
| 190 | PyErr_SetString(PyExc_ValueError, "foo"); |
| 191 | throw py::error_already_set(); |
| 192 | }); |
Dean Moldovan | 135ba8d | 2016-09-10 11:58:02 +0200 | [diff] [blame] | 193 | |
| 194 | m.def("python_call_in_destructor", [](py::dict d) { |
| 195 | try { |
| 196 | PythonCallInDestructor set_dict_in_destructor(d); |
| 197 | PyErr_SetString(PyExc_ValueError, "foo"); |
| 198 | throw py::error_already_set(); |
| 199 | } catch (const py::error_already_set&) { |
| 200 | return true; |
| 201 | } |
| 202 | return false; |
| 203 | }); |
Jason Rhinelander | 1682b67 | 2017-07-20 23:14:33 -0400 | [diff] [blame] | 204 | |
James R. Barlow | 3618bea | 2020-08-08 03:07:14 -0700 | [diff] [blame] | 205 | m.def("python_alreadyset_in_destructor", [](py::str s) { |
| 206 | PythonAlreadySetInDestructor alreadyset_in_destructor(s); |
| 207 | return true; |
| 208 | }); |
| 209 | |
Jason Rhinelander | 1682b67 | 2017-07-20 23:14:33 -0400 | [diff] [blame] | 210 | // test_nested_throws |
| 211 | m.def("try_catch", [m](py::object exc_type, py::function f, py::args args) { |
| 212 | try { f(*args); } |
| 213 | catch (py::error_already_set &ex) { |
| 214 | if (ex.matches(exc_type)) |
| 215 | py::print(ex.what()); |
| 216 | else |
| 217 | throw; |
| 218 | } |
| 219 | }); |
| 220 | |
Henry Schreiner | cf0a645 | 2020-08-18 07:14:34 -0400 | [diff] [blame] | 221 | // Test repr that cannot be displayed |
| 222 | m.def("simple_bool_passthrough", [](bool x) {return x;}); |
| 223 | |
Jason Rhinelander | abcf43d | 2017-07-23 12:26:17 -0400 | [diff] [blame] | 224 | } |