Skip to content

Commit 1cd2513

Browse files
committed
fix: apply logic in Python manually
Signed-off-by: Henry Schreiner <[email protected]>
1 parent 9887cdc commit 1cd2513

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

include/pybind11/embed.h

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,19 +149,27 @@ inline void initialize_interpreter(bool init_signal_handlers = true,
149149
PySys_SetArgvEx(argc, pysys_argv, static_cast<int>(add_program_dir_to_path));
150150
#else
151151
PyConfig config;
152-
PyConfig_InitPythonConfig(&config);
153-
config.safe_path = add_program_dir_to_path ? 0 : 1;
152+
PyConfig_InitIsolatedConfig(&config);
154153
config.install_signal_handlers = init_signal_handlers ? 1 : 0;
155154

156155
PyStatus status = PyConfig_SetBytesArgv(&config, argc, const_cast<char *const *>(argv));
157156
if (PyStatus_Exception(status)) {
158157
// A failure here indicates a character-encoding failure or the python
159158
// interpreter out of memory. Give up.
160159
PyConfig_Clear(&config);
161-
return;
160+
throw std::runtime_error("Failed to prepare CPython");
162161
}
163-
Py_InitializeFromConfig(&config);
162+
status = Py_InitializeFromConfig(&config);
164163
PyConfig_Clear(&config);
164+
if (PyStatus_Exception(status)) {
165+
throw std::runtime_error("Failed to init CPython");
166+
}
167+
if (add_program_dir_to_path) {
168+
PyRun_SimpleString("import sys, os.path; "
169+
"sys.path.insert(0, "
170+
"os.path.abspath(os.path.dirname(sys.argv[0])) "
171+
"if sys.argv and os.path.exists(sys.argv[0]) else '')");
172+
}
165173
#endif
166174
}
167175

0 commit comments

Comments
 (0)