From c67cfb56fc89ac8a6df339177ff4d9c51a7deb70 Mon Sep 17 00:00:00 2001 From: Gilles DAVID Date: Thu, 30 Jun 2022 22:59:05 +0200 Subject: [PATCH] Use importlib instead of imp and __import__ --- bin/setenv.py | 3 ++- src/Container/SALOME_Container.py | 17 +++++++------- src/Container/SALOME_ContainerPy.py | 36 ++++++++++++++--------------- src/KERNEL_PY/kernel/services.py | 3 ++- src/KERNEL_PY/kernel/unittester.py | 7 +++--- src/KERNEL_PY/salome_test.py | 3 ++- 6 files changed, 37 insertions(+), 32 deletions(-) diff --git a/bin/setenv.py b/bin/setenv.py index 26c7919c7..824e6a962 100755 --- a/bin/setenv.py +++ b/bin/setenv.py @@ -207,7 +207,8 @@ def set_env(args, modules_list, modules_root_dir, silent=False, keepEnvironment= # set environment by modules from the list if port: try: - mod=__import__(module.lower()+"_setenv") + import importlib + mod=importlib.import_module(module.lower()+"_setenv") mod.set_env(args) pass except Exception: diff --git a/src/Container/SALOME_Container.py b/src/Container/SALOME_Container.py index b2c962f0d..b42536b8a 100644 --- a/src/Container/SALOME_Container.py +++ b/src/Container/SALOME_Container.py @@ -34,7 +34,7 @@ import os import sys import traceback -import imp +import importlib from omniORB import CORBA, PortableServer import SALOMEDS import Engines, Engines__POA @@ -74,14 +74,15 @@ class SALOME_Container_i: ret="" try: if verbose(): print("try import ",componentName) - __import__(componentName) + importlib.import_module(componentName) if verbose(): print("import ",componentName," successful") - except ImportError as e: + except ImportError: #can't import python module componentName #try to find it in python path try: - fp, pathname, description = imp.find_module(componentName) - if fp:fp.close() + _specs = importlib.util.find_spec(componentName) + _module = importlib.util.module_from_spec(_specs) + _specs.loader.exec_module(_module) #module file found in path ret="Component "+componentName+": Python implementation found but it can't be loaded\n" ret=ret+traceback.format_exc(10) @@ -97,7 +98,7 @@ class SALOME_Container_i: traceback.print_exc() print("import ",componentName," not possible") return ret - + #------------------------------------------------------------------------- def create_component_instance(self, componentName, instanceName): @@ -105,7 +106,7 @@ class SALOME_Container_i: comp_iors="" ret="" try: - component=__import__(componentName) + component=importlib.import_module(componentName) factory=getattr(component,componentName) comp_i=factory(self._orb, self._poa, @@ -122,7 +123,7 @@ class SALOME_Container_i: traceback.print_exc() MESSAGE( "SALOME_Container_i::create_component_instance : NOT OK") return comp_iors, ret - + def create_pynode(self,nodeName,code): try: diff --git a/src/Container/SALOME_ContainerPy.py b/src/Container/SALOME_ContainerPy.py index 4383a22fc..3d533a7e3 100755 --- a/src/Container/SALOME_ContainerPy.py +++ b/src/Container/SALOME_ContainerPy.py @@ -30,10 +30,10 @@ # import os import sys -import string +import importlib from omniORB import CORBA, PortableServer -import SALOMEDS +import SALOMEDS import Engines, Engines__POA import salome_psutil from SALOME_NamingServicePy import * @@ -77,7 +77,7 @@ class SALOME_ContainerPy_Gen_i(Engines__POA.Container): self._numInstance = self._numInstance +1 instanceName = nameToRegister + "_inst_" + repr(self._numInstance) - component=__import__(componentName) + component=importlib.import_module(componentName) factory=getattr(component,componentName) comp_i=factory(self._orb, self._poa, self._this(), self._containerName, instanceName, nameToRegister) @@ -100,16 +100,16 @@ class SALOME_ContainerPy_Gen_i(Engines__POA.Container): exec(the_command) comp_o = comp_i._this() return comp_o - + #------------------------------------------------------------------------- - + def import_component(self, componentName): MESSAGE( "SALOME_Container_i::import_component" ) reason = "" try: if verbose(): print("try import %s" % componentName) # try import component - module=__import__(componentName) + module=importlib.import_module(componentName) if verbose(): print("import %s is done successfully" % componentName) # if import successfully, check that component is loadable if not hasattr(module, componentName): @@ -133,7 +133,7 @@ class SALOME_ContainerPy_Gen_i(Engines__POA.Container): interfaceName = componentName reason = self.import_component(componentName) return reason == "", reason - + #------------------------------------------------------------------------- def create_component_instance_env(self, componentName, env): @@ -145,7 +145,7 @@ class SALOME_ContainerPy_Gen_i(Engines__POA.Container): instanceName = componentName + "_inst_" + repr(self._numInstance) comp_iors="" try: - component=__import__(componentName) + component=importlib.import_module(componentName) factory=getattr(component,componentName) comp_i=factory(self._orb, self._poa, @@ -153,7 +153,7 @@ class SALOME_ContainerPy_Gen_i(Engines__POA.Container): self._containerName, instanceName, componentName) - + MESSAGE( "SALOME_Container_i::create_component_instance : OK") comp_o = comp_i._this() self._listInstances_map[instanceName] = comp_i @@ -172,13 +172,13 @@ class SALOME_ContainerPy_Gen_i(Engines__POA.Container): anEngine = self._listInstances_map[instance] return anEngine._this() return anEngine - + #------------------------------------------------------------------------- def create_python_service_instance(self, CompName): return self.create_component_instance(CompName) - + #------------------------------------------------------------------------- def remove_impl(self, component): @@ -255,7 +255,7 @@ class SALOME_ContainerPy_Gen_i(Engines__POA.Container): return self._machineName #------------------------------------------------------------------------- - + def _get_machineName(self): MESSAGE( "SALOME_ContainerPy_i::_get_MachineName" ) self._machineName = "localhost" @@ -288,7 +288,7 @@ class SALOME_ContainerPy_i(SALOME_ContainerPy_Gen_i): self._naming_service = naming_service MESSAGE( str(Container_path) ) naming_service.Register(self._this(), Container_path) - + #------------------------------------------------------------------------- def start_impl(self, ContainerName): @@ -324,15 +324,15 @@ class SALOME_ContainerPy_i(SALOME_ContainerPy_Gen_i): break num += 1 pass - + shstr += " > " shstr += fileName shstr += " 2>&1 &" - + #shstr += " > /tmp/" #shstr += ContainerName #shstr += ".log 2>&1 &" - + MESSAGE( "SALOME_ContainerPy_i::start_impl " + "os.system(" + str(shstr) + ")" ) os.system( shstr ) count = 21 @@ -364,7 +364,7 @@ class SALOME_ContainerPy_SSL_i(SALOME_ContainerPy_Gen_i): SALOME_ContainerPy_Gen_i.__init__(self, orb, poa, containerName) naming_service = SALOME_Embedded_NamingService() self._naming_service = naming_service._this() - + def get_embedded_NS_if_ssl(self): return self._naming_service @@ -382,7 +382,7 @@ if __name__ == "__main__": MESSAGE( str(sys.argv) ) containerName = sys.argv[1] cpy_i = SALOME_ContainerPy_i(orb, poa, containerName) - if verbose():print("SALOME_ContainerPy_i instance created ",cpy_i) + if verbose():print("SALOME_ContainerPy_i instance created ",cpy_i) cpy_o = cpy_i._this() if verbose():print("SALOME_ContainerPy_i instance activated ",cpy_o) sys.stdout.flush() diff --git a/src/KERNEL_PY/kernel/services.py b/src/KERNEL_PY/kernel/services.py index 2ad3a8bb6..bfae2d7a9 100644 --- a/src/KERNEL_PY/kernel/services.py +++ b/src/KERNEL_PY/kernel/services.py @@ -96,8 +96,9 @@ def getComponent(componentName = "SalomeTestComponent", """ Get a SALOME CORBA component from its name """ + import importlib print("INF: getting component %s from CORBA module %s ..."%(componentName,corbaModule)) - __import__(corbaModule) + importlib.import_module(corbaModule) component=salome.lcc.FindOrLoadComponent(containerType,componentName) if component is None: print("ERR: the SALOME component "+componentName+" can't be reached") diff --git a/src/KERNEL_PY/kernel/unittester.py b/src/KERNEL_PY/kernel/unittester.py index 3516aea65..761cc74c8 100644 --- a/src/KERNEL_PY/kernel/unittester.py +++ b/src/KERNEL_PY/kernel/unittester.py @@ -40,12 +40,13 @@ def run(modulePath, functionName): The function functionName is supposed here to return a boolean value indicating if the test is OK (True) or NOT OK (False) """ - moduleName = modulePath.replace('/','.') - __import__ (moduleName) + moduleName = modulePath.replace('/','.') + import importlib + importlib.import_module(moduleName) module=sys.modules[moduleName] func = getattr(module,functionName) tabsize = 70-len(moduleName)-len(functionName) - print("[TEST] %s.%s %s test in progress" % (moduleName, functionName,"."*tabsize)) + print("[TEST] %s.%s %s test in progress" % (moduleName, functionName,"."*tabsize)) ok = func() if ( ok ): print("[TEST] %s.%s %s OK" % (moduleName, functionName,"."*tabsize)) diff --git a/src/KERNEL_PY/salome_test.py b/src/KERNEL_PY/salome_test.py index 0a0863cfd..9a49fb180 100644 --- a/src/KERNEL_PY/salome_test.py +++ b/src/KERNEL_PY/salome_test.py @@ -21,6 +21,7 @@ # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com # +import importlib import sys import unittest @@ -44,7 +45,7 @@ def main(modules=None): for module in modules: test_name = '{}_test'.format(module.lower()) try: - __import__(test_name) + importlib.import_module(test_name) test_module = sys.modules[test_name] suite.addTest(loader.loadTestsFromModule(test_module)) except ImportError: -- 2.39.2