From 0127461331e37e427a20103e0ba114164df52af4 Mon Sep 17 00:00:00 2001 From: Guytri KASTANE Date: Mon, 24 Jun 2024 14:56:54 +0200 Subject: [PATCH] use importlib instead of imp --- commands/generate.py | 5 ++++- src/salomeTools.py | 30 ++++++++++++++++++++++++------ src/test_module.py | 4 +++- 3 files changed, 31 insertions(+), 8 deletions(-) diff --git a/commands/generate.py b/commands/generate.py index a6a83ce..9d5bf54 100644 --- a/commands/generate.py +++ b/commands/generate.py @@ -19,7 +19,10 @@ import os import sys import shutil -import imp +if sys.version_info[:2] >= (3,12): + import importlib + import importlib.util +else: import imp import subprocess import src diff --git a/src/salomeTools.py b/src/salomeTools.py index 2b1a50d..877ef83 100755 --- a/src/salomeTools.py +++ b/src/salomeTools.py @@ -45,7 +45,10 @@ ERROR: 'salomeTools.py' is not main command entry (CLI) for salomeTools. import os import re import tempfile -import imp +if sys.version_info[:2] >= (3,12): + import importlib + import importlib.util +else: import imp import types import gettext import traceback @@ -361,8 +364,13 @@ class Sat(object): continue # load the module that has name nameCmd in dirPath - (file_, pathname, description) = imp.find_module(nameCmd, [dirPath]) - module = imp.load_module(nameCmd, file_, pathname, description) + if sys.version_info[:2] >= (3,12): + spec = importlib.machinery.PathFinder().find_spec(nameCmd, [dirPath]) + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + else: + (file_, pathname, description) = imp.find_module(nameCmd, [dirPath]) + module = imp.load_module(nameCmd, file_, pathname, description) def run_command(args='', options=None, @@ -609,7 +617,12 @@ class Sat(object): # Try to execute the script, catch the exception if it fails try: # import the module (in the sense of python) - pymodule = imp.load_source(cmd_name, hook_script_path) + if sys.version_info[:2] >= (3,12): + spec = importlib.machinery.PathFinder().find_spec(cmd_name, hook_script_path) + pymodule = importlib.util.module_from_spec(spec) + spec.loader.exec_module(pymodule) + else: + pymodule = imp.load_source(cmd_name, hook_script_path) # format a message to be printed at hook execution msg = src.printcolors.printcWarning(_("Run hook script")) @@ -676,8 +689,13 @@ class Sat(object): raise src.SatException(_("Command '%s' does not exist") % module) # load the module - (file_, pathname, description) = imp.find_module(module, [cmdsdir]) - module = imp.load_module(module, file_, pathname, description) + if sys.version_info[:2] >= (3,12): + spec = importlib.util.find_spec(module) + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + else: + (file_, pathname, description) = imp.find_module(module, [cmdsdir]) + module = imp.load_module(module, file_, pathname, description) return module ################################################################## diff --git a/src/test_module.py b/src/test_module.py index 218ba18..285635d 100644 --- a/src/test_module.py +++ b/src/test_module.py @@ -30,7 +30,9 @@ import sys import datetime import shutil import string -import imp +if sys.version_info[:2] >= (3,12): + import importlib +else: import imp import subprocess import glob import pprint as PP -- 2.39.2