Skip to content

Commit a21d80c

Browse files
committed
mgr: stop using deprecated API to initialize Python
Py_SetProgramName() is deprecated since CPython 3.11, see https://docs.python.org/3/c-api/init_config.html . `Py_InitializeFromConfig()` and friends were introduced by CPython 3.8, but we still need to support CPython 3.6 which is shipped by CentOS8. so we have to be backward compatible with the older Python versions. so let's use new machinary to initialize the Python interpretor if the tree is compiled with CPython 3.8 and up, i.e., PY_VERSION_HEX >= 0x03080000. so that this piece of code can be tested on ubuntu:jammy, which ships Python 3.10, see https://packages.ubuntu.com/jammy/amd64/python3 this change addresses following compiling warning: ``` [428/753] Building CXX object src/mgr/CMakeFiles/ceph-mgr.dir/PyModuleRegistry.cc.o /var/ssd/ceph/src/mgr/PyModuleRegistry.cc: In member function ‘void PyModuleRegistry::init()’: /var/ssd/ceph/src/mgr/PyModuleRegistry.cc:49:20: warning: ‘void Py_SetProgramName(const wchar_t*)’ is deprecated [-Wdeprecated-declarations] 49 | Py_SetProgramName(const_cast<wchar_t*>(WCHAR(MGR_PYTHON_EXECUTABLE))); | ~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In file included from /usr/include/python3.12/Python.h:94, from /var/ssd/ceph/src/mgr/PyModule.h:22, from /var/ssd/ceph/src/mgr/PyModuleRegistry.h:18, from /var/ssd/ceph/src/mgr/PyModuleRegistry.cc:14: /usr/include/python3.12/pylifecycle.h:37:38: note: declared here 37 | Py_DEPRECATED(3.11) PyAPI_FUNC(void) Py_SetProgramName(const wchar_t *); | ^~~~~~~~~~~~~~~~~` ``` Signed-off-by: Kefu Chai <[email protected]>
1 parent 4103b56 commit a21d80c

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

src/mgr/PyModuleRegistry.cc

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "PyModuleRegistry.h"
1515

1616
#include <filesystem>
17+
#include <boost/scope_exit.hpp>
1718

1819
#include "include/stringify.h"
1920
#include "common/errno.h"
@@ -46,14 +47,47 @@ void PyModuleRegistry::init()
4647

4748
// Set up global python interpreter
4849
#define WCHAR(s) L ## #s
50+
#if PY_VERSION_HEX >= 0x03080000
51+
PyConfig py_config;
52+
// do not enable isolated mode, otherwise we would not be able to have access
53+
// to the site packages. since we cannot import any module before initializing
54+
// the interpreter, we would not be able to use "site" module for retrieving
55+
// the path to site packager. we import "site" module for retrieving
56+
// sitepackages in Python < 3.8 though, this does not apply to the
57+
// initialization with PyConfig.
58+
PyConfig_InitPythonConfig(&py_config);
59+
BOOST_SCOPE_EXIT_ALL(&py_config) {
60+
PyConfig_Clear(&py_config);
61+
};
62+
#if PY_VERSION_HEX >= 0x030b0000
63+
py_config.safe_path = 0;
64+
#endif
65+
py_config.parse_argv = 0;
66+
py_config.configure_c_stdio = 0;
67+
py_config.install_signal_handlers = 0;
68+
py_config.pathconfig_warnings = 0;
69+
70+
PyStatus status;
71+
status = PyConfig_SetString(&py_config, &py_config.program_name, WCHAR(MGR_PYTHON_EXECUTABLE));
72+
ceph_assertf(!PyStatus_Exception(status), "PyConfig_SetString: %s:%s", status.func, status.err_msg);
73+
// Add more modules
74+
if (g_conf().get_val<bool>("daemonize")) {
75+
PyImport_AppendInittab("ceph_logger", PyModule::init_ceph_logger);
76+
}
77+
PyImport_AppendInittab("ceph_module", PyModule::init_ceph_module);
78+
status = Py_InitializeFromConfig(&py_config);
79+
ceph_assertf(!PyStatus_Exception(status), "Py_InitializeFromConfig: %s:%s", status.func, status.err_msg);
80+
#else
4981
Py_SetProgramName(const_cast<wchar_t*>(WCHAR(MGR_PYTHON_EXECUTABLE)));
50-
#undef WCHAR
5182
// Add more modules
5283
if (g_conf().get_val<bool>("daemonize")) {
5384
PyImport_AppendInittab("ceph_logger", PyModule::init_ceph_logger);
5485
}
5586
PyImport_AppendInittab("ceph_module", PyModule::init_ceph_module);
5687
Py_InitializeEx(0);
88+
#endif // PY_VERSION_HEX >= 0x03080000
89+
#undef WCHAR
90+
5791
#if PY_VERSION_HEX < 0x03090000
5892
// Let CPython know that we will be calling it back from other
5993
// threads in future.

0 commit comments

Comments
 (0)