From 9e5710db5ab5b2fbfdbee443413eccf86c9c0439 Mon Sep 17 00:00:00 2001 From: Osmo Salomaa Date: Sat, 22 Jun 2024 00:54:30 +0300 Subject: [PATCH] Don't use deprecated load_module Closes #227 --- gaupol/extensionman.py | 4 +--- gaupol/util.py | 9 +++++++++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/gaupol/extensionman.py b/gaupol/extensionman.py index 226e1ef2..8b0fec4c 100644 --- a/gaupol/extensionman.py +++ b/gaupol/extensionman.py @@ -19,7 +19,6 @@ import aeidon import gaupol -import importlib.machinery import inspect import os import re @@ -122,8 +121,7 @@ def setup_extension(self, module, slave=False): directory = os.path.dirname(metadata.path) name = "gaupol.extension.{:s}".format(module) path = os.path.join(directory, "{}.py".format(module)) - loader = importlib.machinery.SourceFileLoader(name, path) - module_object = loader.load_module(name) + module_object = gaupol.util.load_module_from_file(name, path) for attribute in dir(module_object): if attribute.startswith("_"): continue value = getattr(module_object, attribute) diff --git a/gaupol/util.py b/gaupol/util.py index 8420ea86..4a40a257 100644 --- a/gaupol/util.py +++ b/gaupol/util.py @@ -19,6 +19,7 @@ import aeidon import gaupol +import importlib.util import inspect import sys import traceback @@ -227,6 +228,14 @@ def lines_to_px(nlines, font=None): height = label.get_preferred_height()[1] return int(round(nlines * height)) +def load_module_from_file(name, path): + # https://docs.python.org/3/library/importlib.html#importing-a-source-file-directly + spec = importlib.util.spec_from_file_location(name, path) + module = importlib.util.module_from_spec(spec) + sys.modules[name] = module + spec.loader.exec_module(module) + return module + def new_hbox(spacing): """Return a new horizontal :class:`Gtk.Box`.""" return Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL,