Salome HOME
PR: merge branch CCAR_br1
authorprascle <prascle>
Mon, 16 Jun 2003 13:50:13 +0000 (13:50 +0000)
committerprascle <prascle>
Mon, 16 Jun 2003 13:50:13 +0000 (13:50 +0000)
src/SALOMEGUI/PyInterp_base.cxx
src/SALOMEGUI/QAD_PyInterp.cxx
src/SALOME_SWIG/Makefile.in
src/SALOME_SWIG/salome_shared_modules.py

index 52aa2c19f0a78cc49c6527c54c9f0ea694f1f6fa..7356b84295492472fbfc9b739c9c89ec5edc4bc2 100644 (file)
@@ -101,8 +101,14 @@ void init_python()
    */
 
   PyInterp_base::salome_shared_modules_module =PyImport_ImportModule("salome_shared_modules");
+  if(PyInterp_base::salome_shared_modules_module == NULL){
+      MESSAGE("init_python: problem with salome_shared_modules import");
+      PyErr_Print();
+      PyErr_Clear();
+      salomeReleaseLock();
+      return;
+  }
   SCRUTE(PyInterp_base::salome_shared_modules_module->ob_refcnt);
-
   salomeReleaseLock();
 }
 
@@ -133,9 +139,17 @@ int compile_command(const char *command,PyObject *context)
   if (v == NULL)
     {
       /*
-       * Error encountered. It could be SyntaxError
+       * Error encountered. It should be SyntaxError
+       * so we don't write out traceback
        */
-      PyErr_Print();
+      PyObject *exception,*value,*tb;
+      PyErr_Fetch(&exception, &value, &tb);
+      PyErr_NormalizeException(&exception, &value, &tb);
+      PyErr_Display(exception, value, NULL);
+      Py_XDECREF(exception);
+      Py_XDECREF(value);
+      Py_XDECREF(tb);
+
       return -1;
     }
   else if (v == Py_None)
@@ -211,8 +225,8 @@ void PyInterp_base::initialize()
   if(m == NULL)
     {
       MESSAGE("Problem...");
-      ASSERT(0);
       PyErr_Print();
+      ASSERT(0);
       salomeReleaseLock(); 
       return;
     }   
index f4c0da963de2316bad8458c858a65a10aac17597..e3996723b17c4743104b6a6fb234781f35f8c9db 100644 (file)
@@ -78,8 +78,8 @@ void QAD_PyInterp::initContext()
   if(m == NULL)
     {
       MESSAGE("problem...");
-      ASSERT(0);
       PyErr_Print();
+      ASSERT(0);
       salomeReleaseLock(); 
       return;
     }  
@@ -90,21 +90,31 @@ void QAD_PyInterp::initContext()
     {
       PyDict_SetItemString(_g, "__builtins__", builtinmodule); // assign singleton __builtin__ module
     }
+// Debut modif CCAR
   /*
-   * Call salome_shared_modules to import salome shared modules that must not be initialized twice
-   * so import_shared_modules makes only a copy of the modules in _tstate->interp->modules
-   * (sys.modules)
+   * Import special module to change the import mechanism
    */
-  m= PyObject_CallMethod(salome_shared_modules_module,
-                        "import_shared_modules","O",
-                        _tstate->interp->modules);
-  if (m == NULL)
-    {
-      /*
-       * Problem  , print it
-       */
-      MESSAGE("problem...");
-      ASSERT(0);
+  m =PyImport_ImportModule("import_hook");
+  if(m == NULL){
+      MESSAGE("initContext: problem with import_hook import");
       PyErr_Print();
-    }
+      PyErr_Clear();
+      ASSERT(0);
+  }
+  /*
+   * Call init_shared_modules to initialize the shared import mechanism for modules 
+   * that must not be imported twice
+   */
+  if(m != NULL){
+      m= PyObject_CallMethod(m,
+                        "init_shared_modules","O",salome_shared_modules_module);
+      if (m == NULL){
+          MESSAGE("initContext: problem with init_shared_modules call");
+          PyErr_Print();
+          PyErr_Clear();
+          ASSERT(0);
+      }
+  }
+// Fin   modif CCAR
+
 }
index 3475d77dc4052661f9d56584db1eea7c5508555f..b247115741070d57a41a9817a5785cfb322d104c 100644 (file)
@@ -22,7 +22,9 @@ LIB = libSALOME_Swigcmodule.la
 LIB_SRC = 
 
 SWIG_DEF = libSALOME_Swig.i
