From: W.C.A. Wijngaards Date: Mon, 3 Jul 2023 08:23:37 +0000 (+0200) Subject: - Fix #906: warning: ‘Py_SetProgramName’ is deprecated. X-Git-Tag: release-1.18.0rc1~24^2~19 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=48a6ff14a431dbcab30a53322fe7bdc0a6925556;p=thirdparty%2Funbound.git - Fix #906: warning: ‘Py_SetProgramName’ is deprecated. --- diff --git a/doc/Changelog b/doc/Changelog index 0eb1c9ef1..7ecbe01e0 100644 --- a/doc/Changelog +++ b/doc/Changelog @@ -1,3 +1,6 @@ +3 July 2023: Wouter + - Fix #906: warning: ‘Py_SetProgramName’ is deprecated. + 29 June 2023: George - More fixes for reference counting for python module and clean up failure code. diff --git a/pythonmod/pythonmod.c b/pythonmod/pythonmod.c index 5d30b1f87..628308612 100644 --- a/pythonmod/pythonmod.c +++ b/pythonmod/pythonmod.c @@ -303,18 +303,58 @@ int pythonmod_init(struct module_env* env, int id) /* Initialize Python libraries */ if (py_mod_count==1 && !Py_IsInitialized()) { +#if PY_VERSION_HEX >= 0x03080000 + PyStatus status; + PyPreConfig preconfig; + PyConfig config; +#endif #if PY_MAJOR_VERSION >= 3 wchar_t progname[8]; mbstowcs(progname, "unbound", 8); #else char *progname = "unbound"; #endif +#if PY_VERSION_HEX < 0x03080000 Py_SetProgramName(progname); +#else + /* Python must be preinitialized, before the PyImport_AppendInittab + * call. */ + PyPreConfig_InitPythonConfig(&preconfig); + status = Py_PreInitialize(&preconfig); + if(PyStatus_Exception(status)) { + log_err("python exception in Py_PreInitialize: %s%s%s", + (status.func?status.func:""), (status.func?": ":""), + (status.err_msg?status.err_msg:"")); + return 0; + } +#endif Py_NoSiteFlag = 1; #if PY_MAJOR_VERSION >= 3 PyImport_AppendInittab(SWIG_name, (void*)SWIG_init); #endif +#if PY_VERSION_HEX < 0x03080000 Py_Initialize(); +#else + PyConfig_InitPythonConfig(&config); + status = PyConfig_SetString(&config, &config.program_name, progname); + if(PyStatus_Exception(status)) { + log_err("python exception in PyConfig_SetString(.. program_name ..): %s%s%s", + (status.func?status.func:""), (status.func?": ":""), + (status.err_msg?status.err_msg:"")); + PyConfig_Clear(&config); + return 0; + } + config.site_import = 0; + status = Py_InitializeFromConfig(&config); + if(PyStatus_Exception(status)) { + log_err("python exception in Py_InitializeFromConfig: %s%s%s", + (status.func?status.func:""), (status.func?": ":""), + (status.err_msg?status.err_msg:"")); + PyConfig_Clear(&config); + return 0; + } + PyConfig_Clear(&config); +#endif #if PY_MAJOR_VERSION <= 2 || (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION <= 6) /* initthreads only for python 3.6 and older */ PyEval_InitThreads();