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