[PATCH] Handle several variants of Python 'imp', 'importlib' modules

Thomas Schwinge thomas@codesourcery.com
Wed Jan 5 15:32:52 GMT 2022


Fix-up for recent commit f0582fdbf1267f0f34bf3c3b6698b60026410146
"Replace use of deprecated Python 'imp' module with 'importlib'", and
commit cc1f38ffedb8d456d43fb52c369409037c5ca4a
"Replace Python 'import importlib' with 'import importlib.machinery'",
because compatibility...

	* tests/mockfedabipkgdiff.in: Handle several variants of Python
	'imp', 'importlib' modules.

Signed-off-by: Thomas Schwinge <thomas@codesourcery.com>
Tested-by: Mark Wielaard <mark@klomp.org> (CentOS 7)
---
 tests/mockfedabipkgdiff.in | 29 +++++++++++++++++++++++++++--
 1 file changed, 27 insertions(+), 2 deletions(-)

diff --git tests/mockfedabipkgdiff.in tests/mockfedabipkgdiff.in
index 29a28ad9..1e562666 100644
--- tests/mockfedabipkgdiff.in
+++ tests/mockfedabipkgdiff.in
@@ -39,7 +39,6 @@ variables.
 
 import os
 import tempfile
-import importlib.machinery
 import six
 
 try:
@@ -69,8 +68,34 @@ def get_download_dir():
     return DOWNLOAD_CACHE_DIR
 
 
+def load_source(name, path):
+    # Different version of Python want this be done differently.
+    try:
+        import importlib.machinery
+        loader = importlib.machinery.SourceFileLoader(name, path)
+        import importlib.util
+        spec = importlib.util.spec_from_loader(name, loader)
+        module = importlib.util.module_from_spec(spec)
+        spec.loader.exec_module(module)
+        import sys.modules
+        sys.modules[name] = module
+        return module
+    except:
+        pass
+    try:
+        import importlib.machinery
+        loader = importlib.machinery.SourceFileLoader(name, path)
+        module = loader.load_module()
+        return module
+    except:
+        pass
+    import imp
+    module = imp.load_source(name, path)
+    return module
+
+
 # Import the fedabipkgdiff program file from the source directory.
-fedabipkgdiff_mod = importlib.machinery.SourceFileLoader('fedabipkgdiff', FEDABIPKGDIFF).load_module()
+fedabipkgdiff_mod = load_source('fedabipkgdiff', FEDABIPKGDIFF)
 
 
 # -----------------  Koji resource storage begins ------------------
-- 
2.34.1



More information about the Libabigail mailing list