]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
Rework libpython initialization to use PyConfig
authorNick Porter <nick@portercomputing.co.uk>
Tue, 23 May 2023 15:43:53 +0000 (16:43 +0100)
committerArran Cudbard-Bell <a.cudbardb@freeradius.org>
Thu, 25 May 2023 13:43:52 +0000 (09:43 -0400)
Makes minimum Python version required 3.8

src/modules/rlm_python/rlm_python.c

index 55940c47119cbbd29d8f7e4fecb2063faf63f5c2..95486ae8e6afb2acf76cd7a6a18f0e7081db517c 100644 (file)
@@ -1101,6 +1101,9 @@ static int libpython_init(void)
                                                .subsq_prefix = "rlm_python - ", \
                                           }, \
                                           _fmt,  ## __VA_ARGS__)
+       PyConfig        config;
+       PyStatus        status;
+       wchar_t         *wide_name;
 
        fr_assert(!Py_IsInitialized());
 
@@ -1115,6 +1118,22 @@ static int libpython_init(void)
        python_dlhandle = dl_open_by_sym("Py_IsInitialized", RTLD_NOW | RTLD_GLOBAL);
        if (!python_dlhandle) LOAD_WARN("Failed loading libpython symbols into global symbol table");
 
+       PyConfig_InitPythonConfig(&config);
+
+       /*
+        *      Set program name (i.e. the software calling the interpreter)
+        *      The value of argv[0] as a wide char string
+        */
+       wide_name = Py_DecodeLocale(main_config->name, NULL);
+       status = PyConfig_SetString(&config, &config.program_name, wide_name);
+       PyMem_RawFree(wide_name);
+
+       if (PyStatus_Exception(status)) {
+       fail:
+               PyConfig_Clear(&config);
+               return -1;
+       }
+
        /*
         *      Python 3 introduces the concept of a
         *      "inittab", i.e. a list of modules which
@@ -1122,22 +1141,19 @@ static int libpython_init(void)
         *      interpreter is spawned.
         */
        PyImport_AppendInittab("freeradius", python_module_init);
-       LSAN_DISABLE(Py_InitializeEx(0));       /* Don't override signal handlers - noop on subs calls */
+
+       config.install_signal_handlers = 0;     /* Don't override signal handlers - noop on subs calls */
+
+       LSAN_DISABLE(status = Py_InitializeFromConfig(&config));
+       if (PyStatus_Exception(status)) goto fail;
+
+       PyConfig_Clear(&config);
 
        /*
         *      Get the default search path so we can append to it.
         */
        default_path = Py_EncodeLocale(Py_GetPath(), NULL);
 
-       /*
-        *      Set program name (i.e. the software calling the interpreter)
-        */
-       {
-               wchar_t *wide_name;
-               wide_name = Py_DecodeLocale(main_config->name, NULL);
-               Py_SetProgramName(wide_name);           /* The value of argv[0] as a wide char string */
-               PyMem_RawFree(wide_name);
-       }
        global_interpreter = PyEval_SaveThread();       /* Store reference to the main interpreter and release the GIL */
 
        return 0;