Skip to content

Commit e417c1c

Browse files
committed
mgr: set argv for python in PyModuleRegistry
before this change, we setup the progname for Python interpreter, but setup the argv for it in PyModule. and we are using deprecated API to initialize Python interpreter. in this change, let's do this in a single place for better maintainability. also, take this opportunity, to use the non-deprecated API to initialize interpreter on Python >= 3.8. this silence the warning when compiling ceph-mgr with CPython 3.12: ``` /var/ssd/ceph/src/mgr/PyModule.cc: In member function ‘int PyModule::load(PyThreadState*)’: /var/ssd/ceph/src/mgr/PyModule.cc:363:20: warning: ‘void PySys_SetArgv(int, wchar_t**)’ is deprecated [-Wdeprecated-declarations] 363 | PySys_SetArgv(1, (wchar_t**)argv); | ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~ In file included from /usr/include/python3.12/Python.h:96, from /var/ssd/ceph/src/mgr/BaseMgrModule.h:4, from /var/ssd/ceph/src/mgr/PyModule.cc:14: /usr/include/python3.12/sysmodule.h:13:38: note: declared here 13 | Py_DEPRECATED(3.11) PyAPI_FUNC(void) PySys_SetArgv(int, wchar_t **); | ^~~~~~~~~~~~~ ``` Signed-off-by: Kefu Chai <[email protected]>
1 parent a21d80c commit e417c1c

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

src/mgr/PyModule.cc

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -357,10 +357,6 @@ int PyModule::load(PyThreadState *pMainThreadState)
357357
return -EINVAL;
358358
} else {
359359
pMyThreadState.set(thread_state);
360-
// Some python modules do not cope with an unpopulated argv, so lets
361-
// fake one. This step also picks up site-packages into sys.path.
362-
const wchar_t *argv[] = {L"ceph-mgr"};
363-
PySys_SetArgv(1, (wchar_t**)argv);
364360
// Configure sys.path to include mgr_module_path
365361
string paths = (g_conf().get_val<std::string>("mgr_module_path") + ':' +
366362
get_site_packages() + ':');

src/mgr/PyModuleRegistry.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ void PyModuleRegistry::init()
7070
PyStatus status;
7171
status = PyConfig_SetString(&py_config, &py_config.program_name, WCHAR(MGR_PYTHON_EXECUTABLE));
7272
ceph_assertf(!PyStatus_Exception(status), "PyConfig_SetString: %s:%s", status.func, status.err_msg);
73+
// Some python modules do not cope with an unpopulated argv, so lets
74+
// fake one. This step also picks up site-packages into sys.path.
75+
const wchar_t* argv[] = {L"ceph-mgr"};
76+
status = PyConfig_SetArgv(&py_config, 1, (wchar_t *const *)argv);
77+
ceph_assertf(!PyStatus_Exception(status), "PyConfig_SetArgv: %s:%s", status.func, status.err_msg);
7378
// Add more modules
7479
if (g_conf().get_val<bool>("daemonize")) {
7580
PyImport_AppendInittab("ceph_logger", PyModule::init_ceph_logger);
@@ -85,6 +90,8 @@ void PyModuleRegistry::init()
8590
}
8691
PyImport_AppendInittab("ceph_module", PyModule::init_ceph_module);
8792
Py_InitializeEx(0);
93+
const wchar_t *argv[] = {L"ceph-mgr"};
94+
PySys_SetArgv(1, (wchar_t**)argv);
8895
#endif // PY_VERSION_HEX >= 0x03080000
8996
#undef WCHAR
9097

0 commit comments

Comments
 (0)