From a69b00851279fd3e3c729e313e5c8bce4b38f12a Mon Sep 17 00:00:00 2001 From: James Estevez Date: Wed, 25 Oct 2023 16:14:29 -0700 Subject: [PATCH] Port imp.load_source to importlib The imp module was removed in Python 3.12. This commit replaces its use with the equivalent importlib version given in the 3.12 [release notes]. [release notes]: https://docs.python.org/3.12/whatsnew/3.12.html#imp --- rflint/rflint.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/rflint/rflint.py b/rflint/rflint.py index 8355fd9..d731be3 100644 --- a/rflint/rflint.py +++ b/rflint/rflint.py @@ -22,7 +22,8 @@ import sys import glob import argparse -import imp +import importlib.machinery +import importlib.util from .common import SuiteRule, ResourceRule, TestRule, KeywordRule, GeneralRule, Rule from .common import ERROR, WARNING, IGNORE @@ -238,11 +239,19 @@ def _load_rule_file(self, filename): try: basename = os.path.basename(filename) (name, ext) = os.path.splitext(basename) - imp.load_source(name, filename) + self._load_source(name, filename) IMPORTED_RULE_FILES.append(abspath) except Exception as e: sys.stderr.write("rflint: %s: exception while loading: %s\n" % (filename, str(e))) + def _load_source(self, 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) + sys.modules[module.__name__] = module + loader.exec_module(module) + return module + def parse_and_process_args(self, args): """Handle the parsing of command line arguments."""