Skip to content

Commit 28ec485

Browse files
committed
Merge branch 'master' into sh_merge_master
2 parents 4c5b88a + bf88e29 commit 28ec485

File tree

2 files changed

+11
-17
lines changed

2 files changed

+11
-17
lines changed

include/pybind11/cast.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,15 @@ struct type_uses_smart_holder_type_caster {
7272
// Shortcut for calling a caster's `cast_op_type` cast operator for casting a type_caster to a T
7373
template <typename T>
7474
typename make_caster<T>::template cast_op_type<T> cast_op(make_caster<T> &caster) {
75-
return caster.operator typename make_caster<T>::template cast_op_type<T>();
75+
using result_t = typename make_caster<T>::template cast_op_type<T>; // See PR #4893
76+
return caster.operator result_t();
7677
}
7778
template <typename T>
7879
typename make_caster<T>::template cast_op_type<typename std::add_rvalue_reference<T>::type>
7980
cast_op(make_caster<T> &&caster) {
80-
return std::move(caster).operator typename make_caster<T>::
81-
template cast_op_type<typename std::add_rvalue_reference<T>::type>();
81+
using result_t = typename make_caster<T>::template cast_op_type<
82+
typename std::add_rvalue_reference<T>::type>; // See PR #4893
83+
return std::move(caster).operator result_t();
8284
}
8385

8486
template <typename type>

include/pybind11/pybind11.h

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "detail/smart_holder_sfinae_hooks_only.h"
1616
#include "attr.h"
1717
#include "gil.h"
18+
#include "gil_safe_call_once.h"
1819
#include "options.h"
1920
#include "typing.h"
2021

@@ -2869,23 +2870,14 @@ class exception : public object {
28692870
};
28702871

28712872
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-
}
28802873

28812874
// Helper function for register_exception and register_local_exception
28822875
template <typename CppException>
28832876
exception<CppException> &
28842877
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); });
28892881

28902882
auto register_func
28912883
= isLocal ? &register_local_exception_translator : &register_exception_translator;
@@ -2897,10 +2889,10 @@ register_exception_impl(handle scope, const char *name, handle base, bool isLoca
28972889
try {
28982890
std::rethrow_exception(p);
28992891
} catch (const CppException &e) {
2900-
set_error(detail::get_exception_object<CppException>(), e.what());
2892+
set_error(exc_storage.get_stored(), e.what());
29012893
}
29022894
});
2903-
return ex;
2895+
return exc_storage.get_stored();
29042896
}
29052897

29062898
PYBIND11_NAMESPACE_END(detail)

0 commit comments

Comments
 (0)