From d391b49a7fbfd2dce793114f9aa6ba1963e5fd86 Mon Sep 17 00:00:00 2001
From: Andres Freund <andres@anarazel.de>
Date: Tue, 15 Feb 2022 12:30:20 -0800
Subject: [PATCH v5 1/4] plpython: Reject Python 2 during build configuration.

Python 2.7 went EOL 2020-01-01 and the support for Python 2 requires a fair
bit of infrastructure. Therefore we are removing Python 2 support in plpython.

This patch just rejects Python 2 during configure / mkvcbuild.pl. Future
commits will remove the code and infrastructure for Python 2 support and
adjust more of the documentation. This way we can see the buildfarm state
after the removal sooner and we can be sure that failures are due to
desupporting Python 2, rather than caused by infrastructure cleanup.

Discussion: https://postgr.es/m/20211031184548.g4sxfe47n2kyi55r@alap3.anarazel.de
---
 config/python.m4               |  9 ++++++---
 doc/src/sgml/installation.sgml | 18 +++++-------------
 configure                      |  6 +++---
 src/tools/msvc/Mkvcbuild.pm    |  4 ++++
 4 files changed, 18 insertions(+), 19 deletions(-)

diff --git a/config/python.m4 b/config/python.m4
index c7310ee37d3..52f34070dd8 100644
--- a/config/python.m4
+++ b/config/python.m4
@@ -9,6 +9,9 @@
 # Look for Python and set the output variable 'PYTHON' if found,
 # fail otherwise.
 #
+# Since we are supporting only Python 3.x, prefer python3 to plain python.  If
+# the latter exists at all, it very possibly points to python2.
+
 # As the Python 3 transition happens and PEP 394 isn't updated, we
 # need to cater to systems that don't have unversioned "python" by
 # default.  Some systems ship with "python3" by default and perhaps
@@ -16,7 +19,7 @@
 # "python2" and "python3", in which case it's reasonable to prefer the
 # newer version.
 AC_DEFUN([PGAC_PATH_PYTHON],
-[PGAC_PATH_PROGS(PYTHON, [python python3 python2])
+[PGAC_PATH_PROGS(PYTHON, [python3 python])
 AC_ARG_VAR(PYTHON, [Python program])dnl
 if test x"$PYTHON" = x""; then
   AC_MSG_ERROR([Python not found])
@@ -37,8 +40,8 @@ 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)])
+if test "$python_majorversion" -lt 3; then
+  AC_MSG_ERROR([Python version $python_version is too old (version 3 or later is required)])
 fi
 
 AC_MSG_CHECKING([for Python sysconfig module])
diff --git a/doc/src/sgml/installation.sgml b/doc/src/sgml/installation.sgml
index 655095f3b13..848d7caa992 100644
--- a/doc/src/sgml/installation.sgml
+++ b/doc/src/sgml/installation.sgml
@@ -196,11 +196,7 @@ su - postgres
       language, you need a <productname>Python</productname>
       installation with the header files and
       the <application>sysconfig</application> module.  The minimum
-      required version is <productname>Python</productname> 2.7.
-      <productname>Python 3</productname> is supported if it's
-      version 3.2 or later; but see
-      <xref linkend="plpython-python23"/>
-      when using Python 3.
+      required version is <productname>Python</productname> 3.2 or later.
      </para>
 
      <para>
@@ -1868,14 +1864,10 @@ build-postgresql:
        <term><envar>PYTHON</envar></term>
        <listitem>
         <para>
-         Python interpreter program.  This will be used to
-         determine the dependencies for building PL/Python.  Also,
-         whether Python 2 or 3 is specified here (or otherwise
-         implicitly chosen) determines which variant of the PL/Python
-         language becomes available.  See
-         <xref linkend="plpython-python23"/>
-         for more information.  If this is not set, the following are probed
-         in this order: <literal>python python3 python2</literal>.
+         Python interpreter program.  This will be used to determine the
+         dependencies for building PL/Python.  If this is not set, the
+         following are probed in this order:
+         <literal>python3 python</literal>.
         </para>
        </listitem>
       </varlistentry>
diff --git a/configure b/configure
index 93055556585..ba635a00622 100755
--- a/configure
+++ b/configure
@@ -10280,7 +10280,7 @@ fi
 
 if test "$with_python" = yes; then
   if test -z "$PYTHON"; then
-  for ac_prog in python python3 python2
+  for ac_prog in python3 python
 do
   # Extract the first word of "$ac_prog", so it can be a program name with args.
 set dummy $ac_prog; ac_word=$2
@@ -10346,8 +10346,8 @@ 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
+if test "$python_majorversion" -lt 3; then
+  as_fn_error $? "Python version $python_version is too old (version 3 or later is required)" "$LINENO" 5
 fi
 
 { $as_echo "$as_me:${as_lineno-$LINENO}: checking for Python sysconfig module" >&5
diff --git a/src/tools/msvc/Mkvcbuild.pm b/src/tools/msvc/Mkvcbuild.pm
index bab81bd459a..105f5c72a2d 100644
--- a/src/tools/msvc/Mkvcbuild.pm
+++ b/src/tools/msvc/Mkvcbuild.pm
@@ -490,6 +490,10 @@ sub mkvcbuild
 		  if (!(defined($pyprefix) && defined($pyver)));
 
 		my $pymajorver = substr($pyver, 0, 1);
+
+		die "Python version $pyver is too old (version 3 or later is required)"
+		  if int($pymajorver) < 3;
+
 		my $plpython = $solution->AddProject('plpython' . $pymajorver,
 			'dll', 'PLs', 'src/pl/plpython');
 		$plpython->AddIncludeDir($pyprefix . '/include');
-- 
2.34.0

