MCPcopy Create free account
hub / github.com/apple/coremltools / TEST_SUBMODULE

Function TEST_SUBMODULE

deps/pybind11/tests/test_exceptions.cpp:114–388  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

112};
113
114TEST_SUBMODULE(exceptions, m) {
115 m.def("throw_std_exception",
116 []() { throw std::runtime_error("This exception was intentionally thrown."); });
117
118 // PLEASE KEEP IN SYNC with docs/advanced/exceptions.rst
119 PYBIND11_CONSTINIT static py::gil_safe_call_once_and_store<py::object> ex_storage;
120 ex_storage.call_once_and_store_result(
121 [&]() { return py::exception<MyException>(m, "MyException"); });
122 py::register_exception_translator([](std::exception_ptr p) {
123 try {
124 if (p) {
125 std::rethrow_exception(p);
126 }
127 } catch (const MyException &e) {
128 // Set MyException as the active python error
129 py::set_error(ex_storage.get_stored(), e.what());
130 }
131 });
132
133 // Same as above, but using the deprecated `py::exception<>::operator()`
134 // We want to be sure it still works, until it's removed.
135 static const auto *const exd = new py::exception<MyExceptionUseDeprecatedOperatorCall>(
136 m, "MyExceptionUseDeprecatedOperatorCall");
137 py::register_exception_translator([](std::exception_ptr p) {
138 try {
139 if (p) {
140 std::rethrow_exception(p);
141 }
142 } catch (const MyExceptionUseDeprecatedOperatorCall &e) {
143#if defined(__INTEL_COMPILER) || defined(__NVCOMPILER)
144 // It is not worth the trouble dealing with warning suppressions for these compilers.
145 // Falling back to the recommended approach to keep the test code simple.
146 py::set_error(*exd, e.what());
147#else
148 PYBIND11_WARNING_PUSH
149 PYBIND11_WARNING_DISABLE_CLANG("-Wdeprecated-declarations")
150 PYBIND11_WARNING_DISABLE_GCC("-Wdeprecated-declarations")
151 PYBIND11_WARNING_DISABLE_MSVC(4996)
152 (*exd)(e.what());
153 PYBIND11_WARNING_POP
154#endif
155 }
156 });
157
158 // register new translator for MyException2
159 // no need to store anything here because this type will
160 // never by visible from Python
161 py::register_exception_translator([](std::exception_ptr p) {
162 try {
163 if (p) {
164 std::rethrow_exception(p);
165 }
166 } catch (const MyException2 &e) {
167 // Translate this exception to a standard RuntimeError
168 py::set_error(PyExc_RuntimeError, e.what());
169 }
170 });
171

Callers

nothing calls this directly

Calls 15

MyExceptionClass · 0.85
MyException2Class · 0.85
MyException3Class · 0.85
MyException4Class · 0.85
MyException5Class · 0.85
MyException5_1Class · 0.85
MyException6Class · 0.85
LocalExceptionClass · 0.85

Tested by

no test coverage detected