Replace use of deprecated Python module distutils.sysconfig, take 2.

With Python 3.10, configure spits out warnings about the module
distutils.sysconfig being deprecated and scheduled for removal in
Python 3.12.  Change the uses in configure to use the module sysconfig
instead.  The logic stays largely the same, although we have to
rely on INCLUDEPY instead of the deprecated get_python_inc function.

Note that sysconfig exists since Python 2.7, so this moves the
minimum required version up from Python 2.6 (or 2.4, before v13).
Also, sysconfig didn't exist in Python 3.1, so the minimum 3.x
version is now 3.2.

Back-patch of commit bd233bdd8 into all supported branches.

In v10, this also includes back-patching v11's beff4bb9c, primarily
because this opinion is clearly out-of-date:

    While at it, get rid of the code's assumption that both the major and
    minor numbers contain exactly one digit.  That will foreseeably be
    broken by Python 3.10 in perhaps four or five years.  That's far enough
    out that we probably don't need to back-patch this.

Peter Eisentraut, Tom Lane, Andres Freund

Discussion: https://postgr.es/m/c74add3c-09c4-a9dd-1a03-a846e5b2fc52@enterprisedb.com
This commit is contained in:
Tom Lane 2022-02-01 19:03:41 -05:00
parent f83d427d32
commit a72c430120
3 changed files with 52 additions and 57 deletions

View File

