Skip to content

Commit 65bfec2

Browse files
SPKorhonenstefanseefeld
authored andcommitted
Fix static object initialization under Visual Studio 2017 (boostorg#208)
Fix static object initialization under VS 15.7.2
1 parent 4f6d547 commit 65bfec2

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

include/boost/python/detail/caller.hpp

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,23 @@ struct converter_target_type <void_result_to_python >
109109
return 0;
110110
}
111111
};
112+
113+
// Generation of ret moved from caller_arity<N>::impl::signature to here due to "feature" in MSVC 15.7.2 with /O2
114+
// which left the ret uninitialized and caused segfaults in Python interpreter.
115+
template<class Policies, class Sig> const signature_element* get_ret()
116+
{
117+
typedef BOOST_DEDUCED_TYPENAME Policies::template extract_return_type<Sig>::type rtype;
118+
typedef typename select_result_converter<Policies, rtype>::type result_converter;
119+
120+
static const signature_element ret = {
121+
(is_void<rtype>::value ? "void" : type_id<rtype>().name())
122+
, &detail::converter_target_type<result_converter>::get_pytype
123+
, boost::detail::indirect_traits::is_reference_to_non_const<rtype>::value
124+
};
125+
126+
return &ret;
127+
};
128+
112129
#endif
113130

114131

@@ -229,16 +246,12 @@ struct caller_arity<N>
229246
{
230247
const signature_element * sig = detail::signature<Sig>::elements();
231248
#ifndef BOOST_PYTHON_NO_PY_SIGNATURES
249+
// MSVC 15.7.2, when compiling to /O2 left the static const signature_element ret,
250+
// originally defined here, uninitialized. This in turn led to SegFault in Python interpreter.
251+
// Issue is resolved by moving the generation of ret to separate function in detail namespace (see above).
252+
const signature_element * ret = detail::get_ret<Policies, Sig>();
232253

233-
typedef BOOST_DEDUCED_TYPENAME Policies::template extract_return_type<Sig>::type rtype;
234-
typedef typename select_result_converter<Policies, rtype>::type result_converter;
235-
236-
static const signature_element ret = {
237-
(is_void<rtype>::value ? "void" : type_id<rtype>().name())
238-
, &detail::converter_target_type<result_converter>::get_pytype
239-
, boost::detail::indirect_traits::is_reference_to_non_const<rtype>::value
240-
};
241-
py_func_sig_info res = {sig, &ret };
254+
py_func_sig_info res = {sig, ret };
242255
#else
243256
py_func_sig_info res = {sig, sig };
244257
#endif

0 commit comments

Comments
 (0)