From c8aa76fef022fcf11973f30f8557c65ae0bf48d0 Mon Sep 17 00:00:00 2001 From: Olly Betts Date: Mon, 27 Jun 2022 11:14:33 +1200 Subject: [PATCH] [python3] Improve configure probe portability The distutils and imp modules have both been deprecated and are slated for removal in 3.12. (cherry picked from commit 842d4971b3de5231903e780f40477768eb7427fc) --- xapian-bindings/configure.ac | 39 ++++++++++++++++++++----------------- xapian-bindings/python3/Makefile.am | 16 +++++++-------- 2 files changed, 29 insertions(+), 26 deletions(-) diff --git a/xapian-bindings/configure.ac b/xapian-bindings/configure.ac index cb266a84b..abe7bb4ae 100644 --- a/xapian-bindings/configure.ac +++ b/xapian-bindings/configure.ac @@ -431,17 +431,8 @@ if test no != "$with_python3" ; then ;; esac if test -n "$PYTHON3" ; then - if $PYTHON3 -c 'import distutils.sysconfig' 2>/dev/null ; then - PYTHON3_INC=`$PYTHON3 -c 'import os,distutils.sysconfig;print(distutils.sysconfig.get_python_inc().replace(os.sep,"/"))'` - AC_SUBST(PYTHON3_INC) - else - if test yes = "$with_python3" ; then - AC_MSG_ERROR([Couldn't import Python module distutils.sysconfig - you probably need to install a python3-dev or python3-devel package]) - else - AC_MSG_WARN([Couldn't import Python module distutils.sysconfig - you probably don't have a python3-dev or python3-devel package installed]) - PYTHON3= - fi - fi + PYTHON3_INC=`$PYTHON3 -c 'import os,sysconfig;print(sysconfig.get_path("include").replace(os.sep,"/"))' 2>/dev/null` + AC_SUBST(PYTHON3_INC) fi AC_MSG_CHECKING([for python3 sphinx module]) if $PYTHON3 -c 'import sphinx' >&5 2>&5 ; then @@ -461,7 +452,7 @@ if test no != "$with_python3" ; then AC_MSG_RESULT(yes) AC_MSG_CHECKING([for directory to install python3 bindings in]) if test -z "$PYTHON3_LIB" ; then - PYTHON3_LIB=`$PYTHON3 -c 'import os,distutils.sysconfig;print(distutils.sysconfig.get_python_lib(1).replace(os.sep,"/"))'` + PYTHON3_LIB=`$PYTHON3 -c 'import os,sysconfig;print(sysconfig.get_path("platlib").replace(os.sep,"/"))'` fi AC_MSG_RESULT([$PYTHON3_LIB]) AC_ARG_VAR(PYTHON3_LIB, [Directory to install python3 bindings in]) @@ -479,18 +470,30 @@ if test no != "$with_python3" ; then AC_MSG_RESULT([$PYTHON3_LIBS]) AC_MSG_CHECKING([for python3 module extension]) - dnl Usually ".so", but for example, Mac OS X uses ".dylib". - PYTHON3_SO=`$PYTHON3 -c 'import distutils.sysconfig;print(distutils.sysconfig.get_config_vars("SO")[[0]])'` - AC_SUBST(PYTHON3_SO) - AC_MSG_RESULT([$PYTHON3_SO]) + dnl Encodes the Python version the module is built for, and ends with + dnl the platform-dependent shared object extension (which is `.so` on + dnl most Unix-like platforms) - e.g. `.cpython-310-x86_64-linux-gnu.so` + PYTHON3_EXT_SUFFIX=`$PYTHON3 -c 'import sysconfig;print(sysconfig.get_config_var("EXT_SUFFIX"))'` + AC_SUBST(PYTHON3_EXT_SUFFIX) + AC_MSG_RESULT([$PYTHON3_EXT_SUFFIX]) AC_MSG_CHECKING([for tag for cached compiled scripts]) - PYTHON3_CACHE_TAG=`$PYTHON3 -c 'import imp;print(imp.get_tag())'` + dnl Since Python 3.3 sys.implementation.cache_tag is the preferred option here, + dnl and the old approach issues deprecation warnings since Python 3.7. + dnl + dnl This needs a shell-level || because Python one line program support is poor. + PYTHON3_CACHE_TAG=`$PYTHON3 -c 'import sys;print(sys.implementation.cache_tag)' 2>/dev/null || $PYTHON3 -c 'import imp;print(imp.get_tag())'` AC_SUBST(PYTHON3_CACHE_TAG) AC_MSG_RESULT([$PYTHON3_CACHE_TAG]) AC_MSG_CHECKING([for extension of cached and optimized python3 bytecode]) - PYTHON3_CACHE_OPT1_EXT=`$PYTHON3 -c 'import imp,sys;print("%s.pyo"%imp.get_tag()) if sys.version_info.minor<5 else print("{1}{2}".format(*imp.util.cache_from_source("",optimization=1).rpartition(imp.get_tag())))'` + dnl We use -O instead of cache_from_source()'s optimization parameter + dnl as the latter was only added in Python 3.5. The fallback is needed + dnl as cache_from_source() itself is new in Python 3.4. + dnl + dnl This needs a shell-level || because Python one line program support + dnl is poor. + PYTHON3_CACHE_OPT1_EXT=`$PYTHON3 -O -c 'import importlib,sys;print("{1}{2}".format(*importlib.util.cache_from_source("").rpartition(sys.implementation.cache_tag)))' 2>/dev/null || $PYTHON3 -c 'import imp;print(imp.get_tag()+".pyo")'` AC_SUBST(PYTHON3_CACHE_OPT1_EXT) AC_MSG_RESULT([$PYTHON3_CACHE_OPT1_EXT]) diff --git a/xapian-bindings/python3/Makefile.am b/xapian-bindings/python3/Makefile.am index 33c61dffb..fbf98ec28 100644 --- a/xapian-bindings/python3/Makefile.am +++ b/xapian-bindings/python3/Makefile.am @@ -55,11 +55,11 @@ install-data-hook: # Because we don't install the .la file, "make uninstall" doesn't work and # we need to remove the file ourselves. uninstall-local: - rm -f $(DESTDIR)$(pkgpylibdir)/_xapian$(PYTHON3_SO) + rm -f $(DESTDIR)$(pkgpylibdir)/_xapian$(PYTHON3_EXT_SUFFIX) AM_CPPFLAGS = -I$(PYTHON3_INC) AM_CXXFLAGS = $(SWIG_CXXFLAGS) $(XAPIAN_CXXFLAGS) -_xapian_la_LDFLAGS = -avoid-version -module -shrext "$(PYTHON3_SO)" $(NO_UNDEFINED) +_xapian_la_LDFLAGS = -avoid-version -module -shrext "$(PYTHON3_EXT_SUFFIX)" $(NO_UNDEFINED) _xapian_la_SOURCES = xapian_wrap.cc _xapian_la_LIBADD = $(XAPIAN_LIBS) $(PYTHON3_LIBS) @@ -78,23 +78,23 @@ xapian/__init__.py: xapian.py # are once installed - in particular this will fail if the SWIG shadow wrapper # doesn't cope with being xapian/__init__.py (currently it needs fixing # up, which is done by fixup-swig-py3-wrapper). -xapian/__pycache__/__init__.@PYTHON3_CACHE_TAG@.pyc: xapian/__init__.py xapian/_xapian$(PYTHON3_SO) +xapian/__pycache__/__init__.@PYTHON3_CACHE_TAG@.pyc: xapian/__init__.py xapian/_xapian$(PYTHON3_EXT_SUFFIX) $(OSX_SIP_HACK_ENV) $(PYTHON3) -c 'import os;os.chdir("xapian");import _xapian' $(OSX_SIP_HACK_ENV) $(PYTHON3) -c 'import xapian' -xapian/__pycache__/__init__.@PYTHON3_CACHE_OPT1_EXT@: xapian/__init__.py xapian/_xapian$(PYTHON3_SO) +xapian/__pycache__/__init__.@PYTHON3_CACHE_OPT1_EXT@: xapian/__init__.py xapian/_xapian$(PYTHON3_EXT_SUFFIX) $(OSX_SIP_HACK_ENV) $(PYTHON3) -O -c 'import os;os.chdir("xapian");import _xapian' $(OSX_SIP_HACK_ENV) $(PYTHON3) -O -c 'import xapian' -xapian/_xapian$(PYTHON3_SO): _xapian.la +xapian/_xapian$(PYTHON3_EXT_SUFFIX): _xapian.la $(MKDIR_P) xapian $(LIBTOOL) --config > libtoolconfig.tmp ## ksh requires a path on the sourced file. - . ./libtoolconfig.tmp; cp $$objdir/_xapian$(PYTHON3_SO) xapian + . ./libtoolconfig.tmp; cp $$objdir/_xapian$(PYTHON3_EXT_SUFFIX) xapian rm -f libtoolconfig.tmp CLEANFILES = \ - xapian/_xapian$(PYTHON3_SO) \ + xapian/_xapian$(PYTHON3_EXT_SUFFIX) \ xapian/__init__.py \ xapian/__pycache__/__init__.@PYTHON3_CACHE_TAG@.pyc \ xapian/__pycache__/__init__.@PYTHON3_CACHE_OPT1_EXT@ \ @@ -164,7 +164,7 @@ install-data-local: all-local: $(sphinxdocs) -$(sphinxdocs): xapian/__init__.py xapian/_xapian$(PYTHON3_SO) docs/conf.py $(RST_DOCS) $(dist_exampledata_DATA) +$(sphinxdocs): xapian/__init__.py xapian/_xapian$(PYTHON3_EXT_SUFFIX) docs/conf.py $(RST_DOCS) $(dist_exampledata_DATA) PYTHONPATH=..:$$PYTHONPATH $(OSX_SIP_HACK_ENV) $(PYTHON3) \ $(srcdir)/sphinx-build \ -b html -d doctrees -c docs $(srcdir)/docs docs/html -- 2.11.4.GIT