@ -29,35 +29,32 @@ fi
# as well as the Python version. # as well as the Python version.
AC_DEFUN([_PGAC_CHECK_PYTHON_DIRS], AC_DEFUN([_PGAC_CHECK_PYTHON_DIRS],
[AC_REQUIRE([PGAC_PATH_PYTHON]) [AC_REQUIRE([PGAC_PATH_PYTHON])
AC_MSG_CHECKING([for Python distutils module]) python_fullversion=`${PYTHON} -c "import sys; print(sys.version)" | sed q`
if "${PYTHON}" -c 'import distutils' 2>&AS_MESSAGE_LOG_FD AC_MSG_NOTICE([using python $python_fullversion])
# python_fullversion is typically n.n.n plus some trailing junk
python_majorversion=`echo "$python_fullversion" | sed '[s/^\([0-9]*\).*/\1/]'`
python_minorversion=`echo "$python_fullversion" | sed '[s/^[0-9]*\.\([0-9]*\).*/\1/]'`
python_version=`echo "$python_fullversion" | sed '[s/^\([0-9]*\.[0-9]*\).*/\1/]'`
# Reject unsupported Python versions as soon as practical.
if test "$python_majorversion" -lt 3 -a "$python_minorversion" -lt 7; then
AC_MSG_ERROR([Python version $python_version is too old (version 2.7 or later is required)])
fi
AC_MSG_CHECKING([for Python sysconfig module])
if "${PYTHON}" -c 'import sysconfig' 2>&AS_MESSAGE_LOG_FD
then then
AC_MSG_RESULT(yes) AC_MSG_RESULT(yes)
else else
AC_MSG_RESULT(no) AC_MSG_RESULT(no)
AC_MSG_ERROR([distutils module not found]) AC_MSG_ERROR([sysconfig module not found])
fi fi
AC_MSG_CHECKING([Python configuration directory]) AC_MSG_CHECKING([Python configuration directory])
python_majorversion=`${PYTHON} -c "import sys; print(sys.version[[0]])"` python_configdir=`${PYTHON} -c "import sysconfig; print(' '.join(filter(None,sysconfig.get_config_vars('LIBPL'))))"`
python_minorversion=`${PYTHON} -c "import sys; print(sys.version[[2]])"`
python_version=`${PYTHON} -c "import sys; print(sys.version[[:3]])"`
python_configdir=`${PYTHON} -c "import distutils.sysconfig; print(' '.join(filter(None,distutils.sysconfig.get_config_vars('LIBPL'))))"`
AC_MSG_RESULT([$python_configdir]) AC_MSG_RESULT([$python_configdir])
# Reject unsupported Python versions as soon as practical. AC_MSG_CHECKING([Python include directory])
if test "$python_majorversion" -lt 3 -a "$python_minorversion" -lt 4; then python_includespec=`${PYTHON} -c "import sysconfig; print('-I' + sysconfig.get_config_var('INCLUDEPY'))"`
AC_MSG_ERROR([Python version $python_version is too old (version 2.4 or later is required)])
fi
AC_MSG_CHECKING([Python include directories])
python_includespec=`${PYTHON} -c "
import distutils.sysconfig
a = '-I' + distutils.sysconfig.get_python_inc(False)
b = '-I' + distutils.sysconfig.get_python_inc(True)
if a == b:
print(a)
else:
print(a + ' ' + b)"`
if test "$PORTNAME" = win32 ; then if test "$PORTNAME" = win32 ; then
python_includespec=`echo $python_includespec | sed 's,[[\]],/,g'` python_includespec=`echo $python_includespec | sed 's,[[\]],/,g'`
fi fi
@ -91,8 +88,8 @@ AC_DEFUN([PGAC_CHECK_PYTHON_EMBED_SETUP],
[AC_REQUIRE([_PGAC_CHECK_PYTHON_DIRS]) [AC_REQUIRE([_PGAC_CHECK_PYTHON_DIRS])
AC_MSG_CHECKING([how to link an embedded Python application]) AC_MSG_CHECKING([how to link an embedded Python application])
python_libdir=`${PYTHON} -c "import distutils.sysconfig; print(' '.join(filter(None,distutils.sysconfig.get_config_vars('LIBDIR'))))"` python_libdir=`${PYTHON} -c "import sysconfig; print(' '.join(filter(None,sysconfig.get_config_vars('LIBDIR'))))"`
python_ldlibrary=`${PYTHON} -c "import distutils.sysconfig; print(' '.join(filter(None,distutils.sysconfig.get_config_vars('LDLIBRARY'))))"` python_ldlibrary=`${PYTHON} -c "import sysconfig; print(' '.join(filter(None,sysconfig.get_config_vars('LDLIBRARY'))))"`
# If LDLIBRARY exists and has a shlib extension, use it verbatim. # If LDLIBRARY exists and has a shlib extension, use it verbatim.
ldlibrary=`echo "${python_ldlibrary}" | sed -e 's/\.so$//' -e 's/\.dll$//' -e 's/\.dylib$//' -e 's/\.sl$//'` ldlibrary=`echo "${python_ldlibrary}" | sed -e 's/\.so$//' -e 's/\.dll$//' -e 's/\.dylib$//' -e 's/\.sl$//'`
@ -104,11 +101,11 @@ else
# Otherwise, guess the base name of the shlib. # Otherwise, guess the base name of the shlib.
# LDVERSION was added in Python 3.2, before that use VERSION, # LDVERSION was added in Python 3.2, before that use VERSION,
# or failing that, $python_version from _PGAC_CHECK_PYTHON_DIRS. # or failing that, $python_version from _PGAC_CHECK_PYTHON_DIRS.
python_ldversion=`${PYTHON} -c "import distutils.sysconfig; print(' '.join(filter(None,distutils.sysconfig.get_config_vars('LDVERSION'))))"` python_ldversion=`${PYTHON} -c "import sysconfig; print(' '.join(filter(None,sysconfig.get_config_vars('LDVERSION'))))"`
if test x"${python_ldversion}" != x""; then if test x"${python_ldversion}" != x""; then
ldlibrary="python${python_ldversion}" ldlibrary="python${python_ldversion}"
else else
python_version_var=`${PYTHON} -c "import distutils.sysconfig; print(' '.join(filter(None,distutils.sysconfig.get_config_vars('VERSION'))))"` python_version_var=`${PYTHON} -c "import sysconfig; print(' '.join(filter(None,sysconfig.get_config_vars('VERSION'))))"`
if test x"${python_version_var}" != x""; then if test x"${python_version_var}" != x""; then
ldlibrary="python${python_version_var}" ldlibrary="python${python_version_var}"
else else
@ -168,7 +165,7 @@ PL/Python.])
fi fi
python_libspec="-L${python_libdir} -l${ldlibrary}" python_libspec="-L${python_libdir} -l${ldlibrary}"
python_additional_libs=`${PYTHON} -c "import distutils.sysconfig; print(' '.join(filter(None,distutils.sysconfig.get_config_vars('LIBS','LIBC','LIBM','BASEMODLIBS'))))"` python_additional_libs=`${PYTHON} -c "import sysconfig; print(' '.join(filter(None,sysconfig.get_config_vars('LIBS','LIBC','LIBM','BASEMODLIBS'))))"`
AC_MSG_RESULT([${python_libspec} ${python_additional_libs}]) AC_MSG_RESULT([${python_libspec} ${python_additional_libs}])

54
configure vendored
View File

@ -8230,41 +8230,39 @@ if test x"$PYTHON" = x""; then
fi fi
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for Python distutils module" >&5 python_fullversion=`${PYTHON} -c "import sys; print(sys.version)" | sed q`
$as_echo_n "checking for Python distutils module... " >&6; } { $as_echo "$as_me:${as_lineno-$LINENO}: using python $python_fullversion" >&5
if "${PYTHON}" -c 'import distutils' 2>&5 $as_echo "$as_me: using python $python_fullversion" >&6;}
# python_fullversion is typically n.n.n plus some trailing junk
python_majorversion=`echo "$python_fullversion" | sed 's/^\([0-9]*\).*/\1/'`
python_minorversion=`echo "$python_fullversion" | sed 's/^[0-9]*\.\([0-9]*\).*/\1/'`
python_version=`echo "$python_fullversion" | sed 's/^\([0-9]*\.[0-9]*\).*/\1/'`
# Reject unsupported Python versions as soon as practical.
if test "$python_majorversion" -lt 3 -a "$python_minorversion" -lt 7; then
as_fn_error $? "Python version $python_version is too old (version 2.7 or later is required)" "$LINENO" 5
fi
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for Python sysconfig module" >&5
$as_echo_n "checking for Python sysconfig module... " >&6; }
if "${PYTHON}" -c 'import sysconfig' 2>&5
then then
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: yes" >&5 { $as_echo "$as_me:${as_lineno-$LINENO}: result: yes" >&5
$as_echo "yes" >&6; } $as_echo "yes" >&6; }
else else
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5 { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
$as_echo "no" >&6; } $as_echo "no" >&6; }
as_fn_error $? "distutils module not found" "$LINENO" 5 as_fn_error $? "sysconfig module not found" "$LINENO" 5
fi fi
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking Python configuration directory" >&5 { $as_echo "$as_me:${as_lineno-$LINENO}: checking Python configuration directory" >&5
$as_echo_n "checking Python configuration directory... " >&6; } $as_echo_n "checking Python configuration directory... " >&6; }
python_majorversion=`${PYTHON} -c "import sys; print(sys.version[0])"` python_configdir=`${PYTHON} -c "import sysconfig; print(' '.join(filter(None,sysconfig.get_config_vars('LIBPL'))))"`
python_minorversion=`${PYTHON} -c "import sys; print(sys.version[2])"`
python_version=`${PYTHON} -c "import sys; print(sys.version[:3])"`
python_configdir=`${PYTHON} -c "import distutils.sysconfig; print(' '.join(filter(None,distutils.sysconfig.get_config_vars('LIBPL'))))"`
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $python_configdir" >&5 { $as_echo "$as_me:${as_lineno-$LINENO}: result: $python_configdir" >&5
$as_echo "$python_configdir" >&6; } $as_echo "$python_configdir" >&6; }
# Reject unsupported Python versions as soon as practical. { $as_echo "$as_me:${as_lineno-$LINENO}: checking Python include directory" >&5
if test "$python_majorversion" -lt 3 -a "$python_minorversion" -lt 4; then $as_echo_n "checking Python include directory... " >&6; }
as_fn_error $? "Python version $python_version is too old (version 2.4 or later is required)" "$LINENO" 5 python_includespec=`${PYTHON} -c "import sysconfig; print('-I' + sysconfig.get_config_var('INCLUDEPY'))"`
fi
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking Python include directories" >&5
$as_echo_n "checking Python include directories... " >&6; }
python_includespec=`${PYTHON} -c "
import distutils.sysconfig
a = '-I' + distutils.sysconfig.get_python_inc(False)
b = '-I' + distutils.sysconfig.get_python_inc(True)
if a == b:
print(a)
else:
print(a + ' ' + b)"`
if test "$PORTNAME" = win32 ; then if test "$PORTNAME" = win32 ; then
python_includespec=`echo $python_includespec | sed 's,[\],/,g'` python_includespec=`echo $python_includespec | sed 's,[\],/,g'`
fi fi
@ -8276,8 +8274,8 @@ $as_echo "$python_includespec" >&6; }
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking how to link an embedded Python application" >&5 { $as_echo "$as_me:${as_lineno-$LINENO}: checking how to link an embedded Python application" >&5
$as_echo_n "checking how to link an embedded Python application... " >&6; } $as_echo_n "checking how to link an embedded Python application... " >&6; }
python_libdir=`${PYTHON} -c "import distutils.sysconfig; print(' '.join(filter(None,distutils.sysconfig.get_config_vars('LIBDIR'))))"` python_libdir=`${PYTHON} -c "import sysconfig; print(' '.join(filter(None,sysconfig.get_config_vars('LIBDIR'))))"`
python_ldlibrary=`${PYTHON} -c "import distutils.sysconfig; print(' '.join(filter(None,distutils.sysconfig.get_config_vars('LDLIBRARY'))))"` python_ldlibrary=`${PYTHON} -c "import sysconfig; print(' '.join(filter(None,sysconfig.get_config_vars('LDLIBRARY'))))"`
# If LDLIBRARY exists and has a shlib extension, use it verbatim. # If LDLIBRARY exists and has a shlib extension, use it verbatim.
ldlibrary=`echo "${python_ldlibrary}" | sed -e 's/\.so$//' -e 's/\.dll$//' -e 's/\.dylib$//' -e 's/\.sl$//'` ldlibrary=`echo "${python_ldlibrary}" | sed -e 's/\.so$//' -e 's/\.dll$//' -e 's/\.dylib$//' -e 's/\.sl$//'`
@ -8289,11 +8287,11 @@ else
# Otherwise, guess the base name of the shlib. # Otherwise, guess the base name of the shlib.
# LDVERSION was added in Python 3.2, before that use VERSION, # LDVERSION was added in Python 3.2, before that use VERSION,
# or failing that, $python_version from _PGAC_CHECK_PYTHON_DIRS. # or failing that, $python_version from _PGAC_CHECK_PYTHON_DIRS.
python_ldversion=`${PYTHON} -c "import distutils.sysconfig; print(' '.join(filter(None,distutils.sysconfig.get_config_vars('LDVERSION'))))"` python_ldversion=`${PYTHON} -c "import sysconfig; print(' '.join(filter(None,sysconfig.get_config_vars('LDVERSION'))))"`
if test x"${python_ldversion}" != x""; then if test x"${python_ldversion}" != x""; then
ldlibrary="python${python_ldversion}" ldlibrary="python${python_ldversion}"
else else
python_version_var=`${PYTHON} -c "import distutils.sysconfig; print(' '.join(filter(None,distutils.sysconfig.get_config_vars('VERSION'))))"` python_version_var=`${PYTHON} -c "import sysconfig; print(' '.join(filter(None,sysconfig.get_config_vars('VERSION'))))"`
if test x"${python_version_var}" != x""; then if test x"${python_version_var}" != x""; then
ldlibrary="python${python_version_var}" ldlibrary="python${python_version_var}"
else else
@ -8353,7 +8351,7 @@ PL/Python." "$LINENO" 5
fi fi
python_libspec="-L${python_libdir} -l${ldlibrary}" python_libspec="-L${python_libdir} -l${ldlibrary}"
python_additional_libs=`${PYTHON} -c "import distutils.sysconfig; print(' '.join(filter(None,distutils.sysconfig.get_config_vars('LIBS','LIBC','LIBM','BASEMODLIBS'))))"` python_additional_libs=`${PYTHON} -c "import sysconfig; print(' '.join(filter(None,sysconfig.get_config_vars('LIBS','LIBC','LIBM','BASEMODLIBS'))))"`
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: ${python_libspec} ${python_additional_libs}" >&5 { $as_echo "$as_me:${as_lineno-$LINENO}: result: ${python_libspec} ${python_additional_libs}" >&5
$as_echo "${python_libspec} ${python_additional_libs}" >&6; } $as_echo "${python_libspec} ${python_additional_libs}" >&6; }

View File

@ -193,10 +193,10 @@ su - postgres
To build the <application>PL/Python</> server programming To build the <application>PL/Python</> server programming
language, you need a <productname>Python</productname> language, you need a <productname>Python</productname>
installation with the header files and installation with the header files and
the <application>distutils</application> module. The minimum the <application>sysconfig</application> module. The minimum
required version is <productname>Python</productname> 2.4. required version is <productname>Python</productname> 2.7.
<productname>Python 3</productname> is supported if it's <productname>Python 3</productname> is supported if it's
version 3.1 or later; but see version 3.2 or later; but see
<![%standalone-include[the <application>PL/Python</> documentation]]> <![%standalone-include[the <application>PL/Python</> documentation]]>
<![%standalone-ignore[<xref linkend="plpython-python23">]]> <![%standalone-ignore[<xref linkend="plpython-python23">]]>
when using Python 3. when using Python 3.