]> git.ipfire.org Git - thirdparty/unbound.git/commitdiff
- Complementary fix for distutils.sysconfig deprecation in Python 3.10
authorGeorge Thessalonikefs <george@nlnetlabs.nl>
Wed, 9 Nov 2022 10:41:28 +0000 (11:41 +0100)
committerGeorge Thessalonikefs <george@nlnetlabs.nl>
Wed, 9 Nov 2022 10:41:28 +0000 (11:41 +0100)
  to commit 62c5039ab9da42713e006e840b7578e01d66e7f2.

doc/Changelog
pythonmod/pythonmod.c

index b21f3415d661e854ab64cc1dd50f79807d848e92..85a22e58edc66e998a368acefa0ca8af8e227903 100644 (file)
@@ -1,3 +1,7 @@
+9 November 2022: George
+       - Complementary fix for distutils.sysconfig deprecation in Python 3.10
+         to commit 62c5039ab9da42713e006e840b7578e01d66e7f2.
+
 8 November 2022: Wouter
        - Fix to ignore tcp events for closed comm points.
        - Fix to make sure to not read again after a tcp comm point is closed.
index 4bea54e6aceb6c5918fe8fe8e5b6e3fab82810d9..7c7da548994aeb019fecbe4bc644981b41991ddd 100644 (file)
@@ -255,7 +255,7 @@ cleanup:
 int pythonmod_init(struct module_env* env, int id)
 {
    int py_mod_idx = py_mod_count++;
-    
+
    /* Initialize module */
    FILE* script_py = NULL;
    PyObject* py_init_arg, *res;
@@ -316,23 +316,37 @@ int pythonmod_init(struct module_env* env, int id)
 
    if (py_mod_count==1) {
       /* Initialize Python */
-      PyRun_SimpleString("import sys \n");
+      if(PyRun_SimpleString("import sys \n") < 0 ) {
+         goto python_init_fail;
+      }
       PyRun_SimpleString("sys.path.append('.') \n");
+      PyRun_SimpleString("sys.path.append('"RUN_DIR"') \n");
+      PyRun_SimpleString("sys.path.append('"SHARE_DIR"') \n");
       if(env->cfg->directory && env->cfg->directory[0]) {
          char wdir[1524];
          snprintf(wdir, sizeof(wdir), "sys.path.append('%s') \n",
          env->cfg->directory);
          PyRun_SimpleString(wdir);
       }
-      PyRun_SimpleString("sys.path.append('"RUN_DIR"') \n");
-      PyRun_SimpleString("sys.path.append('"SHARE_DIR"') \n");
-      PyRun_SimpleString("import distutils.sysconfig \n");
-      PyRun_SimpleString("sys.path.append(distutils.sysconfig.get_python_lib(1,0)) \n");
-      if (PyRun_SimpleString("from unboundmodule import *\n") < 0)
+      /* Check if sysconfig is there and use that instead of distutils;
+       * distutils.sysconfig is deprecated in Python 3.10. */
+      if(PyRun_SimpleString("import sysconfig \n") < 0) {
+         log_info("pythonmod: module sysconfig not available; "
+            "falling back to distutils.sysconfig.");
+         if(PyRun_SimpleString("import distutils.sysconfig \n") < 0
+            || PyRun_SimpleString("sys.path.append("
+            "distutils.sysconfig.get_python_lib(1,0)) \n") < 0) {
+            goto python_init_fail;
+         }
+      } else {
+         if(PyRun_SimpleString("sys.path.append("
+            "sysconfig.get_path('platlib')) \n") < 0) {
+            goto python_init_fail;
+         }
+      }
+      if(PyRun_SimpleString("from unboundmodule import *\n") < 0)
       {
-         log_err("pythonmod: cannot initialize core module: unboundmodule.py");
-         PyGILState_Release(gil);
-         return 0;
+         goto python_init_fail;
       }
    }
 
@@ -480,6 +494,11 @@ int pythonmod_init(struct module_env* env, int id)
    PyGILState_Release(gil);
 
    return 1;
+
+python_init_fail:
+   log_err("pythonmod: cannot initialize core module: unboundmodule.py");
+   PyGILState_Release(gil);
+   return 0;
 }
 
 void pythonmod_deinit(struct module_env* env, int id)