From: W.C.A. Wijngaards Date: Mon, 9 Jan 2023 14:03:38 +0000 (+0100) Subject: - Fix python module install path detection. X-Git-Tag: release-1.19.0rc1~38^2~48 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=4517dcd439562cb1df07e77738153c0c906bb069;p=thirdparty%2Funbound.git - Fix python module install path detection. --- diff --git a/acx_python.m4 b/acx_python.m4 index 16c0c6fd9..c945d6c89 100644 --- a/acx_python.m4 +++ b/acx_python.m4 @@ -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 diff --git a/configure b/configure index 5823e49f2..0694dce86 100755 --- a/configure +++ b/configure @@ -17541,39 +17541,68 @@ fi 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 diff --git a/doc/Changelog b/doc/Changelog index d83320755..f8d5b5668 100644 --- a/doc/Changelog +++ b/doc/Changelog @@ -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. diff --git a/pythonmod/pythonmod.c b/pythonmod/pythonmod.c index 7c7da5489..28ce0eec4 100644 --- a/pythonmod/pythonmod.c +++ b/pythonmod/pythonmod.c @@ -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;