From 403d17a876d6e57e3f8c6551931aca030d0d9b96 Mon Sep 17 00:00:00 2001 From: "Richard W.M. Jones" Date: Mon, 6 Jul 2020 12:02:01 +0100 Subject: [PATCH] python: Drop support for Python 2. Python 2 reached end of life on 2020-01-01: https://python3statement.org/ https://pythonclock.org/ The minimum version required is now Python 3.4 (since that is the version in Debian oldoldstable), but 3.6 is the minimum version that I actually test. --- docs/guestfs-building.pod | 29 ++++------------------------- generator/python.ml | 25 ------------------------- m4/guestfs-python.m4 | 27 ++++++++++++++++++++------- python/handle.c | 12 ------------ 4 files changed, 24 insertions(+), 69 deletions(-) diff --git a/docs/guestfs-building.pod b/docs/guestfs-building.pod index b8cadabf1..74e862f95 100644 --- a/docs/guestfs-building.pod +++ b/docs/guestfs-building.pod @@ -307,11 +307,10 @@ Optional. For testing the common OCaml modules. Optional. Used to build and test the Perl bindings. -=item Python E 2.7 +=item Python E 3.4 -Optional. Used to build the Python bindings. For building -Python 2 or Python 3 bindings, see -L below. +Optional. Used to build the Python bindings. +S support was removed in S. =item Python C @@ -528,8 +527,6 @@ If there is a file in the top build directory called F, then it will be sourced by C. This file can contain any local environment variables needed, eg. for skipping tests: - # Use an alternate python binary. - export PYTHON=python3 # Skip this test, it is broken. export SKIP_TEST_BTRFS_FSCK=1 @@ -683,8 +680,7 @@ F to understand precisely what this does. This environment variable may be set to point to a python binary (eg. C). When C<./configure> runs, it inspects this python binary to find the version of Python, the location of Python libraries -and so on. See -L below. +and so on. =item B @@ -752,23 +748,6 @@ tarball, eg: and run the libguestfs programs and virt tools in the normal way, eg. using the F<./run> script (see above). -=head1 BUILDING PYTHON 2 AND PYTHON 3 BINDINGS - -The F<./configure> script detects the currently installed version of -Python using whatever program is called C in the current -C<$PATH>. Libguestfs will build Python 2 or Python 3 bindings as -appropriate. - -You can override this behaviour by specifying an alternate Python -binary, eg: - - PYTHON=/usr/bin/python3 ./configure - -To build parallel Python 2 and Python 3 bindings, you will need to -build libguestfs twice. The second time, you can disable all the -other bindings and tools and just build the Python bindings. See the -Fedora spec file (see below) for a complete example of how to do this. - =head1 BUILDING A PACKAGE FOR FEDORA The Fedora spec file is stored under: diff --git a/generator/python.ml b/generator/python.ml index f0d6b5d96..d0f34c32b 100644 --- a/generator/python.ml +++ b/generator/python.ml @@ -174,13 +174,8 @@ and generate_python_structs () = pr " goto err;\n"; pr " PyDict_SetItemString (dict, \"%s\", value);\n" name; | name, FBuffer -> - pr "#if PY_MAJOR_VERSION >= 3\n"; pr " value = PyBytes_FromStringAndSize (%s->%s, %s->%s_len);\n" typ name typ name; - pr "#else\n"; - pr " value = PyString_FromStringAndSize (%s->%s, %s->%s_len);\n" - typ name typ name; - pr "#endif\n"; pr " if (value == NULL)\n"; pr " goto err;\n"; pr " PyDict_SetItemString (dict, \"%s\", value);\n" name; @@ -493,11 +488,7 @@ and generate_python_actions actions () = pr " guestfs_int_free_string_list (r);\n"; pr " if (py_r == NULL) goto out;\n"; | RBufferOut _ -> - pr "#if PY_MAJOR_VERSION >= 3\n"; pr " py_r = PyBytes_FromStringAndSize (r, size);\n"; - pr "#else\n"; - pr " py_r = PyString_FromStringAndSize (r, size);\n"; - pr "#endif\n"; pr " free (r);\n"; pr " if (py_r == NULL) goto out;\n"; ); @@ -575,7 +566,6 @@ and generate_python_module () = (* Init function. *) pr "\ -#if PY_MAJOR_VERSION >= 3 static struct PyModuleDef moduledef = { PyModuleDef_HEAD_INIT, \"libguestfsmod\", /* m_name */ @@ -587,18 +577,13 @@ static struct PyModuleDef moduledef = { NULL, /* m_clear */ NULL, /* m_free */ }; -#endif static PyObject * moduleinit (void) { PyObject *m; -#if PY_MAJOR_VERSION >= 3 m = PyModule_Create (&moduledef); -#else - m = Py_InitModule ((char *) \"libguestfsmod\", methods); -#endif if (m != NULL) guestfs_int_py_extend_module (m); @@ -606,7 +591,6 @@ moduleinit (void) return m; /* m might be NULL if module init failed */ } -#if PY_MAJOR_VERSION >= 3 extern PyMODINIT_FUNC PyInit_libguestfsmod (void); PyMODINIT_FUNC @@ -614,15 +598,6 @@ PyInit_libguestfsmod (void) { return moduleinit (); } -#else -extern void initlibguestfsmod (void); - -void -initlibguestfsmod (void) -{ - (void) moduleinit (); -} -#endif " (* Generate Python module. *) diff --git a/m4/guestfs-python.m4 b/m4/guestfs-python.m4 index 3210c49bd..f562a0540 100644 --- a/m4/guestfs-python.m4 +++ b/m4/guestfs-python.m4 @@ -19,27 +19,40 @@ dnl Check for Python (optional, for Python bindings). PYTHON_PREFIX= PYTHON_VERSION= PYTHON_INSTALLDIR= -PYTHON_REQ_MAJOR=2 -PYTHON_REQ_MINOR=7 AC_ARG_ENABLE([python], AS_HELP_STRING([--disable-python], [disable Python language bindings]), [], [enable_python=yes]) AS_IF([test "x$enable_python" != "xno"],[ - AC_CHECK_PROG([PYTHON],[python],[python],[no]) + AC_CHECK_PROGS([PYTHON],[python3 python],[no]) if test "x$PYTHON" != "xno"; then AC_MSG_CHECKING([Python version]) PYTHON_VERSION_MAJOR=`$PYTHON -c "import sys; print (sys.version_info@<:@0@:>@)"` PYTHON_VERSION_MINOR=`$PYTHON -c "import sys; print (sys.version_info@<:@1@:>@)"` PYTHON_VERSION="$PYTHON_VERSION_MAJOR.$PYTHON_VERSION_MINOR" - AS_IF([test "$PYTHON_VERSION_MAJOR" -lt $PYTHON_REQ_MAJOR || ( test "$PYTHON_VERSION_MAJOR" -eq $PYTHON_REQ_MAJOR && test "$PYTHON_VERSION_MINOR" -lt $PYTHON_REQ_MINOR )],[ - AC_MSG_ERROR([found Python $PYTHON_VERSION, while Python $PYTHON_REQ_MAJOR.$PYTHON_REQ_MINOR is required]) + AS_IF([test -n "$PYTHON_VERSION"],[ + AC_MSG_RESULT([$PYTHON_VERSION]) + ],[ + AC_MSG_NOTICE([Python bindings disabled]) + enable_python=no ]) - AC_MSG_RESULT([$PYTHON_VERSION]) - # Debian: python-2.7.pc, python-3.2.pc + AC_MSG_CHECKING([Python major version is 3]) + AS_IF([test "x$PYTHON_VERSION_MAJOR" = "x3"],[ + AC_MSG_RESULT([yes]) + ],[ + AC_MSG_RESULT([no]) + AC_MSG_ERROR([Python $PYTHON_VERSION_MAJOR <> 3 is no longer supported. + +Python 2 end of life is 2020-01-01 and libguestfs >= 1.42.1 no longer +supports it. + +If you want to use Python 2, you will need to use libguestfs 1.40 or 1.42.0.]) + ]) + + # Debian: python-3.2.pc PKG_CHECK_MODULES([PYTHON], [python-"$PYTHON_VERSION"],[ have_python_module=1 AC_SUBST([PYTHON_CFLAGS]) diff --git a/python/handle.c b/python/handle.c index 2fb8c18f0..bc5890446 100644 --- a/python/handle.c +++ b/python/handle.c @@ -384,30 +384,18 @@ guestfs_int_py_put_table (char * const * const argv) PyObject * guestfs_int_py_fromstring (const char *str) { -#if PY_MAJOR_VERSION < 3 - return PyString_FromString (str); -#else return PyUnicode_FromString (str); -#endif } PyObject * guestfs_int_py_fromstringsize (const char *str, size_t size) { -#if PY_MAJOR_VERSION < 3 - return PyString_FromStringAndSize (str, size); -#else return PyUnicode_FromStringAndSize (str, size); -#endif } char * guestfs_int_py_asstring (PyObject *obj) { -#if PY_MAJOR_VERSION < 3 - return PyString_AsString (obj); -#else PyObject *bytes = PyUnicode_AsUTF8String (obj); return PyBytes_AS_STRING (bytes); -#endif }