From 69666e08a7f6108d5d300c27fac32e71b197244b Mon Sep 17 00:00:00 2001 From: Michael Jeanson Date: Thu, 14 Dec 2023 17:11:42 -0500 Subject: [PATCH] fix: 'load_module()' deprecated in Python 3.12 Starting with Python 3.10 we get the following warning: :283: DeprecationWarning: the load_module() method is deprecated and slated for removal in Python 3.12; use exec_module() instead Replace our use of 'load_module()' with the recommended [1] alternative that includes 'module_from_spec()' introduced in Python 3.5. Since we need to support Python 3.4, keep the original code with a version check. [1] https://docs.python.org/3.12/library/importlib.html#importlib.machinery.SourceFileLoader.load_module Change-Id: I77d14f14fcc39e0227baad959c3e211aba309a21 Signed-off-by: Michael Jeanson Reviewed-on: https://review.lttng.org/c/babeltrace/+/11583 Tested-by: jenkins Reviewed-by: Simon Marchi --- src/bindings/python/bt2/bt2/py_plugin.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/bindings/python/bt2/bt2/py_plugin.py b/src/bindings/python/bt2/bt2/py_plugin.py index 5702a2fe..223615a5 100644 --- a/src/bindings/python/bt2/bt2/py_plugin.py +++ b/src/bindings/python/bt2/bt2/py_plugin.py @@ -105,8 +105,18 @@ def _try_load_plugin_module(path): h.update(path.encode()) module_name = "bt_plugin_{}".format(h.hexdigest()) assert module_name not in sys.modules + # try loading the module: any raised exception is catched by the caller - mod = importlib.machinery.SourceFileLoader(module_name, path).load_module() + if sys.version_info < (3, 5): + mod = importlib.machinery.SourceFileLoader(module_name, path).load_module() + else: + import importlib.util + + loader = importlib.machinery.SourceFileLoader(module_name, path) + spec = importlib.util.spec_from_file_location(module_name, path, loader=loader) + mod = importlib.util.module_from_spec(spec) + sys.modules[mod.__name__] = mod + loader.exec_module(mod) # we have the module: look for its plugin info first if not hasattr(mod, "_bt_plugin_info"): -- 2.34.1