automake-patches
[Top][All Lists]
Advanced

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

Re: Fix python3 deprecation warning


From: Karl Berry
Subject: Re: Fix python3 deprecation warning
Date: Wed, 19 Feb 2020 16:47:50 -0700

    I would have expected your version of python to raise a warning. Maybe it's
    a distribution specific change ?

Looks like that way. The CentOS7 packages for python3 still provide a
/usr/lib64/python3.6/imp.py without any deprecation warnings that I can
discern. Not that I'm sure of anything when it comes to Python.

    propose to try the new code and to fallback
    on the old version on error. 

Fall back how? You mean with some python try/catch mechanism?
That was my original question :)
I know next to nothing about Python.

    I started to implement as you suggested but I found it heavy

It's annoying but seems doable, at least. Below is the diff for what I
had in mind. (Sorry, I didn't have time for all the git administrivia
stuff.) I think the expanded strings are equivalent to either the
existing py-compile (for P2) or your patch (for P3). That was the intent
anyway.

I have not run every test yet, but the t/py-compile-basedir.sh test does
works for me with both the P2[.7] and P3[.6] that I have here on
centos7. Can you try it?  It will be several days before I can get back
to this, so no super rush.

If it is necessary to only use importlib on 3.something or newer, and
not just any P3, that could be done, but I just checked the major version
for now. Please advise.

I see that https://docs.python.org/3/library/importlib.html says it was
introduced in 3.1, but I'm supposing there is no one left running 3.0.
--thanks, karl.


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]