]> git.ipfire.org Git - thirdparty/automake.git/commitdiff
py-compile: fix optimized compiling for Python 3.5+
authorMike Frysinger <vapier@gentoo.org>
Sun, 6 Feb 2022 06:25:59 +0000 (01:25 -0500)
committerMike Frysinger <vapier@gentoo.org>
Thu, 24 Feb 2022 04:48:55 +0000 (23:48 -0500)
Fixes automake bug https://bugs.gnu.org/38043.

Split the optimized compilation logic into a new section.  This avoids
trying to support multiple versions of major versions in a single script
as it gets harder to verify new changes don't break old versions as time
goes on.

Now for Python 3.5+, compile with -O0 (which is "higher" than -O).

* NEWS: Mention fix.
* THANKS: Add Michal Górny.
* lib/py-compile: Add new section for compiling Python 3.5+.

NEWS
THANKS
lib/py-compile

diff --git a/NEWS b/NEWS
index 665d8329d667b041bfd490b2e29b956447c8b55d..8a1cbed49036db9fb1ea296d0b384886339f76b6 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -10,6 +10,9 @@ New in 1.17:
   - When compiling emacs lisp files, emacs is run with --no-site-file to
     disable user config files that might hang or access the terminal.
 
+  - Compiling Python modules with Python 3.5+ will use multiple optimization
+    levels now.
+
 * New features added
 
   - RANLIB may be overridden on a per-target basis.
diff --git a/THANKS b/THANKS
index 1d64877ecf60114620113fa1b68fbc283ecab848..41e15eb73b34470315c0ba74794ffe874b8d5bcf 100644 (file)
--- a/THANKS
+++ b/THANKS
@@ -244,7 +244,7 @@ Leonardo Boiko                  leoboiko@conectiva.com.br
 Libor Bukata                    libor.bukata@oracle.com
 Loulou Pouchet                  loulou@lrde.epita.fr
 Ludovic Courtès                 ludo@gnu.org
-Lukas Fleischer                        lfleischer@lfos.de
+Lukas Fleischer                 lfleischer@lfos.de
 Luo Yi                          luoyi.ly@gmail.com
 Maciej Stachowiak               mstachow@mit.edu
 Maciej W. Rozycki               macro@ds2.pg.gda.pl
@@ -286,6 +286,7 @@ Michael Daniels                 mdaniels@rim.com
 Michael Hofmann                 mhofma@googlemail.com
 Michael Ploujnikov              ploujj@gmail.com
 Michael Zucchi                  notzed@gmail.com
+Michał Górny                    mgorny@gentoo.org
 Michel de Ruiter                mdruiter@cs.vu.nl
 Mike Castle                     dalgoda@ix.netcom.com
 Mike Frysinger                  vapier@gentoo.org
index 6295b2dac78e12027e09cb9f675f0219cb8ea0a8..24a8a1a543efc358d072d617362bc1e702034ce5 100755 (executable)
@@ -131,6 +131,13 @@ case $python_major in
   ;;
 esac
 
+python_minor=`$PYTHON -c 'import sys; print(sys.version_info[1])'`
+
+# NB: When adding support for newer versions, prefer copying & adding new cases
+# rather than try to keep things merged with shell variables.
+
+# First byte compile (no optimization) all the modules.
+# This works for all currently known Python versions.
 $PYTHON -c "
 import sys, os, py_compile, importlib
 
@@ -149,7 +156,10 @@ for file in sys.argv[1:]:
         py_compile.compile(filepath, filepath + 'c', path)
 sys.stdout.write('\n')" "$@" || exit $?
 
-$PYTHON -O -c "
+# Then byte compile w/optimization all the modules.
+case $python_major.$python_minor in
+2.*)
+  $PYTHON -O -c "
 import sys, os, py_compile, importlib
 
 # pypy does not use .pyo optimization
@@ -170,6 +180,51 @@ for file in sys.argv[1:]:
     else:
         py_compile.compile(filepath, filepath + 'o', 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.
+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
+
+print('Byte-compiling python modules (-OO version) ...')
+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(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 $?
+  ;;
+esac
 
 # Local Variables:
 # mode: shell-script