Handle several variants of Python 'imp', 'importlib' modules
Commit Message
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
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,
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
@@ -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 ------------------