Skip to contentSkip to conversation

scripts: Update to use exec_module() instead of load_module()

PATCH New Richard Purdie 1 participant 1 message Jan 4, 2022

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 scripts: Update to use exec_module() instead of load_module()

This is deprecated in python 3.12 and Fedora 35 is throwing warnings so move to the new functions.

---
 scripts/lib/scriptutils.py    | 4 +++-
 scripts/lib/wic/pluginbase.py | 5 ++++-
 2 files changed, 7 insertions(+), 2 deletions(-)
scripts/lib/scriptutils.py +3 -1
index 3164171eb28..da778ea25ec 100644
@@ -82,7 +82,9 @@ def load_plugins(logger, plugins, pluginpath):
         logger.debug('Loading plugin %s' % name)
         spec = importlib.machinery.PathFinder.find_spec(name, path=[pluginpath] )
         if spec:
-            return spec.loader.load_module()
+            mod = importlib.util.module_from_spec(spec)
+            spec.loader.exec_module(mod)
+            return mod
 
     def plugin_name(filename):
         return os.path.splitext(os.path.basename(filename))[0]
scripts/lib/wic/pluginbase.py +4 -1
index d9b4e57747e..d0651da4b71 100644
@@ -9,6 +9,7 @@ __all__ = ['ImagerPlugin', 'SourcePlugin']
 
 import os
 import logging
+import types
 
 from collections import defaultdict
 from importlib.machinery import SourceFileLoader
@@ -54,7 +55,9 @@ class PluginMgr:
                             mname = fname[:-3]
                             mpath = os.path.join(ppath, fname)
                             logger.debug("loading plugin module %s", mpath)
-                            SourceFileLoader(mname, mpath).load_module()
+                            loader = SourceFileLoader(mname, mpath)
+                            mod = types.ModuleType(loader.name)
+                            loader.exec_module(mod)
 
         return PLUGINS.get(ptype)
 
Signature · 1 line
2.32.0