diff --git a/include/pybind11/embed.h b/include/pybind11/embed.h index a456e80a6..b7bd3bec7 100644 --- a/include/pybind11/embed.h +++ b/include/pybind11/embed.h @@ -41,15 +41,18 @@ #define PYBIND11_EMBEDDED_MODULE(name, variable) \ static ::pybind11::module_::module_def PYBIND11_CONCAT(pybind11_module_def_, name); \ static void PYBIND11_CONCAT(pybind11_init_, name)(::pybind11::module_ &); \ - static PyObject PYBIND11_CONCAT(*pybind11_init_wrapper_, name)() { \ - auto m = ::pybind11::module_::create_extension_module( \ - PYBIND11_TOSTRING(name), nullptr, &PYBIND11_CONCAT(pybind11_module_def_, name)); \ - try { \ - PYBIND11_CONCAT(pybind11_init_, name)(m); \ - return m.ptr(); \ - } \ - PYBIND11_CATCH_INIT_EXCEPTIONS \ - return nullptr; \ + static PyObject *PYBIND11_CONCAT(pybind11_init_wrapper_, name)() { \ + static auto result = []() -> PyObject * { \ + auto m = ::pybind11::module_::create_extension_module( \ + PYBIND11_TOSTRING(name), nullptr, &PYBIND11_CONCAT(pybind11_module_def_, name)); \ + try { \ + PYBIND11_CONCAT(pybind11_init_, name)(m); \ + return m.ptr(); \ + } \ + PYBIND11_CATCH_INIT_EXCEPTIONS \ + return nullptr; \ + }(); \ + return result; \ } \ PYBIND11_EMBEDDED_MODULE_IMPL(name) \ ::pybind11::detail::embedded_module PYBIND11_CONCAT(pybind11_module_, name)( \