automake-commit
[Top][All Lists]
Advanced

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

[automake-commit] branch master updated: python: support both Python 2 a


From: Karl Berry
Subject: [automake-commit] branch master updated: python: support both Python 2 and 3 in py-compile
Date: Tue, 25 Feb 2020 21:07:38 -0500

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=9ba2f0ddbcf08d4d67baffa8779abff7042a1c96

The following commit(s) were added to refs/heads/master by this push:
     new 9ba2f0d  python: support both Python 2 and 3 in py-compile
9ba2f0d is described below

commit 9ba2f0ddbcf08d4d67baffa8779abff7042a1c96
Author: Karl Berry <address@hidden>
AuthorDate: Tue Feb 25 18:07:15 2020 -0800

    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
---
 lib/py-compile | 33 ++++++++++++++++++++++++++-------
 1 file changed, 26 insertions(+), 7 deletions(-)

diff --git a/lib/py-compile b/lib/py-compile
index f2be7d0..e56d98d 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 || :



reply via email to

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