-EXPORT_PYSCRIPTS = libSALOME_Swig.py Help.py PyInterp.py salome.py examplevtk1.py supervisionexample.py supervisiongeomexample.py salome_shared_modules.py batchmode_salome.py test_table.py test_big_table.py test_many_objects.py
+EXPORT_PYSCRIPTS = libSALOME_Swig.py Help.py PyInterp.py salome.py examplevtk1.py supervisionexample.py supervisiongeomexample.py salome_shared_modules.py batchmode_salome.py test_table.py test_big_table.py test_many_objects.py import_hook.py
+
+EXPORT_SHAREDPYSCRIPTS=kernel_shared_modules.py
 
 LIB_CLIENT_IDL = SALOMEDS.idl \
                 SALOME_Exception.idl
index 4f6d14c66df307e2ad01871675572d93aecdf879..de2b8742d34c86b15a89526af6411087f37b63f8 100644 (file)
-from SALOME_utilities import *
-
-"""
 """
-MESSAGE( "Module salome_shared_modules" )
-
-modules={}
-try:
-  # We try to import PyQt module. If present we import it
-  # as a "shared" module
-  import qt
-  modules["qt"]=qt.__dict__
-except:
-  pass
-
-# We keep in modules a copy of dictionnary modules
-# that need to be imported only once in multi-study context
-
-# Specific case : omniORB
-import omniORB
-modules["omniORB"]=omniORB.__dict__.copy()
-import omniORB.CORBA
-modules["omniORB.CORBA"]=omniORB.CORBA.__dict__.copy()
-modules["CORBA"]=modules["omniORB.CORBA"]
-import CosNaming
-modules["CosNaming"]=CosNaming.__dict__.copy()
-# end omniORB
-
-#
-# We search all Python CORBA (omniorb) modules.
-# A Python CORBA module has 2 associated Python packages 
-# These packages are named : <module_name> and <module_name>__POA
-#
-
-# 
-# SALOMEDS must be imported first, at least before any CORBA module
-# that references it.
-# It seems that import order of related CORBA modules is important
-# Perhaps, it's not sufficient so you should complete the list ???
-#
-import SALOMEDS
-import Engines
+This module with help of import_hook and *_shared_modules
+filters imports when using the embedded Python interpretor.
+
+Some imports can't be done more than once.
+This is related to the multi study feature that is implemented
+by using the Python multi interpreter feature.
+Some modules register objects or classes by calling modules
+implemented in C. These operations can't be done multiple times.
+So it's very important to control these imports.
+
+Examples:
+  - PyQt : import qt calls a C module to register classes
+  - OmniORB : import *_idl calls a C module to register CORBA interfaces
+
+Usage:
+  - First : the module salome_shared_modules is imported by main Python interpretor.
+    It will keep a dictionnary and a list that are shared among all
+    the subinterpretors and imports import_hook module that replaces
+    the standard import mechanism par one that prevents more than one import
+    for some modules identified by name (call register_name) or by a 
+    validator (call register_pattern).
+
+  Calls to register_name and register_pattern are done in modules named *_shared_modules
+  that could be found in the path SALOMEPATH
 
