Skip to content

Commit 8aeb76f

Browse files
committed
Use Py_InitializeFromConfig() not Py_InitializeEx() for Python >= 3.8.
Avoids deprecation warnings on Python 3.12. --HG-- branch : 1.9
1 parent 5a6f0d5 commit 8aeb76f

1 file changed

Lines changed: 18 additions & 4 deletions

File tree

plugins/python/python_plugin_common.c

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -366,18 +366,32 @@ _python_plugin_register_plugin_in_py_ctx(void)
366366
debug_decl(_python_plugin_register_plugin_in_py_ctx, PYTHON_DEBUG_PLUGIN_LOAD);
367367

368368
if (!Py_IsInitialized()) {
369+
if (_save_inittab() != SUDO_RC_OK)
370+
debug_return_int(SUDO_RC_ERROR);
371+
PyImport_AppendInittab("sudo", sudo_module_init);
372+
369373
// Disable environment variables effecting the python interpreter
370374
// This is important since we are running code here as root, the
371375
// user should not be able to alter what is running any how.
376+
#if (PY_MAJOR_VERSION > 3) || (PY_MINOR_VERSION >= 8)
377+
PyStatus status;
378+
PyConfig config;
379+
380+
PyConfig_InitPythonConfig(&config);
381+
config.isolated = 1;
382+
config.use_environment = 0;
383+
config.user_site_directory = 0;
384+
status = Py_InitializeFromConfig(&config);
385+
PyConfig_Clear(&config);
386+
if (PyStatus_Exception(status))
387+
debug_return_int(SUDO_RC_ERROR);
388+
#else
372389
Py_IgnoreEnvironmentFlag = 1;
373390
Py_IsolatedFlag = 1;
374391
Py_NoUserSiteDirectory = 1;
375392

376-
if (_save_inittab() != SUDO_RC_OK)
377-
debug_return_int(SUDO_RC_ERROR);
378-
379-
PyImport_AppendInittab("sudo", sudo_module_init);
380393
Py_InitializeEx(0);
394+
#endif
381395
py_ctx.py_main_interpreter = PyThreadState_Get();
382396

383397
// This ensures we import "sudo" module in the main interpreter,

0 commit comments

Comments
 (0)