]> git.ipfire.org Git - thirdparty/automake.git/commitdiff
python: better Python compilation portability.
authorZack Weinberg <zackw@panix.com>
Wed, 28 Sep 2022 17:12:24 +0000 (10:12 -0700)
committerKarl Berry <karl@freefriends.org>
Wed, 28 Sep 2022 17:12:24 +0000 (10:12 -0700)
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.

lib/py-compile

index 24a8a1a543efc358d072d617362bc1e702034ce5..de0f02e321115d55aa13a48e5c61f4f563599469 100755 (executable)
@@ -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