Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mavexpression.py: cope with imp module being removed in 3.12 #978

Merged
merged 1 commit into from
Sep 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading