18 #if defined(PYPY_VERSION)
19 # error Embedding the interpreter is not supported with PyPy
22 #define PYBIND11_EMBEDDED_MODULE_IMPL(name) \
23 extern "C" PyObject *pybind11_init_impl_##name(); \
24 extern "C" PyObject *pybind11_init_impl_##name() { return pybind11_init_wrapper_##name(); }
41 #define PYBIND11_EMBEDDED_MODULE(name, variable) \
42 static ::pybind11::module_::module_def PYBIND11_CONCAT(pybind11_module_def_, name); \
43 static void PYBIND11_CONCAT(pybind11_init_, name)(::pybind11::module_ &); \
44 static PyObject PYBIND11_CONCAT(*pybind11_init_wrapper_, name)() { \
45 auto m = ::pybind11::module_::create_extension_module( \
46 PYBIND11_TOSTRING(name), nullptr, &PYBIND11_CONCAT(pybind11_module_def_, name)); \
48 PYBIND11_CONCAT(pybind11_init_, name)(m); \
51 PYBIND11_CATCH_INIT_EXCEPTIONS \
53 PYBIND11_EMBEDDED_MODULE_IMPL(name) \
54 ::pybind11::detail::embedded_module PYBIND11_CONCAT(pybind11_module_, name)( \
55 PYBIND11_TOSTRING(name), PYBIND11_CONCAT(pybind11_init_impl_, name)); \
56 void PYBIND11_CONCAT(pybind11_init_, name)(::pybind11::module_ \
57 & variable) // NOLINT(bugprone-macro-parentheses)
66 if (Py_IsInitialized() != 0) {
67 pybind11_fail(
"Can't add new modules after the interpreter has been initialized");
85 wchar_t *widened_arg = Py_DecodeLocale(safe_arg,
nullptr);
90 if (Py_IsInitialized() != 0) {
95 #if !defined(PYBIND11_PYCONFIG_SUPPORT_PY_VERSION_HEX)
96 # define PYBIND11_PYCONFIG_SUPPORT_PY_VERSION_HEX (0x03080000)
99 #if PY_VERSION_HEX < PYBIND11_PYCONFIG_SUPPORT_PY_VERSION_HEX
102 const char *
const *argv,
103 bool add_program_dir_to_path) {
105 Py_InitializeEx(init_signal_handlers ? 1 : 0);
109 bool special_case = (argv ==
nullptr || argc <= 0);
111 const char *
const empty_argv[]{
"\0"};
112 const char *
const *safe_argv = special_case ? empty_argv : argv;
117 auto argv_size =
static_cast<size_t>(argc);
119 std::unique_ptr<wchar_t *[]> widened_argv(
new wchar_t *[argv_size]);
120 std::vector<std::unique_ptr<wchar_t[], detail::wide_char_arg_deleter>> widened_argv_entries;
121 widened_argv_entries.reserve(argv_size);
122 for (
size_t ii = 0; ii < argv_size; ++ii) {
124 if (!widened_argv_entries.back()) {
129 widened_argv[ii] = widened_argv_entries.back().get();
132 auto *pysys_argv = widened_argv.get();
134 PySys_SetArgvEx(argc, pysys_argv,
static_cast<int>(add_program_dir_to_path));
140 #if PY_VERSION_HEX >= PYBIND11_PYCONFIG_SUPPORT_PY_VERSION_HEX
143 const char *
const *argv =
nullptr,
144 bool add_program_dir_to_path =
true) {
146 PyStatus status = PyConfig_SetBytesArgv(config, argc,
const_cast<char *
const *
>(argv));
147 if (PyStatus_Exception(status) != 0) {
150 PyConfig_Clear(config);
151 throw std::runtime_error(PyStatus_IsError(status) != 0 ? status.err_msg
152 :
"Failed to prepare CPython");
154 status = Py_InitializeFromConfig(config);
155 if (PyStatus_Exception(status) != 0) {
156 PyConfig_Clear(config);
157 throw std::runtime_error(PyStatus_IsError(status) != 0 ? status.err_msg
158 :
"Failed to init CPython");
160 if (add_program_dir_to_path) {
161 PyRun_SimpleString(
"import sys, os.path; "
162 "sys.path.insert(0, "
163 "os.path.abspath(os.path.dirname(sys.argv[0])) "
164 "if sys.argv and os.path.exists(sys.argv[0]) else '')");
166 PyConfig_Clear(config);
191 const char *
const *argv =
nullptr,
192 bool add_program_dir_to_path =
true) {
193 #if PY_VERSION_HEX < PYBIND11_PYCONFIG_SUPPORT_PY_VERSION_HEX
195 init_signal_handlers, argc, argv, add_program_dir_to_path);
198 PyConfig_InitPythonConfig(&config);
200 config.parse_argv = 0;
202 config.install_signal_handlers = init_signal_handlers ? 1 : 0;
248 if (
object internals_obj
259 if (internals_ptr_ptr) {
260 delete *internals_ptr_ptr;
261 *internals_ptr_ptr =
nullptr;
284 const char *
const *argv =
nullptr,
285 bool add_program_dir_to_path =
true) {
289 #if PY_VERSION_HEX >= PYBIND11_PYCONFIG_SUPPORT_PY_VERSION_HEX
292 const char *
const *argv =
nullptr,
293 bool add_program_dir_to_path =
true) {