]> git.ipfire.org Git - thirdparty/unbound.git/commitdiff
- Fix python module install path detection.
authorW.C.A. Wijngaards <wouter@nlnetlabs.nl>
Mon, 9 Jan 2023 14:03:38 +0000 (15:03 +0100)
committerW.C.A. Wijngaards <wouter@nlnetlabs.nl>
Mon, 9 Jan 2023 14:03:38 +0000 (15:03 +0100)
acx_python.m4
configure
doc/Changelog
pythonmod/pythonmod.c

index 16c0c6fd943f56e0b4f5970d818214b042abcd7f..c945d6c8989e605de6e9d00de10683b01d11b519 100644 (file)
@@ -17,33 +17,62 @@ AC_DEFUN([AC_PYTHON_DEVEL],[
                PYTHON_VERSION=`$PYTHON -c "import sys; \
                        print(sys.version.split()[[0]])"`
        fi
+       # calculate the version number components.
+       [
+       v="$PYTHON_VERSION"
+       PYTHON_VERSION_MAJOR=`echo $v | sed 's/[^0-9].*//'`
+       if test -z "$PYTHON_VERSION_MAJOR"; then PYTHON_VERSION_MAJOR="0"; fi
+       v=`echo $v | sed -e 's/^[0-9]*$//' -e 's/[0-9]*[^0-9]//'`
+       PYTHON_VERSION_MINOR=`echo $v | sed 's/[^0-9].*//'`
+       if test -z "$PYTHON_VERSION_MINOR"; then PYTHON_VERSION_MINOR="0"; fi
+       v=`echo $v | sed -e 's/^[0-9]*$//' -e 's/[0-9]*[^0-9]//'`
+       PYTHON_VERSION_PATCH=`echo $v | sed 's/[^0-9].*//'`
+       if test -z "$PYTHON_VERSION_PATCH"; then PYTHON_VERSION_PATCH="0"; fi
+       ]
 
-       # Check if you have sysconfig
-       AC_MSG_CHECKING([for the sysconfig Python module])
-        if ac_sysconfig_result=`$PYTHON -c "import sysconfig" 2>&1`; then
+       # For some systems, sysconfig exists, but has the wrong paths,
+       # on Debian 10, for python 2.7 and 3.7. So, we check the version,
+       # and for older versions try distutils.sysconfig first. For newer
+       # versions>=3.10, where distutils.sysconfig is deprecated, use
+       # sysconfig first and then attempt the other one.
+       py_distutils_first="no"
+       if test $PYTHON_VERSION_MAJOR -lt 3; then
+               py_distutils_first="yes"
+       fi
+       if test $PYTHON_VERSION_MAJOR -eq 3 -a $PYTHON_VERSION_MINOR -lt 10; then
+               py_distutils_first="yes"
+       fi
+
+       # Check if you have the first module
+       if test "$py_distutils_first" = "yes"; then m="distutils"; else m="sysconfig"; fi
+       sysconfig_module=""
+       AC_MSG_CHECKING([for the $m Python module])
+        if ac_modulecheck_result1=`$PYTHON -c "import $m" 2>&1`; then
                 AC_MSG_RESULT([yes])
-               sysconfig_module="sysconfig"
-               # if yes, use sysconfig, because distutils is deprecated.
+               sysconfig_module="$m"
        else
                 AC_MSG_RESULT([no])
-               # if no, try to use distutils
+       fi
 
-               #
-               # Check if you have distutils, else fail
-               #
-               AC_MSG_CHECKING([for the distutils Python package])
-               if ac_distutils_result=`$PYTHON -c "import distutils" 2>&1`; then
+       # if not found, try the other one.
+       if test -z "$sysconfig_module"; then
+               if test "$py_distutils_first" = "yes"; then m2="sysconfig"; else m2="distutils"; fi
+               AC_MSG_CHECKING([for the $m2 Python module])
+               if ac_modulecheck_result2=`$PYTHON -c "import $m2" 2>&1`; then
                        AC_MSG_RESULT([yes])
+                       sysconfig_module="$m2"
                else
                        AC_MSG_RESULT([no])
-                       AC_MSG_ERROR([cannot import Python module "distutils".
-       Please check your Python installation. The error was:
-       $ac_distutils_result])
+                       AC_MSG_ERROR([cannot import Python module "$m", or "$m2".
+       Please check your Python installation. The errors are:
+       $m
+       $ac_modulecheck_result1
+       $m2
+       $ac_modulecheck_result2])
                        PYTHON_VERSION=""
                fi
-
-               sysconfig_module="distutils.sysconfig"
        fi
+       if test "$sysconfig_module" = "distutils"; then sysconfig_module="distutils.sysconfig"; fi
 
         #
         # Check for Python include path
index 5823e49f2f80688535730367a83aee210bffdc9e..0694dce86606626b0750bd059f12bcb4b17ae82c 100755 (executable)
--- a/configure
+++ b/configure
                PYTHON_VERSION=`$PYTHON -c "import sys; \
                        print(sys.version.split()[0])"`
        fi
+       # calculate the version number components.
+
+       v="$PYTHON_VERSION"
+       PYTHON_VERSION_MAJOR=`echo $v | sed 's/[^0-9].*//'`
+       if test -z "$PYTHON_VERSION_MAJOR"; then PYTHON_VERSION_MAJOR="0"; fi
+       v=`echo $v | sed -e 's/^[0-9]*$//' -e 's/[0-9]*[^0-9]//'`
+       PYTHON_VERSION_MINOR=`echo $v | sed 's/[^0-9].*//'`
+       if test -z "$PYTHON_VERSION_MINOR"; then PYTHON_VERSION_MINOR="0"; fi
+       v=`echo $v | sed -e 's/^[0-9]*$//' -e 's/[0-9]*[^0-9]//'`
+       PYTHON_VERSION_PATCH=`echo $v | sed 's/[^0-9].*//'`
+       if test -z "$PYTHON_VERSION_PATCH"; then PYTHON_VERSION_PATCH="0"; fi
+
+
+       # For some systems, sysconfig exists, but has the wrong paths,
+       # on Debian 10, for python 2.7 and 3.7. So, we check the version,
+       # and for older versions try distutils.sysconfig first. For newer
+       # versions>=3.10, where distutils.sysconfig is deprecated, use
+       # sysconfig first and then attempt the other one.
+       py_distutils_first="no"
+       if test $PYTHON_VERSION_MAJOR -lt 3; then
+               py_distutils_first="yes"
+       fi
+       if test $PYTHON_VERSION_MAJOR -eq 3 -a $PYTHON_VERSION_MINOR -lt 10; then
+               py_distutils_first="yes"
+       fi
 
-       # Check if you have sysconfig
-       { $as_echo "$as_me:${as_lineno-$LINENO}: checking for the sysconfig Python module" >&5
-$as_echo_n "checking for the sysconfig Python module... " >&6; }
-        if ac_sysconfig_result=`$PYTHON -c "import sysconfig" 2>&1`; then
+       # Check if you have the first module
+       if test "$py_distutils_first" = "yes"; then m="distutils"; else m="sysconfig"; fi
+       sysconfig_module=""
+       { $as_echo "$as_me:${as_lineno-$LINENO}: checking for the $m Python module" >&5
+$as_echo_n "checking for the $m Python module... " >&6; }
+        if ac_modulecheck_result1=`$PYTHON -c "import $m" 2>&1`; then
                 { $as_echo "$as_me:${as_lineno-$LINENO}: result: yes" >&5
 $as_echo "yes" >&6; }
-               sysconfig_module="sysconfig"
-               # if yes, use sysconfig, because distutils is deprecated.
+               sysconfig_module="$m"
        else
                 { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
 $as_echo "no" >&6; }
-               # if no, try to use distutils
-
-               #
-               # Check if you have distutils, else fail
-               #
-               { $as_echo "$as_me:${as_lineno-$LINENO}: checking for the distutils Python package" >&5
-$as_echo_n "checking for the distutils Python package... " >&6; }
-               if ac_distutils_result=`$PYTHON -c "import distutils" 2>&1`; then
+       fi
+
+       # if not found, try the other one.
+       if test -z "$sysconfig_module"; then
+               if test "$py_distutils_first" = "yes"; then m2="sysconfig"; else m2="distutils"; fi
+               { $as_echo "$as_me:${as_lineno-$LINENO}: checking for the $m2 Python module" >&5
+$as_echo_n "checking for the $m2 Python module... " >&6; }
+               if ac_modulecheck_result2=`$PYTHON -c "import $m2" 2>&1`; then
                        { $as_echo "$as_me:${as_lineno-$LINENO}: result: yes" >&5
 $as_echo "yes" >&6; }
+                       sysconfig_module="$m2"
                else
                        { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
 $as_echo "no" >&6; }
-                       as_fn_error $? "cannot import Python module \"distutils\".
-       Please check your Python installation. The error was:
-       $ac_distutils_result" "$LINENO" 5
+                       as_fn_error $? "cannot import Python module \"$m\", or \"$m2\".
+       Please check your Python installation. The errors are:
+       $m
+       $ac_modulecheck_result1
+       $m2
+       $ac_modulecheck_result2" "$LINENO" 5
                        PYTHON_VERSION=""
                fi
-
-               sysconfig_module="distutils.sysconfig"
        fi
+       if test "$sysconfig_module" = "distutils"; then sysconfig_module="distutils.sysconfig"; fi
 
         #
         # Check for Python include path
index d833207554d38608a0ee81a636b3a5fdfd9c10c7..f8d5b566832d6487f5dfc4cf21ac58994b8c9523 100644 (file)
@@ -1,3 +1,6 @@
+9 January 2023: Wouter
+       - Fix python module install path detection.
+
 6 January 2023: Wouter
        - Fix #823: Response change to NODATA for some ANY queries since
          1.12, tested on 1.16.1.
index 7c7da548994aeb019fecbe4bc644981b41991ddd..28ce0eec4f170d32527493b798defd555eae009e 100644 (file)
@@ -330,6 +330,27 @@ int pythonmod_init(struct module_env* env, int id)
       }
       /* Check if sysconfig is there and use that instead of distutils;
        * distutils.sysconfig is deprecated in Python 3.10. */
+#if PY_MAJOR_VERSION <= 2 || (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION <= 9)
+      /* For older versions, first try distutils.sysconfig because the
+       * sysconfig paths may contain wrong values, eg. on Debian10 for
+       * python 2.7 and 3.7. */
+      if(PyRun_SimpleString("import distutils.sysconfig \n") < 0) {
+         log_info("pythonmod: module distutils.sysconfig not available; "
+            "falling back to sysconfig.");
+         if(PyRun_SimpleString("import sysconfig \n") < 0
+            || PyRun_SimpleString("sys.path.append("
+            "sysconfig.get_path('platlib')) \n") < 0) {
+            goto python_init_fail;
+         }
+      } else {
+         if(PyRun_SimpleString("sys.path.append("
+            "distutils.sysconfig.get_python_lib(1,0)) \n") < 0) {
+            goto python_init_fail;
+         }
+      }
+#else
+      /* Python 3.10 and higher, check sysconfig first,
+       * distutils is deprecated. */
       if(PyRun_SimpleString("import sysconfig \n") < 0) {
          log_info("pythonmod: module sysconfig not available; "
             "falling back to distutils.sysconfig.");
@@ -344,6 +365,7 @@ int pythonmod_init(struct module_env* env, int id)
             goto python_init_fail;
          }
       }
+#endif
       if(PyRun_SimpleString("from unboundmodule import *\n") < 0)
       {
          goto python_init_fail;