From: Guytri KASTANE Date: Mon, 24 Jun 2024 12:56:54 +0000 (+0200) Subject: spns #41954: support python 3.12 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=340f9859588c7a8e40b5109129080db371dc7302;p=tools%2Fsat.git spns #41954: support python 3.12 --- diff --git a/commands/generate.py b/commands/generate.py index a6a83ce..67df03d 100644 --- a/commands/generate.py +++ b/commands/generate.py @@ -19,7 +19,11 @@ 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..4e4681f 100755 --- a/src/salomeTools.py +++ b/src/salomeTools.py @@ -26,7 +26,6 @@ This file is the main API file for salomeTools """ import sys - # exit OKSYS and KOSYS seems equal on linux or windows _OKSYS = 0 # OK _KOSYS = 1 # KO @@ -45,7 +44,11 @@ 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..3792b13 100644 --- a/src/test_module.py +++ b/src/test_module.py @@ -30,7 +30,10 @@ 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