diff mbox series

Handle several variants of Python 'imp', 'importlib' modules

Message ID 20211221194117.2386192-1-thomas@codesourcery.com
State Superseded
Headers show
Series Handle several variants of Python 'imp', 'importlib' modules | expand

Commit Message

Thomas Schwinge Dec. 21, 2021, 7:41 p.m. UTC
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.

Once more, I've asked The Internet what to do about that, and this commit is
the result.  But beware: I'm still not much of a Python wizard.

	* tests/mockfedabipkgdiff.in: Handle several variants of Python
	'imp', 'importlib' modules.
---
 tests/mockfedabipkgdiff.in | 29 +++++++++++++++++++++++++++--
 1 file changed, 27 insertions(+), 2 deletions(-)

Comments

Dodji Seketeli Jan. 3, 2022, 4:33 p.m. UTC | #1
Hello Thomas,

Happy New Years!

Thomas Schwinge <thomas@codesourcery.com> a écrit:

> 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.
>
> Once more, I've asked The Internet what to do about that, and this commit is
> the result.  But beware: I'm still not much of a Python wizard.
>
> 	* tests/mockfedabipkgdiff.in: Handle several variants of Python
> 	'imp', 'importlib' modules.

The patch looks good to me.

However, it lacks the 'signed-of-by' mention.

[...]

Cheers,
Mark Wielaard Jan. 4, 2022, 2:51 p.m. UTC | #2
Hi Thomas,

On Tue, 2021-12-21 at 20:41 +0100, Thomas Schwinge wrote:
> 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.
> 
> Once more, I've asked The Internet what to do about that, and this
> commit is
> the result.  But beware: I'm still not much of a Python wizard.
> 
> 	* tests/mockfedabipkgdiff.in: Handle several variants of Python
> 	'imp', 'importlib' modules.

This variant works for me on a CentOS 7 setup.

Thanks,

Mark
diff mbox series

Patch

diff --git a/tests/mockfedabipkgdiff.in b/tests/mockfedabipkgdiff.in
index 29a28ad9..1e562666 100644
--- a/tests/mockfedabipkgdiff.in
+++ b/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 ------------------