+"""
 import glob,os,sys
 
-repertoire=os.path.dirname(__file__)
-path=[repertoire,]
+import import_hook
+# shared_imported, patterns, register_name, register_pattern
+# will be shared by all Python sub interpretors
+from import_hook import shared_imported
+from import_hook import patterns
+from import_hook import register_name
+from import_hook import register_pattern
 
-# KERNEL is defined in KERNELCatalog
-#KERNEL_ROOT_DIR = os.getenv("KERNEL_ROOT_DIR")
-#if KERNEL_ROOT_DIR != None:
-#      path.append(os.path.join(KERNEL_ROOT_DIR,"lib","python"+sys.version[:3],"site-packages","salome"))
+register_name("salome_shared_modules")
 
+# Get the SALOMEPATH if set or else use KERNEL_ROOT_DIR that should be set.
+salome_path=os.environ.get("SALOMEPATH",os.getenv("KERNEL_ROOT_DIR"))
 
-#
-import SALOME_ModuleCatalog
-from SALOME_NamingServicePy import * 
-orb = CORBA.ORB_init([''], CORBA.ORB_ID)
-ns = SALOME_NamingServicePy_i(orb)
-modulecatalog = ns.Resolve('/Kernel/ModulCatalog')
-compos = []
-compos = modulecatalog.GetComponentList()
-
-for name in compos:
-       print name
-       MODULE_ROOT_DIR = os.getenv( name + "_ROOT_DIR" )
-       print MODULE_ROOT_DIR
-
-       if MODULE_ROOT_DIR != None:
-               path.append(os.path.join(MODULE_ROOT_DIR,"lib","python"+sys.version[:3],"site-packages","salome"))
-
-#SALOME_ROOT_DIR = os.getenv("SALOME_ROOT_DIR")
-#if SALOME_ROOT_DIR != None:
-#      path.append(os.path.join(SALOME_ROOT_DIR,"lib","python"+sys.version[:3],"site-packages","salome"))
-
-#SALOME_SITE_DIR = os.getenv("SALOME_SITE_DIR")
-#if SALOME_SITE_DIR != None:
-#        SALOME_SITE_NAME = os.getenv("SALOME_SITE_NAME")
-#        if SALOME_SITE_NAME != None:
-#              path.append(os.path.join(SALOME_SITE_DIR,"lib","python"+sys.version[:3],"site-packages",SALOME_SITE_NAME))
-
-MESSAGE( str(path) )
+list_modules=[]
 
+# Import all *_shared_modules in the path and store them in list_modules
+path=salome_path.split(":")
 for rep in path:
-   # Add rep directory in the Python path to be able to import modules 
-   sys.path[:0]=[rep]
-   listdir=glob.glob(os.path.join(rep,"*__POA"))
-   for elem in listdir:
-      if os.path.isdir(elem):
-         # Found a directory (Python package) named *__POA 
-         module__POA=os.path.basename(elem)
-         module=module__POA[:-5]
-         MESSAGE( "Import CORBA module: " + module + ".\n Directory: " + os.path.abspath(elem)[:-5] )
-         mod=__import__(module)
-         # force the reload of CORBA module to resolve all the include relations between modules
-         # specific of omniORBpy implementation (1.5)
-         reload(mod)
-         modules[module]=mod.__dict__.copy()
-   # Now we import modules found in shared_modules directory
-   r=os.path.join(rep,"shared_modules")
-   MESSAGE( r )
-   if os.path.isdir(r):
-      sys.path[:0]=[r]
-      listfich=glob.glob(os.path.join(r,"*.py"))
-      MESSAGE( str(listfich) )
-      for m in listfich:
-         module=os.path.basename(m)[:-3]
-         MESSAGE( "Import module: " + module + ".\n Location: " + os.path.abspath(m) )
-         mod=__import__(module)
-         modules[module]=mod.__dict__.copy()
-      # Don't keep r directory in the path to not pollute it
-      del sys.path[0]
+    # Import all *_shared_modules in rep
+    for f in glob.glob(os.path.join(rep,"lib","python"+sys.version[:3],"site-packages","salome","shared_modules","*_shared_modules.py")):
+        try:
+           m=__import__(os.path.splitext(os.path.basename(f))[0])
+           list_modules.append(m)
+        except:
+           pass
 
-   # Don't keep rep directory in the path to not pollute it
-   del sys.path[0]
-
-# End of CORBA modules import
-
-def import_shared_modules(sysmodules):
-   """
-      This function "imports" shared modules contained in modules dictionnary
-      in sysmodules.
-      All these modules are only copied and not completely imported (not executed)
-   """
-   # EDF-CCAR: 
-   # Problem with omniORB : omniORB creates a C Python module named  _omnipy
-   # this module has sub-modules : omni_func, ...
-   # _omnipy is quite a package but import with Python sub-interpreters does not seem to work 
-   # To make it work we need to add those sub-modules in sysmodules
-   import _omnipy
-   sysmodules["_omnipy.omni_func"]=_omnipy.omni_func
-   sysmodules["_omnipy.poa_func"]=_omnipy.poa_func
-   sysmodules["_omnipy.poamanager_func"]=_omnipy.poa_func
-   sysmodules["_omnipy.orb_func"]=_omnipy.orb_func
-
-   import imp
-
-   # All modules in the modules dictionnary are only copied, not completely imported 
-   for nom_module,module_dict in modules.items():
-      if sysmodules.has_key(nom_module):continue
-      m=imp.new_module(nom_module)
-      m.__dict__.update(module_dict)
-      sysmodules[nom_module]=m
+# 
+# If shared modules have been imported before installing import mechanism
+# we add them to shared_imported
+#
+for name,module in sys.modules.items():
+    if import_hook.is_shared(name) and shared_imported.get(name) is None:
+       #print "Module shared added to shared_imported: ",name
+       shared_imported[name]=module