Hi Karl,

Thanks for reviewing.
I had not thought about it, but it is absolutely true that autotools should
not drop python2 support.

I had a look at when the imp module has been explicitly deprecated. In
cpython, it has been flagged so in commit
e4f41deccf94ccc798b1eb1f44657ade66669a60, which is part of releases >= 3.4.
I would have expected your version of python to raise a warning. Maybe it's
a distribution specific change ?
I checked Debian Jessie (old-old-stable) which uses 3.4, and already has
that warning.

I started to implement as you suggested but I found it heavy, and so I
propose to try the new code and to fallback
on the old version on error. Please tell me if this too much of an
optimistic approach.

Best regards,

On Mon, Feb 17, 2020 at 3:09 AM Karl Berry <k...@freefriends.org> wrote:

> Hi Gabriel - back on your patch from last July (sorry):
>
>
> https://lists.gnu.org/archive/html/automake-patches/2019-07/msg00002.html
>
>     replace the imp functions with the new ones from importlib.
>     ...
>     -import sys, os, py_compile, imp
>     +import sys, os, py_compile, importlib
>
> I understand the problem, but unfortunately the new code is not
> compatible with Python 2. I don't know the best way to handle that. Help?
>
> With the patch, the tests, e.g., py-compile-basedir, fail with the
> not-unexpected:
>
>   + ./py-compile --basedir foo __init__.py sub/__init__.py
>   Byte-compiling python modules...
>   __init__.pyTraceback (most recent call last):
>     File "<string>", line 15, in <module>
>   AttributeError: 'module' object has no attribute 'implementation'
>
> I know Python 2 itself is being eradicated, but still, automake cannot
> simply abandon it.
>
> About all I can imagine is to change the above line to something like:
>   import sys, os, py_compile, $import_library
> and check the version number of $PYTHON in the script before and set
> $import_library accordingly. That is not especially pretty. Is there
> a way to do it within Python that would be cleaner and work with both
> versions?
>
> I also don't see the deprecation warning, but I only have Python 3.6.8
> at the moment, so I suppose it came in with Python 3.7. --thanks, karl.
>

-- 





**Attention: Confidential**

This email message is for the sole use of 
the intended recipient(s) and may contain confidential and/or privileged 
information.
Any unauthorized review, use, disclosure or distribution is 
prohibited.
If you are not the intended recipient, please contact me by 
reply email and destroy all copies of the original message.
From 43a436b214cec70639d1c61ffaf1e724a2ac00d6 Mon Sep 17 00:00:00 2001
From: Gabriel Ganne <gabriel.ga...@mindmaze.ch>
Date: Sat, 13 Jul 2019 08:14:06 +0200
Subject: [PATCH] python3: fix deprecation warning

Python3 "imp" module is deprecated since version 3.4 in favor of the
importlib module. On debian stable (python3.7) this raises a deprecation
warning each time you byte-compile python code.

This commit replaces the functions:
 imp.cache_from_source() -> importlib.util.cache_from_source()

Keep the python2 implementation (using the old "imp" module) for
compatibility.
---
 lib/py-compile | 32 ++++++++++++++++++++++----------
 1 file changed, 22 insertions(+), 10 deletions(-)

diff --git a/lib/py-compile b/lib/py-compile
index f2be7d084..bed86e301 100755
--- a/lib/py-compile
+++ b/lib/py-compile
@@ -116,7 +116,7 @@ else
 fi
 
 $PYTHON -c "
-import sys, os, py_compile, imp
+import sys, os, py_compile
 
 files = '''$files'''
 
@@ -129,15 +129,21 @@ 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)
-    else:
-        py_compile.compile(filepath, filepath + 'c', path)
+    try:
+        import importlib
+        py_compile.compile(filepath, importlib.util.cache_from_source(filepath), path)
+    except:
+        # python2
+        import imp
+        if hasattr(imp, 'get_tag'):
+            py_compile.compile(filepath, imp.cache_from_source(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
 
 # pypy does not use .pyo optimization
 if hasattr(sys, 'pypy_translation_info'):
@@ -153,10 +159,16 @@ 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)
-    else:
-        py_compile.compile(filepath, filepath + 'o', path)
+    try:
+        import importlib
+        py_compile.compile(filepath, importlib.util.cache_from_source(filepath), path)
+    except:
+        # python2
+        import imp
+        if hasattr(imp, 'get_tag'):
+            py_compile.compile(filepath, imp.cache_from_source(filepath, False), path)
+        else:
+            py_compile.compile(filepath, filepath + 'o', path)
 sys.stdout.write('\n')" 2>/dev/null || :
 
 # Local Variables:
-- 
2.23.0

Reply via email to