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