1 # Copyright (C) 2005 OPEN CASCADE, CEA, EDF R&D, LEG
2 # PRINCIPIA R&D, EADS CCR, Lip6, BV, CEDRAT
3 # This library is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU Lesser General Public
5 # License as published by the Free Software Foundation; either
6 # version 2.1 of the License.
8 # This library is distributed in the hope that it will be useful
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 # Lesser General Public License for more details.
13 # You should have received a copy of the GNU Lesser General Public
14 # License along with this library; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 This module replaces the standard import mechanism with one
21 that filters some imports that can't be done more than once.
23 This is related to the multi study feature that is implemented
24 by using the Python multi interpreter feature.
25 Some modules register objects or classes by calling modules
26 implemented in C. These operations can't be done multiple times.
27 So it's very important to control these imports.
30 - PyQt : import qt calls a C module to register classes
31 - OmniORB : import *_idl calls a C module to register CORBA interfaces
34 - First import the module : import import_hook. This module will
35 replace the original importer mechanism
37 - Next register the module names or pattern names to filter out::
38 import_hook.register_name("a")
39 import_hook.register_pattern(pattern)
41 where pattern is a function with one parameter, the module name
42 to be imported, that returns true or false depending if this module is
43 to be filtered or not.
47 IMPORTANT : Every subinterpretor has its own import_hook module. import_hook is not shared among subinterpretors.
48 The mechanism only works if shared_imported and pattern are shared between all subinterpretors.
49 This is done by calling init_shared_modules().
52 import sys, imp, __builtin__
54 # Keep in shared_imported a copy of dictionnary modules
55 # that need to be imported only once in multi-study context
58 # patterns contains functions that returns 1 or 0 depending if
59 # the module name (argument) must be filtered out or not
60 # These functions are added by calling register_pattern
63 original_import=__builtin__.__import__
65 def register_name(name):
66 if shared_imported.has_key(name):return
67 shared_imported[name]=None
69 def register_pattern(pattern):
70 patterns.append(pattern)
73 if shared_imported.has_key(name):return 1
74 for pattern in patterns:
75 if pattern(name) : return 1
78 def get_shared_imported(name):
79 return shared_imported.get(name)
81 def set_shared_imported(name,module):
82 shared_imported[name]=module
83 #print "Module %s shared registered" % name,module
85 def get_shared_imported_with_copy(name):
86 module_dict= shared_imported.get(name)
87 m=imp.new_module(name)
88 m.__dict__.update(module_dict)
90 def set_shared_imported_with_copy(name,module):
91 shared_imported[name]=module.__dict__.copy()
92 #print "Module %s shared registered" % name
94 def import_hook(name, globals=None, locals=None, fromlist=None):
95 #print "import_hook",name,fromlist
96 module=get_shared_imported(name)
98 sys.modules[name]=module
101 module= original_import(name, globals, locals, fromlist)
104 set_shared_imported(name,module)
107 original_reload=__builtin__.reload
109 def reload_hook(module):
110 if is_shared(module.__name__):
112 return original_reload(module)
114 __builtin__.__import__=import_hook
115 # Reload is not replaced
116 #__builtin__.reload=reload_hook
118 def init_shared_modules(shared_module):
119 global shared_imported, patterns
120 shared_imported=shared_module.shared_imported
121 patterns= shared_module.patterns
122 for k,v in shared_imported.items():
123 if v is not None:sys.modules[k]=v
124 shared_imported["salome_shared_modules"]=shared_module
125 import salome_shared_modules
126 for m in salome_shared_modules.list_modules:
127 m.init_shared_modules()