]> SALOME platform Git repositories - modules/kernel.git/commitdiff
Salome HOME
Use importlib instead of imp and __import__ EMC2P-1.3.6-rc4 EMC2P-1.3.6-rc5
authorGilles DAVID <gilles-g.david@edf.fr>
Thu, 30 Jun 2022 20:59:05 +0000 (22:59 +0200)
committerGilles DAVID <gilles-g.david@edf.fr>
Thu, 30 Jun 2022 20:59:05 +0000 (22:59 +0200)
bin/setenv.py
src/Container/SALOME_Container.py
src/Container/SALOME_ContainerPy.py
src/KERNEL_PY/kernel/services.py
src/KERNEL_PY/kernel/unittester.py
src/KERNEL_PY/salome_test.py

index 26c7919c76758ab7296db598c4e616ad4107d3f1..824e6a96246558104d7f2591848ef658535bb325 100755 (executable)
@@ -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:
index b2c962f0d455d93993eb0956b46a7a8ac2c462ab..b42536b8a45a9a63fbe723e84b4a8234797d23cc 100644 (file)
@@ -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:
index 4383a22fc14bb8f0eb3c1567ac43e2fe1c921a0f..3d533a7e3b6048f33dd8c6973bd6e477d56334e0 100755 (executable)
 #
 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()
index 2ad3a8bb68e2efc40ed691ab60028e46ee67f2ca..bfae2d7a9cd9f289a5266dba397cc174b803a86e 100644 (file)
@@ -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")
index 3516aea65ee6591ebe151aec6be3c322afd830f5..761cc74c895e9b3dd05b1f107cc91190ce91cc3c 100644 (file)
@@ -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))
index 0a0863cfd1e5c22db399afa84469d03139e9e959..9a49fb180fb8dcd3cc977ea166be775adbc3441f 100644 (file)
@@ -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: