diff options
author | Karl Berry <karl@freefriends.org> | 2020-02-25 18:07:15 -0800 |
---|---|---|
committer | Karl Berry <karl@freefriends.org> | 2020-02-25 18:07:15 -0800 |
commit | 9ba2f0ddbcf08d4d67baffa8779abff7042a1c96 (patch) | |
tree | 6550bfe687eb9fd4f13258a1991f9c171ae27dc4 /lib | |
parent | 9c65590a164099dbb483dbcd339347f45b827b29 (diff) | |
download | automake-9ba2f0ddbcf08d4d67baffa8779abff7042a1c96.tar.gz |
python: support both Python 2 and 3 in py-compile
* lib/py-compile: check python major version and use imp
or importlib accordingly, plus related changes. Original
patch for Python 3 only from Gabriel Ganne at:
https://lists.gnu.org/archive/html/automake-patches/2019-07/msg00002.html
Diffstat (limited to 'lib')
-rwxr-xr-x | lib/py-compile | 33 |
1 files changed, 26 insertions, 7 deletions
diff --git a/lib/py-compile b/lib/py-compile index f2be7d084..e56d98d6e 100755 --- a/lib/py-compile +++ b/lib/py-compile @@ -1,7 +1,7 @@ #!/bin/sh # py-compile - Compile a Python program -scriptversion=2018-03-07.03; # UTC +scriptversion=2020-02-19.23; # UTC # Copyright (C) 2000-2020 Free Software Foundation, Inc. @@ -115,8 +115,27 @@ else filetrans="filepath = os.path.normpath('$destdir' + os.sep + path)" fi +python_major=$($PYTHON -V 2>&1 | sed -e 's/.* //;s/\..*$//;1q') +if test -z "$python_major"; then + echo "$me: could not determine $PYTHON major version, guessing 3" >&2 + python_major=3 +fi + +# The old way to import libraries was deprecated. +if test "$python_major" -le 2; then + import_lib=imp + import_test="hasattr(imp, 'get_tag')" + import_call=imp.cache_from_source + import_arg2=', False' # needed in one call and not the other +else + import_lib=importlib + import_test="hasattr(sys.implementation, 'cache_tag')" + import_call=importlib.util.cache_from_source + import_arg2= +fi + $PYTHON -c " -import sys, os, py_compile, imp +import sys, os, py_compile, $import_lib files = '''$files''' @@ -129,15 +148,15 @@ for file in files.split(): continue sys.stdout.write(file) sys.stdout.flush() - if hasattr(imp, 'get_tag'): - py_compile.compile(filepath, imp.cache_from_source(filepath), path) + if $import_test: + py_compile.compile(filepath, $import_call(filepath), path) else: py_compile.compile(filepath, filepath + 'c', path) sys.stdout.write('\n')" || exit $? # this will fail for python < 1.5, but that doesn't matter ... $PYTHON -O -c " -import sys, os, py_compile, imp +import sys, os, py_compile, $import_lib # pypy does not use .pyo optimization if hasattr(sys, 'pypy_translation_info'): @@ -153,8 +172,8 @@ for file in files.split(): continue sys.stdout.write(file) sys.stdout.flush() - if hasattr(imp, 'get_tag'): - py_compile.compile(filepath, imp.cache_from_source(filepath, False), path) + if $import_test: + py_compile.compile(filepath, $import_call(filepath$import_arg2), path) else: py_compile.compile(filepath, filepath + 'o', path) sys.stdout.write('\n')" 2>/dev/null || : |