15
15
#include " detail/smart_holder_sfinae_hooks_only.h"
16
16
#include " attr.h"
17
17
#include " gil.h"
18
+ #include " gil_safe_call_once.h"
18
19
#include " options.h"
19
20
#include " typing.h"
20
21
@@ -2869,23 +2870,14 @@ class exception : public object {
2869
2870
};
2870
2871
2871
2872
PYBIND11_NAMESPACE_BEGIN (detail)
2872
- // Returns a reference to a function-local static exception object used in the simple
2873
- // register_exception approach below. (It would be simpler to have the static local variable
2874
- // directly in register_exception, but that makes clang <3.5 segfault - issue #1349).
2875
- template <typename CppException>
2876
- exception <CppException> &get_exception_object () {
2877
- static exception <CppException> ex;
2878
- return ex;
2879
- }
2880
2873
2881
2874
// Helper function for register_exception and register_local_exception
2882
2875
template <typename CppException>
2883
2876
exception <CppException> &
2884
2877
register_exception_impl (handle scope, const char *name, handle base, bool isLocal) {
2885
- auto &ex = detail::get_exception_object<CppException>();
2886
- if (!ex) {
2887
- ex = exception <CppException>(scope, name, base);
2888
- }
2878
+ PYBIND11_CONSTINIT static gil_safe_call_once_and_store<exception <CppException>> exc_storage;
2879
+ exc_storage.call_once_and_store_result (
2880
+ [&]() { return exception <CppException>(scope, name, base); });
2889
2881
2890
2882
auto register_func
2891
2883
= isLocal ? ®ister_local_exception_translator : ®ister_exception_translator;
@@ -2897,10 +2889,10 @@ register_exception_impl(handle scope, const char *name, handle base, bool isLoca
2897
2889
try {
2898
2890
std::rethrow_exception (p);
2899
2891
} catch (const CppException &e) {
2900
- set_error (detail::get_exception_object<CppException> (), e.what ());
2892
+ set_error (exc_storage. get_stored (), e.what ());
2901
2893
}
2902
2894
});
2903
- return ex ;
2895
+ return exc_storage. get_stored () ;
2904
2896
}
2905
2897
2906
2898
PYBIND11_NAMESPACE_END (detail)
0 commit comments