From 43a436b214cec70639d1c61ffaf1e724a2ac00d6 Mon Sep 17 00:00:00 2001 From: Gabriel Ganne 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