File tree 2 files changed +31
-0
lines changed 2 files changed +31
-0
lines changed Original file line number Diff line number Diff line change @@ -276,6 +276,19 @@ extern "C" {
276
276
} \
277
277
}
278
278
279
+ #if PY_VERSION_HEX >= 0x03030000
280
+
281
+ #define PYBIND11_CATCH_INIT_EXCEPTIONS \
282
+ catch (pybind11::error_already_set &e) { \
283
+ pybind11::raise_from (e, PyExc_ImportError, " initialization failed" ); \
284
+ return nullptr ; \
285
+ } catch (const std::exception &e) { \
286
+ PyErr_SetString (PyExc_ImportError, e.what ()); \
287
+ return nullptr ; \
288
+ } \
289
+
290
+ #else
291
+
279
292
#define PYBIND11_CATCH_INIT_EXCEPTIONS \
280
293
catch (pybind11::error_already_set &e) { \
281
294
PyErr_SetString (PyExc_ImportError, e.what ()); \
@@ -285,6 +298,8 @@ extern "C" {
285
298
return nullptr ; \
286
299
} \
287
300
301
+ #endif
302
+
288
303
/* * \rst
289
304
***Deprecated in favor of PYBIND11_MODULE***
290
305
Original file line number Diff line number Diff line change @@ -74,8 +74,24 @@ TEST_CASE("Import error handling") {
74
74
REQUIRE_NOTHROW (py::module_::import (" widget_module" ));
75
75
REQUIRE_THROWS_WITH (py::module_::import (" throw_exception" ),
76
76
" ImportError: C++ Error" );
77
+ #if PY_VERSION_HEX >= 0x03030000
78
+ REQUIRE_THROWS_WITH (py::module_::import (" throw_error_already_set" ),
79
+ Catch::Contains (" ImportError: initialization failed" ));
80
+
81
+ auto locals = py::dict (" is_keyerror" _a=false , " message" _a=" not set" );
82
+ py::exec (R"(
83
+ try:
84
+ import throw_error_already_set
85
+ except ImportError as e:
86
+ is_keyerror = type(e.__cause__) == KeyError
87
+ message = str(e.__cause__)
88
+ )" , py::globals (), locals);
89
+ REQUIRE (locals[" is_keyerror" ].cast <bool >() == true );
90
+ REQUIRE (locals[" message" ].cast <std::string>() == " 'missing'" );
91
+ #else
77
92
REQUIRE_THROWS_WITH (py::module_::import (" throw_error_already_set" ),
78
93
Catch::Contains (" ImportError: KeyError" ));
94
+ #endif
79
95
}
80
96
81
97
TEST_CASE (" There can be only one interpreter" ) {
You can’t perform that action at this time.
0 commit comments