automake-commit
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[automake-commit] branch master updated: python: better Python compilati


From: Karl Berry
Subject: [automake-commit] branch master updated: python: better Python compilation portability.
Date: Wed, 28 Sep 2022 13:12:37 -0400

This is an automated email from the git hooks/post-receive script.

karl pushed a commit to branch master
in repository automake.

View the commit online:
https://git.savannah.gnu.org/gitweb/?p=automake.git;a=commitdiff;h=13fb472cd02d86bf33c092609ac00506465d2df1

The following commit(s) were added to refs/heads/master by this push:
     new 13fb472cd python: better Python compilation portability.
13fb472cd is described below

commit 13fb472cd02d86bf33c092609ac00506465d2df1
Author: Zack Weinberg <zackw@panix.com>
AuthorDate: Wed Sep 28 10:12:24 2022 -0700

    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.
---
 lib/py-compile | 99 ++++++++++++++++++++++++++++------------------------------
 1 file 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
 



reply via email to

[Prev in Thread] Current Thread [Next in Thread]