summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZack Weinberg <zackw@panix.com>2022-09-28 10:12:24 -0700
committerKarl Berry <karl@freefriends.org>2022-09-28 10:12:24 -0700
commit13fb472cd02d86bf33c092609ac00506465d2df1 (patch)
treeac70171d70022002b559486bb6354e0ef73e809f
parent8a4ecd4f2f1ffa2ef12b6946cf3aea989ff70cfd (diff)
downloadautomake-13fb472cd02d86bf33c092609ac00506465d2df1.tar.gz
python: better Python compilation portability.
This change is per automake thread: https://lists.gnu.org/archive/html/automake/2022-09/msg00002.html * lib/py-compile: Test directly for availability of importlib.util.cache_from_source. Untangle logic for when to generate -O and -OO bytecode. Reformat embedded Python fragments.
-rwxr-xr-xlib/py-compile99
1 files changed, 47 insertions, 52 deletions
diff --git a/lib/py-compile b/lib/py-compile
index 24a8a1a54..de0f02e32 100755
--- a/lib/py-compile
+++ b/lib/py-compile
@@ -1,7 +1,7 @@
#!/bin/sh
# py-compile - Compile a Python program
-scriptversion=2022-02-06.07; # UTC
+scriptversion=2022-09-26.22; # UTC
# Copyright (C) 2000-2022 Free Software Foundation, Inc.
@@ -141,88 +141,83 @@ python_minor=`$PYTHON -c 'import sys; print(sys.version_info[1])'`
$PYTHON -c "
import sys, os, py_compile, importlib
+# importlib.util.cache_from_source was added in 3.4
+if (
+ hasattr(importlib, 'util')
+ and hasattr(importlib.util, 'cache_from_source')
+):
+ destpath = importlib.util.cache_from_source
+else:
+ destpath = lambda filepath: filepath + 'c'
+
sys.stdout.write('Byte-compiling python modules...\n')
for file in sys.argv[1:]:
$pathtrans
$filetrans
- if not os.path.exists(filepath) or not (len(filepath) >= 3
- and filepath[-3:] == '.py'):
- continue
+ if (
+ not os.path.exists(filepath)
+ or not (len(filepath) >= 3 and filepath[-3:] == '.py')
+ ):
+ continue
sys.stdout.write(file + ' ')
sys.stdout.flush()
- if hasattr(sys.implementation, 'cache_tag'):
- py_compile.compile(filepath, importlib.util.cache_from_source(filepath), path)
- else:
- py_compile.compile(filepath, filepath + 'c', path)
+ py_compile.compile(filepath, destpath(filepath), path)
sys.stdout.write('\n')" "$@" || exit $?
# Then byte compile w/optimization all the modules.
-case $python_major.$python_minor in
-2.*)
- $PYTHON -O -c "
+$PYTHON -O -c "
import sys, os, py_compile, importlib
-# pypy does not use .pyo optimization
-if hasattr(sys, 'pypy_translation_info'):
+# importlib.util.cache_from_source was added in 3.4
+if (
+ hasattr(importlib, 'util')
+ and hasattr(importlib.util, 'cache_from_source')
+):
+ destpath = importlib.util.cache_from_source
+else:
+ destpath = lambda filepath: filepath + 'o'
+
+# pypy2 does not use .pyo optimization
+if sys.version_info.major <= 2 and hasattr(sys, 'pypy_translation_info'):
sys.exit(0)
sys.stdout.write('Byte-compiling python modules (optimized versions) ...\n')
for file in sys.argv[1:]:
$pathtrans
$filetrans
- if not os.path.exists(filepath) or not (len(filepath) >= 3
- and filepath[-3:] == '.py'):
- continue
+ if (
+ not os.path.exists(filepath)
+ or not (len(filepath) >= 3 and filepath[-3:] == '.py')
+ ):
+ continue
sys.stdout.write(file + ' ')
sys.stdout.flush()
- if hasattr(sys.implementation, 'cache_tag'):
- py_compile.compile(filepath, importlib.util.cache_from_source(filepath), path)
- else:
- py_compile.compile(filepath, filepath + 'o', path)
+ py_compile.compile(filepath, destpath(filepath), path)
sys.stdout.write('\n')" "$@" 2>/dev/null || exit $?
- ;;
-*) # Python 3+
- $PYTHON -O -c "
-import sys, os, py_compile, importlib
-
-print('Byte-compiling python modules (optimized versions) ...')
-for file in sys.argv[1:]:
- $pathtrans
- $filetrans
- if not os.path.exists(filepath) or not (len(filepath) >= 3
- and filepath[-3:] == '.py'):
- continue
- print(file, end=' ', flush=True)
- if hasattr(sys.implementation, 'cache_tag'):
- py_compile.compile(filepath, importlib.util.cache_from_source(filepath), path)
- else:
- py_compile.compile(filepath, filepath + 'o', path)
-print()" "$@" 2>/dev/null || exit $?
- ;;
-esac
# Then byte compile w/more optimization.
+# Only do this for Python 3.5+, see https://bugs.gnu.org/38043 for background.
case $python_major.$python_minor in
2.*|3.[0-4])
;;
-*) # Python 3.5+
- # See https://bugs.gnu.org/38043 for background.
+*)
$PYTHON -OO -c "
-import sys, os, py_compile, imp
+import sys, os, py_compile, importlib
-print('Byte-compiling python modules (-OO version) ...')
+sys.stdout.write('Byte-compiling python modules (more optimized versions)'
+ ' ...\n')
for file in sys.argv[1:]:
$pathtrans
$filetrans
- if not os.path.exists(filepath) or not (len(filepath) >= 3
- and filepath[-3:] == '.py'):
+ if (
+ not os.path.exists(filepath)
+ or not (len(filepath) >= 3 and filepath[-3:] == '.py')
+ ):
continue
- print(file, end=' ', flush=True)
- if hasattr(imp, 'get_tag'):
- py_compile.compile(filepath, imp.cache_from_source(filepath), path)
- else:
- py_compile.compile(filepath, filepath + 'o', path)
-print()" "$@" 2>/dev/null || exit $?
+ sys.stdout.write(file + ' ')
+ sys.stdout.flush()
+ py_compile.compile(filepath, importlib.util.cache_from_source(filepath), path)
+sys.stdout.write('\n')" "$@" 2>/dev/null || exit $?
;;
esac