Skip to content

Commit

Permalink
mavexpression.py: cope with imp module being removed in 3.12
Browse files Browse the repository at this point in the history
  • Loading branch information
peterbarker committed Sep 21, 2024
1 parent feb81c4 commit 250e3cc
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions mavexpression.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 250e3cc

Please sign in to comment.