Mercurial > cpython-lru_cache-weakref
annotate Lib/ihooks.py @ 54294:dba767bc3155 legacy-trunk
remove mention of old ctypes version
| author | Benjamin Peterson <benjamin@python.org> |
|---|---|
| date | Sat, 23 May 2009 20:59:09 +0000 |
| parents | f2a63ec5055e |
| children | 50b7f56814f9 |
| rev | line source |
|---|---|
| 2887 | 1 """Import hook support. |
| 2 | |
| 3 Consistent use of this module will make it possible to change the | |
| 4 different mechanisms involved in loading modules independently. | |
| 5 | |
| 6 While the built-in module imp exports interfaces to the built-in | |
| 7 module searching and loading algorithm, and it is possible to replace | |
| 8 the built-in function __import__ in order to change the semantics of | |
| 9 the import statement, until now it has been difficult to combine the | |
| 10 effect of different __import__ hacks, like loading modules from URLs | |
|
8586
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
11 by rimport.py, or restricted execution by rexec.py. |
| 2887 | 12 |
| 13 This module defines three new concepts: | |
| 14 | |
|
8586
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
15 1) A "file system hooks" class provides an interface to a filesystem. |
| 2887 | 16 |
| 17 One hooks class is defined (Hooks), which uses the interface provided | |
| 18 by standard modules os and os.path. It should be used as the base | |
| 19 class for other hooks classes. | |
| 20 | |
| 30723 | 21 2) A "module loader" class provides an interface to search for a |
| 2887 | 22 module in a search path and to load it. It defines a method which |
| 23 searches for a module in a single directory; by overriding this method | |
| 24 one can redefine the details of the search. If the directory is None, | |
| 25 built-in and frozen modules are searched instead. | |
| 26 | |
| 27 Two module loader class are defined, both implementing the search | |
| 28 strategy used by the built-in __import__ function: ModuleLoader uses | |
| 29 the imp module's find_module interface, while HookableModuleLoader | |
| 30 uses a file system hooks class to interact with the file system. Both | |
| 31 use the imp module's load_* interfaces to actually load the module. | |
| 32 | |
|
8586
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
33 3) A "module importer" class provides an interface to import a |
| 2887 | 34 module, as well as interfaces to reload and unload a module. It also |
| 35 provides interfaces to install and uninstall itself instead of the | |
| 36 default __import__ and reload (and unload) functions. | |
| 37 | |
| 38 One module importer class is defined (ModuleImporter), which uses a | |
| 39 module loader instance passed in (by default HookableModuleLoader is | |
| 40 instantiated). | |
| 41 | |
| 42 The classes defined here should be used as base classes for extended | |
| 43 functionality along those lines. | |
| 44 | |
|
16160
46920b60cd33
added a few more __all__ lists
Skip Montanaro <skip@pobox.com>
parents:
15868
diff
changeset
|
45 If a module importer class supports dotted names, its import_module() |
| 2887 | 46 must return a different value depending on whether it is called on |
| 47 behalf of a "from ... import ..." statement or not. (This is caused | |
| 48 by the way the __import__ hook is used by the Python interpreter.) It | |
| 49 would also do wise to install a different version of reload(). | |
| 50 | |
| 51 """ | |
|
46849
f2a63ec5055e
Deprecate ihooks for removal in 3.0.
Brett Cannon <bcannon@gmail.com>
parents:
32946
diff
changeset
|
52 from warnings import warnpy3k |
|
f2a63ec5055e
Deprecate ihooks for removal in 3.0.
Brett Cannon <bcannon@gmail.com>
parents:
32946
diff
changeset
|
53 warnpy3k("the ihooks module has been removed in Python 3.0", stacklevel=2) |
|
f2a63ec5055e
Deprecate ihooks for removal in 3.0.
Brett Cannon <bcannon@gmail.com>
parents:
32946
diff
changeset
|
54 del warnpy3k |
| 2887 | 55 |
| 56 import __builtin__ | |
| 57 import imp | |
| 58 import os | |
| 59 import sys | |
|
8586
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
60 |
|
16160
46920b60cd33
added a few more __all__ lists
Skip Montanaro <skip@pobox.com>
parents:
15868
diff
changeset
|
61 __all__ = ["BasicModuleLoader","Hooks","ModuleLoader","FancyModuleLoader", |
|
46920b60cd33
added a few more __all__ lists
Skip Montanaro <skip@pobox.com>
parents:
15868
diff
changeset
|
62 "BasicModuleImporter","ModuleImporter","install","uninstall"] |
|
8586
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
63 |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
64 VERBOSE = 0 |
| 2887 | 65 |
| 66 | |
| 67 from imp import C_EXTENSION, PY_SOURCE, PY_COMPILED | |
|
8586
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
68 from imp import C_BUILTIN, PY_FROZEN, PKG_DIRECTORY |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
69 BUILTIN_MODULE = C_BUILTIN |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
70 FROZEN_MODULE = PY_FROZEN |
| 2887 | 71 |
| 72 | |
| 73 class _Verbose: | |
| 74 | |
|
8586
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
75 def __init__(self, verbose = VERBOSE): |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
76 self.verbose = verbose |
| 2887 | 77 |
| 78 def get_verbose(self): | |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
79 return self.verbose |
| 2887 | 80 |
| 81 def set_verbose(self, verbose): | |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
82 self.verbose = verbose |
| 2887 | 83 |
| 84 # XXX The following is an experimental interface | |
| 85 | |
| 86 def note(self, *args): | |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
87 if self.verbose: |
|
28089
14f65d661084
Get rid of many apply() calls.
Guido van Rossum <guido@python.org>
parents:
26668
diff
changeset
|
88 self.message(*args) |
| 2887 | 89 |
| 90 def message(self, format, *args): | |
|
8586
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
91 if args: |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
92 print format%args |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
93 else: |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
94 print format |
| 2887 | 95 |
| 96 | |
| 97 class BasicModuleLoader(_Verbose): | |
| 98 | |
| 99 """Basic module loader. | |
| 100 | |
| 101 This provides the same functionality as built-in import. It | |
| 102 doesn't deal with checking sys.modules -- all it provides is | |
| 103 find_module() and a load_module(), as well as find_module_in_dir() | |
| 104 which searches just one directory, and can be overridden by a | |
| 105 derived class to change the module search algorithm when the basic | |
| 106 dependency on sys.path is unchanged. | |
| 107 | |
| 108 The interface is a little more convenient than imp's: | |
| 109 find_module(name, [path]) returns None or 'stuff', and | |
| 110 load_module(name, stuff) loads the module. | |
| 111 | |
| 112 """ | |
| 113 | |
| 114 def find_module(self, name, path = None): | |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
12392
diff
changeset
|
115 if path is None: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
116 path = [None] + self.default_path() |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
117 for dir in path: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
118 stuff = self.find_module_in_dir(name, dir) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
119 if stuff: return stuff |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
120 return None |
| 2887 | 121 |
| 122 def default_path(self): | |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
123 return sys.path |
| 2887 | 124 |
| 125 def find_module_in_dir(self, name, dir): | |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
126 if dir is None: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
127 return self.find_builtin_module(name) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
128 else: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
129 try: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
130 return imp.find_module(name, [dir]) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
131 except ImportError: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
132 return None |
| 2887 | 133 |
| 134 def find_builtin_module(self, name): | |
|
8586
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
135 # XXX frozen packages? |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
136 if imp.is_builtin(name): |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
137 return None, '', ('', '', BUILTIN_MODULE) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
138 if imp.is_frozen(name): |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
139 return None, '', ('', '', FROZEN_MODULE) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
140 return None |
| 2887 | 141 |
| 142 def load_module(self, name, stuff): | |
|
8586
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
143 file, filename, info = stuff |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
144 try: |
|
8586
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
145 return imp.load_module(name, file, filename, info) |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
146 finally: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
147 if file: file.close() |
| 2887 | 148 |
| 149 | |
| 150 class Hooks(_Verbose): | |
| 151 | |
| 152 """Hooks into the filesystem and interpreter. | |
| 153 | |
| 154 By deriving a subclass you can redefine your filesystem interface, | |
| 155 e.g. to merge it with the URL space. | |
| 156 | |
| 157 This base class behaves just like the native filesystem. | |
| 158 | |
| 159 """ | |
| 160 | |
| 161 # imp interface | |
| 162 def get_suffixes(self): return imp.get_suffixes() | |
| 163 def new_module(self, name): return imp.new_module(name) | |
| 164 def is_builtin(self, name): return imp.is_builtin(name) | |
| 165 def init_builtin(self, name): return imp.init_builtin(name) | |
| 166 def is_frozen(self, name): return imp.is_frozen(name) | |
| 167 def init_frozen(self, name): return imp.init_frozen(name) | |
| 168 def get_frozen_object(self, name): return imp.get_frozen_object(name) | |
| 169 def load_source(self, name, filename, file=None): | |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
170 return imp.load_source(name, filename, file) |
| 2887 | 171 def load_compiled(self, name, filename, file=None): |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
172 return imp.load_compiled(name, filename, file) |
| 2887 | 173 def load_dynamic(self, name, filename, file=None): |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
174 return imp.load_dynamic(name, filename, file) |
|
8586
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
175 def load_package(self, name, filename, file=None): |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
176 return imp.load_module(name, file, filename, ("", "", PKG_DIRECTORY)) |
| 2887 | 177 |
| 178 def add_module(self, name): | |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
179 d = self.modules_dict() |
|
23620
a1de0bab5475
SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents:
16508
diff
changeset
|
180 if name in d: return d[name] |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
181 d[name] = m = self.new_module(name) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
182 return m |
| 2887 | 183 |
| 184 # sys interface | |
| 185 def modules_dict(self): return sys.modules | |
| 186 def default_path(self): return sys.path | |
| 187 | |
| 188 def path_split(self, x): return os.path.split(x) | |
| 189 def path_join(self, x, y): return os.path.join(x, y) | |
| 190 def path_isabs(self, x): return os.path.isabs(x) | |
| 191 # etc. | |
| 192 | |
| 193 def path_exists(self, x): return os.path.exists(x) | |
| 194 def path_isdir(self, x): return os.path.isdir(x) | |
| 195 def path_isfile(self, x): return os.path.isfile(x) | |
| 196 def path_islink(self, x): return os.path.islink(x) | |
| 197 # etc. | |
| 198 | |
|
28089
14f65d661084
Get rid of many apply() calls.
Guido van Rossum <guido@python.org>
parents:
26668
diff
changeset
|
199 def openfile(self, *x): return open(*x) |
| 2887 | 200 openfile_error = IOError |
| 201 def listdir(self, x): return os.listdir(x) | |
| 202 listdir_error = os.error | |
| 203 # etc. | |
| 204 | |
| 205 | |
| 206 class ModuleLoader(BasicModuleLoader): | |
| 207 | |
| 208 """Default module loader; uses file system hooks. | |
| 209 | |
| 210 By defining suitable hooks, you might be able to load modules from | |
| 211 other sources than the file system, e.g. from compressed or | |
| 212 encrypted files, tar files or (if you're brave!) URLs. | |
| 213 | |
| 214 """ | |
| 215 | |
|
8586
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
216 def __init__(self, hooks = None, verbose = VERBOSE): |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
217 BasicModuleLoader.__init__(self, verbose) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
218 self.hooks = hooks or Hooks(verbose) |
| 2887 | 219 |
| 220 def default_path(self): | |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
221 return self.hooks.default_path() |
| 2887 | 222 |
| 223 def modules_dict(self): | |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
224 return self.hooks.modules_dict() |
| 2887 | 225 |
| 226 def get_hooks(self): | |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
227 return self.hooks |
| 2887 | 228 |
| 229 def set_hooks(self, hooks): | |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
230 self.hooks = hooks |
| 2887 | 231 |
| 232 def find_builtin_module(self, name): | |
|
8586
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
233 # XXX frozen packages? |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
234 if self.hooks.is_builtin(name): |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
235 return None, '', ('', '', BUILTIN_MODULE) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
236 if self.hooks.is_frozen(name): |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
237 return None, '', ('', '', FROZEN_MODULE) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
238 return None |
| 2887 | 239 |
|
8586
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
240 def find_module_in_dir(self, name, dir, allow_packages=1): |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
241 if dir is None: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
242 return self.find_builtin_module(name) |
|
8586
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
243 if allow_packages: |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
244 fullname = self.hooks.path_join(dir, name) |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
245 if self.hooks.path_isdir(fullname): |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
246 stuff = self.find_module_in_dir("__init__", fullname, 0) |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
247 if stuff: |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
248 file = stuff[0] |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
249 if file: file.close() |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
250 return None, fullname, ('', '', PKG_DIRECTORY) |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
251 for info in self.hooks.get_suffixes(): |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
252 suff, mode, type = info |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
253 fullname = self.hooks.path_join(dir, name+suff) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
254 try: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
255 fp = self.hooks.openfile(fullname, mode) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
256 return fp, fullname, info |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
257 except self.hooks.openfile_error: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
258 pass |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
259 return None |
| 2887 | 260 |
| 261 def load_module(self, name, stuff): | |
|
8586
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
262 file, filename, info = stuff |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
263 (suff, mode, type) = info |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
264 try: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
265 if type == BUILTIN_MODULE: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
266 return self.hooks.init_builtin(name) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
267 if type == FROZEN_MODULE: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
268 return self.hooks.init_frozen(name) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
269 if type == C_EXTENSION: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
270 m = self.hooks.load_dynamic(name, filename, file) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
271 elif type == PY_SOURCE: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
272 m = self.hooks.load_source(name, filename, file) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
273 elif type == PY_COMPILED: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
274 m = self.hooks.load_compiled(name, filename, file) |
|
8586
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
275 elif type == PKG_DIRECTORY: |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
276 m = self.hooks.load_package(name, filename, file) |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
277 else: |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
30723
diff
changeset
|
278 raise ImportError, "Unrecognized module type (%r) for %s" % \ |
|
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
30723
diff
changeset
|
279 (type, name) |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
280 finally: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
281 if file: file.close() |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
282 m.__file__ = filename |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
283 return m |
| 2887 | 284 |
| 285 | |
| 286 class FancyModuleLoader(ModuleLoader): | |
| 287 | |
| 288 """Fancy module loader -- parses and execs the code itself.""" | |
| 289 | |
| 290 def load_module(self, name, stuff): | |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
291 file, filename, (suff, mode, type) = stuff |
|
8586
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
292 realfilename = filename |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
293 path = None |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
294 |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
295 if type == PKG_DIRECTORY: |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
296 initstuff = self.find_module_in_dir("__init__", filename, 0) |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
297 if not initstuff: |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
298 raise ImportError, "No __init__ module in package %s" % name |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
299 initfile, initfilename, initinfo = initstuff |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
300 initsuff, initmode, inittype = initinfo |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
301 if inittype not in (PY_COMPILED, PY_SOURCE): |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
302 if initfile: initfile.close() |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
303 raise ImportError, \ |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
30723
diff
changeset
|
304 "Bad type (%r) for __init__ module in package %s" % ( |
|
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
30723
diff
changeset
|
305 inittype, name) |
|
8586
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
306 path = [filename] |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
307 file = initfile |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
308 realfilename = initfilename |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
309 type = inittype |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
310 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
311 if type == FROZEN_MODULE: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
312 code = self.hooks.get_frozen_object(name) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
313 elif type == PY_COMPILED: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
314 import marshal |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
315 file.seek(8) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
316 code = marshal.load(file) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
317 elif type == PY_SOURCE: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
318 data = file.read() |
|
8586
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
319 code = compile(data, realfilename, 'exec') |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
320 else: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
321 return ModuleLoader.load_module(self, name, stuff) |
|
8586
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
322 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
323 m = self.hooks.add_module(name) |
|
8586
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
324 if path: |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
325 m.__path__ = path |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
326 m.__file__ = filename |
|
32946
db38c1e83965
ihooks FancyModuleLoader.load_module()
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
327 try: |
|
db38c1e83965
ihooks FancyModuleLoader.load_module()
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
328 exec code in m.__dict__ |
|
db38c1e83965
ihooks FancyModuleLoader.load_module()
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
329 except: |
|
db38c1e83965
ihooks FancyModuleLoader.load_module()
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
330 d = self.hooks.modules_dict() |
|
db38c1e83965
ihooks FancyModuleLoader.load_module()
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
331 if name in d: |
|
db38c1e83965
ihooks FancyModuleLoader.load_module()
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
332 del d[name] |
|
db38c1e83965
ihooks FancyModuleLoader.load_module()
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
333 raise |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
334 return m |
| 2887 | 335 |
| 336 | |
|
8586
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
337 class BasicModuleImporter(_Verbose): |
| 2887 | 338 |
|
8586
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
339 """Basic module importer; uses module loader. |
| 2887 | 340 |
|
8586
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
341 This provides basic import facilities but no package imports. |
| 2887 | 342 |
| 343 """ | |
| 344 | |
|
8586
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
345 def __init__(self, loader = None, verbose = VERBOSE): |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
346 _Verbose.__init__(self, verbose) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
347 self.loader = loader or ModuleLoader(None, verbose) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
348 self.modules = self.loader.modules_dict() |
| 2887 | 349 |
| 350 def get_loader(self): | |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
351 return self.loader |
| 2887 | 352 |
| 353 def set_loader(self, loader): | |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
354 self.loader = loader |
| 2887 | 355 |
| 356 def get_hooks(self): | |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
357 return self.loader.get_hooks() |
| 2887 | 358 |
| 359 def set_hooks(self, hooks): | |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
360 return self.loader.set_hooks(hooks) |
| 2887 | 361 |
| 362 def import_module(self, name, globals={}, locals={}, fromlist=[]): | |
| 26668 | 363 name = str(name) |
|
23620
a1de0bab5475
SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents:
16508
diff
changeset
|
364 if name in self.modules: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
365 return self.modules[name] # Fast path |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
366 stuff = self.loader.find_module(name) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
367 if not stuff: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
368 raise ImportError, "No module named %s" % name |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
369 return self.loader.load_module(name, stuff) |
| 2887 | 370 |
| 371 def reload(self, module, path = None): | |
| 26668 | 372 name = str(module.__name__) |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
373 stuff = self.loader.find_module(name, path) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
374 if not stuff: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
375 raise ImportError, "Module %s not found for reload" % name |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
376 return self.loader.load_module(name, stuff) |
| 2887 | 377 |
| 378 def unload(self, module): | |
| 26668 | 379 del self.modules[str(module.__name__)] |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
380 # XXX Should this try to clear the module's namespace? |
| 2887 | 381 |
| 382 def install(self): | |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
383 self.save_import_module = __builtin__.__import__ |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
384 self.save_reload = __builtin__.reload |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
385 if not hasattr(__builtin__, 'unload'): |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
386 __builtin__.unload = None |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
387 self.save_unload = __builtin__.unload |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
388 __builtin__.__import__ = self.import_module |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
389 __builtin__.reload = self.reload |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
390 __builtin__.unload = self.unload |
| 2887 | 391 |
| 392 def uninstall(self): | |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
393 __builtin__.__import__ = self.save_import_module |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
394 __builtin__.reload = self.save_reload |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
395 __builtin__.unload = self.save_unload |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
396 if not __builtin__.unload: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5995
diff
changeset
|
397 del __builtin__.unload |
| 2887 | 398 |
| 399 | |
|
8586
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
400 class ModuleImporter(BasicModuleImporter): |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
401 |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
402 """A module importer that supports packages.""" |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
12392
diff
changeset
|
403 |
|
8586
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
404 def import_module(self, name, globals=None, locals=None, fromlist=None): |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
405 parent = self.determine_parent(globals) |
| 26668 | 406 q, tail = self.find_head_package(parent, str(name)) |
|
8586
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
407 m = self.load_tail(q, tail) |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
408 if not fromlist: |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
409 return q |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
410 if hasattr(m, "__path__"): |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
411 self.ensure_fromlist(m, fromlist) |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
412 return m |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
413 |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
414 def determine_parent(self, globals): |
|
23620
a1de0bab5475
SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents:
16508
diff
changeset
|
415 if not globals or not "__name__" in globals: |
|
8586
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
416 return None |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
417 pname = globals['__name__'] |
|
23620
a1de0bab5475
SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents:
16508
diff
changeset
|
418 if "__path__" in globals: |
|
8586
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
419 parent = self.modules[pname] |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
420 assert globals is parent.__dict__ |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
421 return parent |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
422 if '.' in pname: |
|
16508
5c3bf2e89d80
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
16160
diff
changeset
|
423 i = pname.rfind('.') |
|
8586
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
424 pname = pname[:i] |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
425 parent = self.modules[pname] |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
426 assert parent.__name__ == pname |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
427 return parent |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
428 return None |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
429 |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
430 def find_head_package(self, parent, name): |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
431 if '.' in name: |
|
16508
5c3bf2e89d80
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
16160
diff
changeset
|
432 i = name.find('.') |
|
8586
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
433 head = name[:i] |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
434 tail = name[i+1:] |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
435 else: |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
436 head = name |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
437 tail = "" |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
438 if parent: |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
439 qname = "%s.%s" % (parent.__name__, head) |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
440 else: |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
441 qname = head |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
442 q = self.import_it(head, qname, parent) |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
443 if q: return q, tail |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
444 if parent: |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
445 qname = head |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
446 parent = None |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
447 q = self.import_it(head, qname, parent) |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
448 if q: return q, tail |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
449 raise ImportError, "No module named " + qname |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
450 |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
451 def load_tail(self, q, tail): |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
452 m = q |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
453 while tail: |
|
16508
5c3bf2e89d80
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
16160
diff
changeset
|
454 i = tail.find('.') |
|
8586
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
455 if i < 0: i = len(tail) |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
456 head, tail = tail[:i], tail[i+1:] |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
457 mname = "%s.%s" % (m.__name__, head) |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
458 m = self.import_it(head, mname, m) |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
459 if not m: |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
460 raise ImportError, "No module named " + mname |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
461 return m |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
462 |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
463 def ensure_fromlist(self, m, fromlist, recursive=0): |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
464 for sub in fromlist: |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
465 if sub == "*": |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
466 if not recursive: |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
467 try: |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
468 all = m.__all__ |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
469 except AttributeError: |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
470 pass |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
471 else: |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
472 self.ensure_fromlist(m, all, 1) |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
473 continue |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
474 if sub != "*" and not hasattr(m, sub): |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
475 subname = "%s.%s" % (m.__name__, sub) |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
476 submod = self.import_it(sub, subname, m) |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
477 if not submod: |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
478 raise ImportError, "No module named " + subname |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
479 |
|
12392
8d7e07737b4c
Thomas Heller <thomas.heller@ion-tof.com>:
Fred Drake <fdrake@acm.org>
parents:
8586
diff
changeset
|
480 def import_it(self, partname, fqname, parent, force_load=0): |
|
8586
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
481 if not partname: |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
482 raise ValueError, "Empty module name" |
|
12392
8d7e07737b4c
Thomas Heller <thomas.heller@ion-tof.com>:
Fred Drake <fdrake@acm.org>
parents:
8586
diff
changeset
|
483 if not force_load: |
|
8d7e07737b4c
Thomas Heller <thomas.heller@ion-tof.com>:
Fred Drake <fdrake@acm.org>
parents:
8586
diff
changeset
|
484 try: |
|
8d7e07737b4c
Thomas Heller <thomas.heller@ion-tof.com>:
Fred Drake <fdrake@acm.org>
parents:
8586
diff
changeset
|
485 return self.modules[fqname] |
|
8d7e07737b4c
Thomas Heller <thomas.heller@ion-tof.com>:
Fred Drake <fdrake@acm.org>
parents:
8586
diff
changeset
|
486 except KeyError: |
|
8d7e07737b4c
Thomas Heller <thomas.heller@ion-tof.com>:
Fred Drake <fdrake@acm.org>
parents:
8586
diff
changeset
|
487 pass |
|
8586
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
488 try: |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
489 path = parent and parent.__path__ |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
490 except AttributeError: |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
491 return None |
| 26668 | 492 partname = str(partname) |
|
8586
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
493 stuff = self.loader.find_module(partname, path) |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
494 if not stuff: |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
495 return None |
| 26668 | 496 fqname = str(fqname) |
|
8586
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
497 m = self.loader.load_module(fqname, stuff) |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
498 if parent: |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
499 setattr(parent, partname, m) |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
500 return m |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
501 |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
502 def reload(self, module): |
| 26668 | 503 name = str(module.__name__) |
|
8586
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
504 if '.' not in name: |
|
12392
8d7e07737b4c
Thomas Heller <thomas.heller@ion-tof.com>:
Fred Drake <fdrake@acm.org>
parents:
8586
diff
changeset
|
505 return self.import_it(name, name, None, force_load=1) |
|
16508
5c3bf2e89d80
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
16160
diff
changeset
|
506 i = name.rfind('.') |
|
8586
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
507 pname = name[:i] |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
508 parent = self.modules[pname] |
|
12392
8d7e07737b4c
Thomas Heller <thomas.heller@ion-tof.com>:
Fred Drake <fdrake@acm.org>
parents:
8586
diff
changeset
|
509 return self.import_it(name[i+1:], name, parent, force_load=1) |
|
8586
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
510 |
|
bdfef8e73457
Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
511 |
| 2887 | 512 default_importer = None |
| 513 current_importer = None | |
| 514 | |
| 515 def install(importer = None): | |
| 516 global current_importer | |
| 517 current_importer = importer or default_importer or ModuleImporter() | |
| 518 current_importer.install() | |
| 519 | |
| 520 def uninstall(): | |
| 521 global current_importer | |
| 522 current_importer.uninstall() |
