From c0a955e9735c6fef01645d88c015bb365609ad7b Mon Sep 17 00:00:00 2001 From: prascle Date: Fri, 7 Jul 2006 10:27:17 +0000 Subject: [PATCH] PR: Corrections from C. Caremoli, for embedded python. No Driver attribute for SALOMEDS__POA. allow: from omniORB import any. --- src/KERNEL_PY/import_hook.py | 84 +++++++++++++++++++++----- src/KERNEL_PY/kernel_shared_modules.py | 29 ++------- src/KERNEL_PY/omnipatch.py | 17 +++++- src/KERNEL_PY/salome_shared_modules.py | 14 +++-- 4 files changed, 97 insertions(+), 47 deletions(-) diff --git a/src/KERNEL_PY/import_hook.py b/src/KERNEL_PY/import_hook.py index 42b23daa2..904e3d441 100755 --- a/src/KERNEL_PY/import_hook.py +++ b/src/KERNEL_PY/import_hook.py @@ -70,38 +70,90 @@ def register_pattern(pattern): patterns.append(pattern) def is_shared(name): + """ Indicate if module name is a shared module + among multiple interpreters (return value=1) + """ if shared_imported.has_key(name):return 1 for pattern in patterns: if pattern(name) : return 1 return 0 -def get_shared_imported(name): - return shared_imported.get(name) +def get_shared_imported(name,fromlist): + """ If the module is registered in shared_imported + update the sys.modules dict + Let the real import be done by original_import + """ + module= shared_imported.get(name) + if module is None : + #module name is not shared or not already imported + #let original_import do the job + return None + + # module is already imported and shared. Put it in sys.modules and + # let original_import finish the job + sys.modules[name]=module + +def get_real_module(mod,name): + """Return effective module on import + Standard import returns module A on import A.B + To get module A.B use get_real_module with name "A.B" + """ + components = name.split('.') + for comp in components[1:]: + mod = getattr(mod, comp) + return mod def set_shared_imported(name,module): + """ Register a shared module + Name can be a dotted name : package + """ shared_imported[name]=module #print "Module %s shared registered" % name,module -def get_shared_imported_with_copy(name): - module_dict= shared_imported.get(name) - m=imp.new_module(name) - m.__dict__.update(module_dict) - return m -def set_shared_imported_with_copy(name,module): - shared_imported[name]=module.__dict__.copy() - #print "Module %s shared registered" % name +def ensure_fromlist(m, fromlist, recursive=0): + """ Return the real modules list to be imported + """ + l=[] + for sub in fromlist: + if sub == "*": + if not recursive: + try: + all = m.__all__ + except AttributeError: + pass + else: + l.extend(ensure_fromlist(m, all, 1)) + else: + submod=getattr(m,sub) + if type(submod) == type(sys): + l.append(("%s.%s" % (m.__name__, sub),submod)) + return l def import_hook(name, globals=None, locals=None, fromlist=None): + """ Import replacement for sharing modules among multiple interpreters + Mostly update sys.modules before doing real import + """ #print "import_hook",name,fromlist - module=get_shared_imported(name) - if module: - sys.modules[name]=module - return module + m=get_shared_imported(name,fromlist) module= original_import(name, globals, locals, fromlist) - if is_shared(name): - set_shared_imported(name,module) + if fromlist: + #when fromlist is specified, module is the real module + #fromlist is a list of possibly dotted name + m=module + for nam,mod in ensure_fromlist(m, fromlist): + if is_shared(nam): + set_shared_imported(nam,mod) + else: + #when fromlist is not specified and name is a dotted name, + # module is the root package not the real module + #so we need to retrieve it + m=get_real_module(module,name) + + if type(m) == type(sys) and is_shared(m.__name__): + set_shared_imported(m.__name__,m) + return module original_reload=__builtin__.reload diff --git a/src/KERNEL_PY/kernel_shared_modules.py b/src/KERNEL_PY/kernel_shared_modules.py index c06cba1b6..bcf92d92b 100755 --- a/src/KERNEL_PY/kernel_shared_modules.py +++ b/src/KERNEL_PY/kernel_shared_modules.py @@ -31,36 +31,19 @@ register_name("libSALOME_LifeCycleCORBA") register_pattern(lambda(x):x.endswith("_idl")) register_pattern(lambda(x):x.endswith("_Swig")) +register_name("omniORB") register_name("CORBA") -from omniORB import CORBA +register_name("CosNaming") +register_name("CosNaming__POA") +register_name("omnipatch") -register_name("omniORB") import omniORB - -register_name("CosNaming") +from omniORB import CORBA import CosNaming - -register_name("omnipatch") +import CosNaming__POA import omnipatch -import Engines -import SALOME -import SALOMEDS -import SALOME_ModuleCatalog - def init_shared_modules(): """ This function initializes shared modules that need to be """ - # 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 sys.modules - import sys - import _omnipy - sys.modules["_omnipy.omni_func"]=_omnipy.omni_func - sys.modules["_omnipy.poa_func"]=_omnipy.poa_func - sys.modules["_omnipy.poamanager_func"]=_omnipy.poamanager_func - sys.modules["_omnipy.orb_func"]=_omnipy.orb_func - diff --git a/src/KERNEL_PY/omnipatch.py b/src/KERNEL_PY/omnipatch.py index 1f4f8d9c4..2ebaf5429 100644 --- a/src/KERNEL_PY/omnipatch.py +++ b/src/KERNEL_PY/omnipatch.py @@ -42,6 +42,9 @@ def openModule(mname, fname=None): if mname == "CORBA": mod = sys.modules["omniORB.CORBA"] + # Salome modification start + shared_imported[mname]=mod + # Salome modification end elif sys.modules.has_key(mname): mod = sys.modules[mname] @@ -50,6 +53,9 @@ def openModule(mname, fname=None): pmod = _partialModules[mname] mod.__dict__.update(pmod.__dict__) del _partialModules[mname] + # Salome modification start + shared_imported[mname]=mod + # Salome modification end elif _partialModules.has_key(mname): mod = _partialModules[mname] @@ -57,9 +63,6 @@ def openModule(mname, fname=None): else: mod = newModule(mname) - # Salome modification start - shared_imported[mname]=mod - # Salome modification end if not hasattr(mod, "__doc__") or mod.__doc__ is None: mod.__doc__ = "omniORB IDL module " + mname + "\n\n" + \ @@ -103,12 +106,20 @@ def newModule(mname): # Function to update a module with the partial module store in the # partial module map def updateModule(mname): + # Salome modification start + # Be sure to use the right module dictionnary + import sys + # Salome modification end if _partialModules.has_key(mname): pmod = _partialModules[mname] mod = sys.modules[mname] mod.__dict__.update(pmod.__dict__) del _partialModules[mname] + # Salome modification start + shared_imported[mname]=sys.modules[mname] + # Salome modification end + omniORB.updateModule=updateModule omniORB.newModule=newModule omniORB.openModule=openModule diff --git a/src/KERNEL_PY/salome_shared_modules.py b/src/KERNEL_PY/salome_shared_modules.py index bb3c2f20a..9cd3abcb0 100755 --- a/src/KERNEL_PY/salome_shared_modules.py +++ b/src/KERNEL_PY/salome_shared_modules.py @@ -22,8 +22,6 @@ # File : salome_shared_modules.py # Module : SALOME -from SALOME_utilities import * - """ This module with help of import_hook and *_shared_modules filters imports when using the embedded Python interpretor. @@ -56,13 +54,19 @@ import glob,os,sys import import_hook # shared_imported, patterns, register_name, register_pattern # will be shared by all Python sub interpretors -from omnipatch import shared_imported -import_hook.shared_imported=shared_imported from import_hook import patterns from import_hook import register_name from import_hook import register_pattern register_name("salome_shared_modules") +register_name("omniORB") +register_name("omnipatch") +register_pattern(lambda(x):x.endswith("_idl")) +register_pattern(lambda(x):x.startswith("omniORB.")) + +from omnipatch import shared_imported +shared_imported.update(import_hook.shared_imported) +import_hook.shared_imported=shared_imported # 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")) @@ -87,7 +91,7 @@ for rep in path: # 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: + if module and import_hook.is_shared(name) and not shared_imported.has_key(name): #print "Module shared added to shared_imported: ",name shared_imported[name]=module -- 2.39.2