From c2f0bb3d7de38944ef9e812dfc1924a42be16024 Mon Sep 17 00:00:00 2001 From: Peter Barker Date: Mon, 16 Sep 2024 11:51:33 +1000 Subject: [PATCH] mavexpression.py: cope with imp module being removed in 3.12 --- mavexpression.py | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/mavexpression.py b/mavexpression.py index 48a2b483a..5c56372af 100644 --- a/mavexpression.py +++ b/mavexpression.py @@ -19,8 +19,28 @@ if home is not None: extra = os.path.join(home, '.pymavlink', 'mavextra.py') if os.path.exists(extra): - import imp - mavuser = imp.load_source('pymavlink.mavuser', extra) + try: + import imp + mavuser = imp.load_source('pymavlink.mavuser', extra) + except ModuleNotFoundError: + # "imp" is removed in Python 3.12. Try to use importlib instead: + import sys + # from: https://docs.python.org/dev/whatsnew/3.12.html#removed + import importlib.util + import importlib.machinery + + def load_source(modname, filename): + loader = importlib.machinery.SourceFileLoader(modname, filename) + spec = importlib.util.spec_from_file_location(modname, filename, loader=loader) + module = importlib.util.module_from_spec(spec) + # The module is always executed and not cached in sys.modules. + # Uncomment the following line to cache the module. + sys.modules[module.__name__] = module + loader.exec_module(module) + return module + + load_source('pymavlink.mavuser', extra) + from pymavlink.mavuser import * def evaluate_expression(expression, vars, nocondition=False):