Greg Stein | 281b8d8 | 1999-11-07 12:54:45 +0000 | [diff] [blame] | 1 | # |
| 2 | # imputil.py |
| 3 | # |
| 4 | # Written by Greg Stein. Public Domain. |
| 5 | # No Copyright, no Rights Reserved, and no Warranties. |
| 6 | # |
| 7 | # Utilities to help out with custom import mechanisms. |
| 8 | # |
| 9 | # Additional modifications were contribed by Marc-Andre Lemburg and |
| 10 | # Gordon McMillan. |
| 11 | # |
Greg Stein | d4c64ba | 1999-11-07 13:14:58 +0000 | [diff] [blame] | 12 | # This module is maintained by Greg and is available at: |
Greg Stein | d4f1d20 | 2000-02-18 12:03:40 +0000 | [diff] [blame] | 13 | # http://www.lyra.org/greg/python/ |
Greg Stein | d4c64ba | 1999-11-07 13:14:58 +0000 | [diff] [blame] | 14 | # |
| 15 | # Since this isn't in the Python distribution yet, we'll use the CVS ID |
| 16 | # for tracking: |
| 17 | # $Id$ |
| 18 | # |
Greg Stein | 281b8d8 | 1999-11-07 12:54:45 +0000 | [diff] [blame] | 19 | |
Greg Stein | 281b8d8 | 1999-11-07 12:54:45 +0000 | [diff] [blame] | 20 | # note: avoid importing non-builtin modules |
Greg Stein | 3bb578c | 2000-02-18 13:04:10 +0000 | [diff] [blame] | 21 | import imp ### not available in JPython? |
Greg Stein | 281b8d8 | 1999-11-07 12:54:45 +0000 | [diff] [blame] | 22 | import sys |
| 23 | import strop |
Greg Stein | 7ec28d2 | 1999-11-20 12:31:07 +0000 | [diff] [blame] | 24 | import __builtin__ |
Greg Stein | 281b8d8 | 1999-11-07 12:54:45 +0000 | [diff] [blame] | 25 | |
| 26 | # for the DirectoryImporter |
| 27 | import struct |
| 28 | import marshal |
| 29 | |
Greg Stein | f23aa1e | 2000-01-03 02:38:29 +0000 | [diff] [blame] | 30 | _StringType = type('') |
Greg Stein | d4f1d20 | 2000-02-18 12:03:40 +0000 | [diff] [blame] | 31 | _ModuleType = type(sys) ### doesn't work in JPython... |
Greg Stein | f23aa1e | 2000-01-03 02:38:29 +0000 | [diff] [blame] | 32 | |
| 33 | class ImportManager: |
| 34 | "Manage the import process." |
Greg Stein | 281b8d8 | 1999-11-07 12:54:45 +0000 | [diff] [blame] | 35 | |
Greg Stein | d4f1d20 | 2000-02-18 12:03:40 +0000 | [diff] [blame] | 36 | def install(self, namespace=vars(__builtin__)): |
| 37 | "Install this ImportManager into the specified namespace." |
| 38 | |
| 39 | if isinstance(namespace, _ModuleType): |
| 40 | namespace = vars(namespace) |
| 41 | |
| 42 | ### Note that we have no notion of "uninstall" or "chaining" |
Greg Stein | 3bb578c | 2000-02-18 13:04:10 +0000 | [diff] [blame] | 43 | |
Greg Stein | d4f1d20 | 2000-02-18 12:03:40 +0000 | [diff] [blame] | 44 | namespace['__import__'] = self._import_hook |
Greg Stein | f23aa1e | 2000-01-03 02:38:29 +0000 | [diff] [blame] | 45 | ### fix this |
Greg Stein | d4f1d20 | 2000-02-18 12:03:40 +0000 | [diff] [blame] | 46 | #namespace['reload'] = self._reload_hook |
Greg Stein | f23aa1e | 2000-01-03 02:38:29 +0000 | [diff] [blame] | 47 | |
Greg Stein | 3bb578c | 2000-02-18 13:04:10 +0000 | [diff] [blame] | 48 | def add_suffix(self, suffix, importFunc): |
| 49 | assert callable(importFunc) |
| 50 | self.fs_imp.add_suffix(suffix, importFunc) |
Greg Stein | 281b8d8 | 1999-11-07 12:54:45 +0000 | [diff] [blame] | 51 | |
| 52 | ###################################################################### |
| 53 | # |
| 54 | # PRIVATE METHODS |
| 55 | # |
Greg Stein | 3bb578c | 2000-02-18 13:04:10 +0000 | [diff] [blame] | 56 | |
| 57 | clsFilesystemImporter = None |
| 58 | |
| 59 | def __init__(self, fs_imp=None): |
Greg Stein | f23aa1e | 2000-01-03 02:38:29 +0000 | [diff] [blame] | 60 | # we're definitely going to be importing something in the future, |
| 61 | # so let's just load the OS-related facilities. |
| 62 | if not _os_stat: |
| 63 | _os_bootstrap() |
Greg Stein | 281b8d8 | 1999-11-07 12:54:45 +0000 | [diff] [blame] | 64 | |
Greg Stein | 3bb578c | 2000-02-18 13:04:10 +0000 | [diff] [blame] | 65 | # This is the Importer that we use for grabbing stuff from the |
| 66 | # filesystem. It defines one more method (import_from_dir) for our use. |
| 67 | if not fs_imp: |
| 68 | cls = self.clsFilesystemImporter or _FilesystemImporter |
| 69 | fs_imp = cls() |
| 70 | self.fs_imp = fs_imp |
| 71 | |
Greg Stein | f23aa1e | 2000-01-03 02:38:29 +0000 | [diff] [blame] | 72 | # Initialize the set of suffixes that we recognize and import. |
| 73 | # The default will import dynamic-load modules first, followed by |
| 74 | # .py files (or a .py file's cached bytecode) |
Greg Stein | f23aa1e | 2000-01-03 02:38:29 +0000 | [diff] [blame] | 75 | for desc in imp.get_suffixes(): |
| 76 | if desc[2] == imp.C_EXTENSION: |
Greg Stein | 3bb578c | 2000-02-18 13:04:10 +0000 | [diff] [blame] | 77 | self.add_suffix(desc[0], DynLoadSuffixImporter(desc).import_file) |
| 78 | self.add_suffix('.py', py_suffix_importer) |
Greg Stein | 281b8d8 | 1999-11-07 12:54:45 +0000 | [diff] [blame] | 79 | |
Greg Stein | f23aa1e | 2000-01-03 02:38:29 +0000 | [diff] [blame] | 80 | def _import_hook(self, fqname, globals=None, locals=None, fromlist=None): |
| 81 | """Python calls this hook to locate and import a module.""" |
| 82 | |
| 83 | parts = strop.split(fqname, '.') |
Greg Stein | 63faa01 | 1999-11-20 11:22:37 +0000 | [diff] [blame] | 84 | |
Greg Stein | 281b8d8 | 1999-11-07 12:54:45 +0000 | [diff] [blame] | 85 | # determine the context of this import |
| 86 | parent = self._determine_import_context(globals) |
| 87 | |
Greg Stein | f23aa1e | 2000-01-03 02:38:29 +0000 | [diff] [blame] | 88 | # if there is a parent, then its importer should manage this import |
| 89 | if parent: |
| 90 | module = parent.__importer__._do_import(parent, parts, fromlist) |
| 91 | if module: |
| 92 | return module |
Greg Stein | 281b8d8 | 1999-11-07 12:54:45 +0000 | [diff] [blame] | 93 | |
Greg Stein | f23aa1e | 2000-01-03 02:38:29 +0000 | [diff] [blame] | 94 | # has the top module already been imported? |
| 95 | try: |
| 96 | top_module = sys.modules[parts[0]] |
| 97 | except KeyError: |
Greg Stein | 281b8d8 | 1999-11-07 12:54:45 +0000 | [diff] [blame] | 98 | |
Greg Stein | f23aa1e | 2000-01-03 02:38:29 +0000 | [diff] [blame] | 99 | # look for the topmost module |
| 100 | top_module = self._import_top_module(parts[0]) |
| 101 | if not top_module: |
| 102 | # the topmost module wasn't found at all. |
| 103 | raise ImportError, 'No module named ' + fqname |
Greg Stein | f23aa1e | 2000-01-03 02:38:29 +0000 | [diff] [blame] | 104 | |
| 105 | # fast-path simple imports |
| 106 | if len(parts) == 1: |
| 107 | if not fromlist: |
| 108 | return top_module |
| 109 | |
| 110 | if not top_module.__dict__.get('__ispkg__'): |
| 111 | # __ispkg__ isn't defined (the module was not imported by us), or |
| 112 | # it is zero. |
| 113 | # |
| 114 | # In the former case, there is no way that we could import |
| 115 | # sub-modules that occur in the fromlist (but we can't raise an |
| 116 | # error because it may just be names) because we don't know how |
| 117 | # to deal with packages that were imported by other systems. |
| 118 | # |
| 119 | # In the latter case (__ispkg__ == 0), there can't be any sub- |
| 120 | # modules present, so we can just return. |
| 121 | # |
| 122 | # In both cases, since len(parts) == 1, the top_module is also |
| 123 | # the "bottom" which is the defined return when a fromlist exists. |
| 124 | return top_module |
| 125 | |
| 126 | importer = top_module.__dict__.get('__importer__') |
| 127 | if importer: |
| 128 | return importer._finish_import(top_module, parts[1:], fromlist) |
| 129 | |
| 130 | # If the importer does not exist, then we have to bail. A missing importer |
| 131 | # means that something else imported the module, and we have no knowledge |
| 132 | # of how to get sub-modules out of the thing. |
| 133 | raise ImportError, 'No module named ' + fqname |
Greg Stein | f23aa1e | 2000-01-03 02:38:29 +0000 | [diff] [blame] | 134 | |
| 135 | def _determine_import_context(self, globals): |
| 136 | """Returns the context in which a module should be imported. |
| 137 | |
| 138 | The context could be a loaded (package) module and the imported module |
| 139 | will be looked for within that package. The context could also be None, |
| 140 | meaning there is no context -- the module should be looked for as a |
| 141 | "top-level" module. |
| 142 | """ |
| 143 | |
| 144 | if not globals or not globals.get('__importer__'): |
| 145 | # globals does not refer to one of our modules or packages. That |
| 146 | # implies there is no relative import context (as far as we are |
| 147 | # concerned), and it should just pick it off the standard path. |
| 148 | return None |
| 149 | |
| 150 | # The globals refer to a module or package of ours. It will define |
| 151 | # the context of the new import. Get the module/package fqname. |
| 152 | parent_fqname = globals['__name__'] |
| 153 | |
| 154 | # if a package is performing the import, then return itself (imports |
| 155 | # refer to pkg contents) |
| 156 | if globals['__ispkg__']: |
| 157 | parent = sys.modules[parent_fqname] |
| 158 | assert globals is parent.__dict__ |
| 159 | return parent |
| 160 | |
| 161 | i = strop.rfind(parent_fqname, '.') |
| 162 | |
| 163 | # a module outside of a package has no particular import context |
| 164 | if i == -1: |
| 165 | return None |
| 166 | |
| 167 | # if a module in a package is performing the import, then return the |
| 168 | # package (imports refer to siblings) |
| 169 | parent_fqname = parent_fqname[:i] |
| 170 | parent = sys.modules[parent_fqname] |
| 171 | assert parent.__name__ == parent_fqname |
| 172 | return parent |
| 173 | |
| 174 | def _import_top_module(self, name): |
| 175 | # scan sys.path looking for a location in the filesystem that contains |
| 176 | # the module, or an Importer object that can import the module. |
| 177 | for item in sys.path: |
Greg Stein | d4f1d20 | 2000-02-18 12:03:40 +0000 | [diff] [blame] | 178 | if isinstance(item, _StringType): |
Greg Stein | f23aa1e | 2000-01-03 02:38:29 +0000 | [diff] [blame] | 179 | module = self.fs_imp.import_from_dir(item, name) |
| 180 | else: |
| 181 | module = item.import_top(name) |
| 182 | if module: |
| 183 | return module |
| 184 | return None |
| 185 | |
| 186 | def _reload_hook(self, module): |
| 187 | "Python calls this hook to reload a module." |
| 188 | |
| 189 | # reloading of a module may or may not be possible (depending on the |
| 190 | # importer), but at least we can validate that it's ours to reload |
| 191 | importer = module.__dict__.get('__importer__') |
| 192 | if not importer: |
Greg Stein | d4f1d20 | 2000-02-18 12:03:40 +0000 | [diff] [blame] | 193 | ### oops. now what... |
| 194 | pass |
Greg Stein | f23aa1e | 2000-01-03 02:38:29 +0000 | [diff] [blame] | 195 | |
| 196 | # okay. it is using the imputil system, and we must delegate it, but |
| 197 | # we don't know what to do (yet) |
| 198 | ### we should blast the module dict and do another get_code(). need to |
| 199 | ### flesh this out and add proper docco... |
| 200 | raise SystemError, "reload not yet implemented" |
| 201 | |
| 202 | |
| 203 | class Importer: |
| 204 | "Base class for replacing standard import functions." |
| 205 | |
Greg Stein | f23aa1e | 2000-01-03 02:38:29 +0000 | [diff] [blame] | 206 | def import_top(self, name): |
| 207 | "Import a top-level module." |
| 208 | return self._import_one(None, name, name) |
| 209 | |
| 210 | ###################################################################### |
| 211 | # |
| 212 | # PRIVATE METHODS |
| 213 | # |
| 214 | def _finish_import(self, top, parts, fromlist): |
Greg Stein | 281b8d8 | 1999-11-07 12:54:45 +0000 | [diff] [blame] | 215 | # if "a.b.c" was provided, then load the ".b.c" portion down from |
| 216 | # below the top-level module. |
Greg Stein | f23aa1e | 2000-01-03 02:38:29 +0000 | [diff] [blame] | 217 | bottom = self._load_tail(top, parts) |
Greg Stein | 281b8d8 | 1999-11-07 12:54:45 +0000 | [diff] [blame] | 218 | |
| 219 | # if the form is "import a.b.c", then return "a" |
| 220 | if not fromlist: |
| 221 | # no fromlist: return the top of the import tree |
| 222 | return top |
| 223 | |
Greg Stein | f23aa1e | 2000-01-03 02:38:29 +0000 | [diff] [blame] | 224 | # the top module was imported by self. |
Greg Stein | 281b8d8 | 1999-11-07 12:54:45 +0000 | [diff] [blame] | 225 | # |
Greg Stein | f23aa1e | 2000-01-03 02:38:29 +0000 | [diff] [blame] | 226 | # this means that the bottom module was also imported by self (just |
| 227 | # now, or in the past and we fetched it from sys.modules). |
Greg Stein | 281b8d8 | 1999-11-07 12:54:45 +0000 | [diff] [blame] | 228 | # |
| 229 | # since we imported/handled the bottom module, this means that we can |
Greg Stein | f23aa1e | 2000-01-03 02:38:29 +0000 | [diff] [blame] | 230 | # also handle its fromlist (and reliably use __ispkg__). |
Greg Stein | 281b8d8 | 1999-11-07 12:54:45 +0000 | [diff] [blame] | 231 | |
| 232 | # if the bottom node is a package, then (potentially) import some modules. |
| 233 | # |
| 234 | # note: if it is not a package, then "fromlist" refers to names in |
| 235 | # the bottom module rather than modules. |
| 236 | # note: for a mix of names and modules in the fromlist, we will |
| 237 | # import all modules and insert those into the namespace of |
| 238 | # the package module. Python will pick up all fromlist names |
| 239 | # from the bottom (package) module; some will be modules that |
| 240 | # we imported and stored in the namespace, others are expected |
| 241 | # to be present already. |
Greg Stein | f23aa1e | 2000-01-03 02:38:29 +0000 | [diff] [blame] | 242 | if bottom.__ispkg__: |
Greg Stein | 281b8d8 | 1999-11-07 12:54:45 +0000 | [diff] [blame] | 243 | self._import_fromlist(bottom, fromlist) |
| 244 | |
| 245 | # if the form is "from a.b import c, d" then return "b" |
| 246 | return bottom |
| 247 | |
Greg Stein | 281b8d8 | 1999-11-07 12:54:45 +0000 | [diff] [blame] | 248 | def _import_one(self, parent, modname, fqname): |
| 249 | "Import a single module." |
| 250 | |
| 251 | # has the module already been imported? |
| 252 | try: |
| 253 | return sys.modules[fqname] |
| 254 | except KeyError: |
| 255 | pass |
| 256 | |
| 257 | # load the module's code, or fetch the module itself |
| 258 | result = self.get_code(parent, modname, fqname) |
| 259 | if result is None: |
| 260 | return None |
| 261 | |
Greg Stein | f23aa1e | 2000-01-03 02:38:29 +0000 | [diff] [blame] | 262 | module = self._process_result(result, fqname) |
Greg Stein | 281b8d8 | 1999-11-07 12:54:45 +0000 | [diff] [blame] | 263 | |
| 264 | # insert the module into its parent |
| 265 | if parent: |
| 266 | setattr(parent, modname, module) |
| 267 | return module |
| 268 | |
Greg Stein | f23aa1e | 2000-01-03 02:38:29 +0000 | [diff] [blame] | 269 | def _process_result(self, (ispkg, code, values), fqname): |
| 270 | # did get_code() return an actual module? (rather than a code object) |
Greg Stein | d4f1d20 | 2000-02-18 12:03:40 +0000 | [diff] [blame] | 271 | is_module = isinstance(code, _ModuleType) |
Greg Stein | f23aa1e | 2000-01-03 02:38:29 +0000 | [diff] [blame] | 272 | |
| 273 | # use the returned module, or create a new one to exec code into |
| 274 | if is_module: |
| 275 | module = code |
| 276 | else: |
| 277 | module = imp.new_module(fqname) |
| 278 | |
| 279 | ### record packages a bit differently?? |
| 280 | module.__importer__ = self |
| 281 | module.__ispkg__ = ispkg |
| 282 | |
| 283 | # insert additional values into the module (before executing the code) |
| 284 | module.__dict__.update(values) |
| 285 | |
| 286 | # the module is almost ready... make it visible |
| 287 | sys.modules[fqname] = module |
| 288 | |
| 289 | # execute the code within the module's namespace |
| 290 | if not is_module: |
| 291 | exec code in module.__dict__ |
| 292 | |
| 293 | return module |
| 294 | |
| 295 | def _load_tail(self, m, parts): |
Greg Stein | 281b8d8 | 1999-11-07 12:54:45 +0000 | [diff] [blame] | 296 | """Import the rest of the modules, down from the top-level module. |
| 297 | |
| 298 | Returns the last module in the dotted list of modules. |
| 299 | """ |
Greg Stein | f23aa1e | 2000-01-03 02:38:29 +0000 | [diff] [blame] | 300 | for part in parts: |
| 301 | fqname = "%s.%s" % (m.__name__, part) |
| 302 | m = self._import_one(m, part, fqname) |
| 303 | if not m: |
| 304 | raise ImportError, "No module named " + fqname |
Greg Stein | 281b8d8 | 1999-11-07 12:54:45 +0000 | [diff] [blame] | 305 | return m |
| 306 | |
| 307 | def _import_fromlist(self, package, fromlist): |
| 308 | 'Import any sub-modules in the "from" list.' |
| 309 | |
| 310 | # if '*' is present in the fromlist, then look for the '__all__' variable |
| 311 | # to find additional items (modules) to import. |
| 312 | if '*' in fromlist: |
| 313 | fromlist = list(fromlist) + list(package.__dict__.get('__all__', [])) |
| 314 | |
| 315 | for sub in fromlist: |
| 316 | # if the name is already present, then don't try to import it (it |
| 317 | # might not be a module!). |
| 318 | if sub != '*' and not hasattr(package, sub): |
| 319 | subname = "%s.%s" % (package.__name__, sub) |
| 320 | submod = self._import_one(package, sub, subname) |
| 321 | if not submod: |
| 322 | raise ImportError, "cannot import name " + subname |
| 323 | |
Greg Stein | f23aa1e | 2000-01-03 02:38:29 +0000 | [diff] [blame] | 324 | def _do_import(self, parent, parts, fromlist): |
| 325 | """Attempt to import the module relative to parent. |
Greg Stein | 281b8d8 | 1999-11-07 12:54:45 +0000 | [diff] [blame] | 326 | |
Greg Stein | f23aa1e | 2000-01-03 02:38:29 +0000 | [diff] [blame] | 327 | This method is used when the import context specifies that <self> |
| 328 | imported the parent module. |
Greg Stein | 281b8d8 | 1999-11-07 12:54:45 +0000 | [diff] [blame] | 329 | """ |
Greg Stein | f23aa1e | 2000-01-03 02:38:29 +0000 | [diff] [blame] | 330 | top_name = parts[0] |
| 331 | top_fqname = parent.__name__ + '.' + top_name |
| 332 | top_module = self._import_one(parent, top_name, top_fqname) |
| 333 | if not top_module: |
| 334 | # this importer and parent could not find the module (relatively) |
| 335 | return None |
| 336 | |
| 337 | return self._finish_import(top_module, parts[1:], fromlist) |
Greg Stein | 281b8d8 | 1999-11-07 12:54:45 +0000 | [diff] [blame] | 338 | |
| 339 | ###################################################################### |
| 340 | # |
| 341 | # METHODS TO OVERRIDE |
| 342 | # |
| 343 | def get_code(self, parent, modname, fqname): |
| 344 | """Find and retrieve the code for the given module. |
| 345 | |
| 346 | parent specifies a parent module to define a context for importing. It |
| 347 | may be None, indicating no particular context for the search. |
| 348 | |
| 349 | modname specifies a single module (not dotted) within the parent. |
| 350 | |
| 351 | fqname specifies the fully-qualified module name. This is a (potentially) |
| 352 | dotted name from the "root" of the module namespace down to the modname. |
| 353 | If there is no parent, then modname==fqname. |
| 354 | |
Greg Stein | f23aa1e | 2000-01-03 02:38:29 +0000 | [diff] [blame] | 355 | This method should return None, or a 3-tuple. |
Greg Stein | 281b8d8 | 1999-11-07 12:54:45 +0000 | [diff] [blame] | 356 | |
| 357 | * If the module was not found, then None should be returned. |
| 358 | |
| 359 | * The first item of the 2- or 3-tuple should be the integer 0 or 1, |
| 360 | specifying whether the module that was found is a package or not. |
| 361 | |
| 362 | * The second item is the code object for the module (it will be |
| 363 | executed within the new module's namespace). This item can also |
| 364 | be a fully-loaded module object (e.g. loaded from a shared lib). |
| 365 | |
Greg Stein | f23aa1e | 2000-01-03 02:38:29 +0000 | [diff] [blame] | 366 | * The third item is a dictionary of name/value pairs that will be |
| 367 | inserted into new module before the code object is executed. This |
| 368 | is provided in case the module's code expects certain values (such |
Greg Stein | 281b8d8 | 1999-11-07 12:54:45 +0000 | [diff] [blame] | 369 | as where the module was found). When the second item is a module |
| 370 | object, then these names/values will be inserted *after* the module |
| 371 | has been loaded/initialized. |
| 372 | """ |
| 373 | raise RuntimeError, "get_code not implemented" |
| 374 | |
| 375 | |
| 376 | ###################################################################### |
| 377 | # |
Greg Stein | 63faa01 | 1999-11-20 11:22:37 +0000 | [diff] [blame] | 378 | # Some handy stuff for the Importers |
| 379 | # |
| 380 | |
Greg Stein | d4f1d20 | 2000-02-18 12:03:40 +0000 | [diff] [blame] | 381 | # byte-compiled file suffix character |
Greg Stein | 63faa01 | 1999-11-20 11:22:37 +0000 | [diff] [blame] | 382 | _suffix_char = __debug__ and 'c' or 'o' |
| 383 | |
| 384 | # byte-compiled file suffix |
| 385 | _suffix = '.py' + _suffix_char |
| 386 | |
Greg Stein | 63faa01 | 1999-11-20 11:22:37 +0000 | [diff] [blame] | 387 | def _compile(pathname, timestamp): |
| 388 | """Compile (and cache) a Python source file. |
| 389 | |
| 390 | The file specified by <pathname> is compiled to a code object and |
| 391 | returned. |
| 392 | |
| 393 | Presuming the appropriate privileges exist, the bytecodes will be |
| 394 | saved back to the filesystem for future imports. The source file's |
| 395 | modification timestamp must be provided as a Long value. |
| 396 | """ |
| 397 | codestring = open(pathname, 'r').read() |
| 398 | if codestring and codestring[-1] != '\n': |
| 399 | codestring = codestring + '\n' |
| 400 | code = __builtin__.compile(codestring, pathname, 'exec') |
| 401 | |
| 402 | # try to cache the compiled code |
| 403 | try: |
| 404 | f = open(pathname + _suffix_char, 'wb') |
| 405 | except IOError: |
| 406 | pass |
| 407 | else: |
| 408 | f.write('\0\0\0\0') |
| 409 | f.write(struct.pack('<I', timestamp)) |
| 410 | marshal.dump(code, f) |
| 411 | f.flush() |
| 412 | f.seek(0, 0) |
| 413 | f.write(imp.get_magic()) |
| 414 | f.close() |
| 415 | |
| 416 | return code |
| 417 | |
| 418 | _os_stat = _os_path_join = None |
| 419 | def _os_bootstrap(): |
| 420 | "Set up 'os' module replacement functions for use during import bootstrap." |
| 421 | |
| 422 | names = sys.builtin_module_names |
| 423 | |
| 424 | join = None |
| 425 | if 'posix' in names: |
| 426 | sep = '/' |
| 427 | from posix import stat |
| 428 | elif 'nt' in names: |
| 429 | sep = '\\' |
| 430 | from nt import stat |
| 431 | elif 'dos' in names: |
| 432 | sep = '\\' |
| 433 | from dos import stat |
| 434 | elif 'os2' in names: |
| 435 | sep = '\\' |
| 436 | from os2 import stat |
| 437 | elif 'mac' in names: |
| 438 | from mac import stat |
| 439 | def join(a, b): |
| 440 | if a == '': |
| 441 | return b |
| 442 | path = s |
| 443 | if ':' not in a: |
| 444 | a = ':' + a |
| 445 | if a[-1:] <> ':': |
| 446 | a = a + ':' |
| 447 | return a + b |
| 448 | else: |
| 449 | raise ImportError, 'no os specific module found' |
| 450 | |
| 451 | if join is None: |
| 452 | def join(a, b, sep=sep): |
| 453 | if a == '': |
| 454 | return b |
| 455 | lastchar = a[-1:] |
| 456 | if lastchar == '/' or lastchar == sep: |
| 457 | return a + b |
| 458 | return a + sep + b |
| 459 | |
| 460 | global _os_stat |
| 461 | _os_stat = stat |
| 462 | |
| 463 | global _os_path_join |
| 464 | _os_path_join = join |
| 465 | |
| 466 | def _os_path_isdir(pathname): |
| 467 | "Local replacement for os.path.isdir()." |
| 468 | try: |
| 469 | s = _os_stat(pathname) |
| 470 | except OSError: |
| 471 | return None |
| 472 | return (s[0] & 0170000) == 0040000 |
| 473 | |
| 474 | def _timestamp(pathname): |
| 475 | "Return the file modification time as a Long." |
| 476 | try: |
| 477 | s = _os_stat(pathname) |
| 478 | except OSError: |
| 479 | return None |
| 480 | return long(s[8]) |
| 481 | |
Greg Stein | 63faa01 | 1999-11-20 11:22:37 +0000 | [diff] [blame] | 482 | |
| 483 | ###################################################################### |
| 484 | # |
| 485 | # Emulate the import mechanism for builtin and frozen modules |
| 486 | # |
| 487 | class BuiltinImporter(Importer): |
| 488 | def get_code(self, parent, modname, fqname): |
| 489 | if parent: |
| 490 | # these modules definitely do not occur within a package context |
| 491 | return None |
| 492 | |
| 493 | # look for the module |
| 494 | if imp.is_builtin(modname): |
| 495 | type = imp.C_BUILTIN |
| 496 | elif imp.is_frozen(modname): |
| 497 | type = imp.PY_FROZEN |
| 498 | else: |
| 499 | # not found |
| 500 | return None |
| 501 | |
| 502 | # got it. now load and return it. |
| 503 | module = imp.load_module(modname, None, modname, ('', '', type)) |
| 504 | return 0, module, { } |
| 505 | |
| 506 | |
| 507 | ###################################################################### |
Greg Stein | f23aa1e | 2000-01-03 02:38:29 +0000 | [diff] [blame] | 508 | # |
| 509 | # Internal importer used for importing from the filesystem |
| 510 | # |
| 511 | class _FilesystemImporter(Importer): |
Greg Stein | 3bb578c | 2000-02-18 13:04:10 +0000 | [diff] [blame] | 512 | def __init__(self): |
| 513 | self.suffixes = [ ] |
| 514 | |
| 515 | def add_suffix(self, suffix, importFunc): |
| 516 | assert callable(importFunc) |
| 517 | self.suffixes.append((suffix, importFunc)) |
Greg Stein | f23aa1e | 2000-01-03 02:38:29 +0000 | [diff] [blame] | 518 | |
| 519 | def import_from_dir(self, dir, fqname): |
| 520 | result = self._import_pathname(_os_path_join(dir, fqname), fqname) |
| 521 | if result: |
| 522 | return self._process_result(result, fqname) |
| 523 | return None |
| 524 | |
| 525 | def get_code(self, parent, modname, fqname): |
| 526 | # This importer is never used with an empty parent. Its existence is |
| 527 | # private to the ImportManager. The ImportManager uses the |
| 528 | # import_from_dir() method to import top-level modules/packages. |
| 529 | # This method is only used when we look for a module within a package. |
| 530 | assert parent |
| 531 | |
| 532 | return self._import_pathname(_os_path_join(parent.__pkgdir__, modname), |
| 533 | fqname) |
| 534 | |
| 535 | def _import_pathname(self, pathname, fqname): |
| 536 | if _os_path_isdir(pathname): |
| 537 | result = self._import_pathname(_os_path_join(pathname, '__init__'), |
| 538 | fqname) |
| 539 | if result: |
| 540 | values = result[2] |
| 541 | values['__pkgdir__'] = pathname |
| 542 | values['__path__'] = [ pathname ] |
| 543 | return 1, result[1], values |
| 544 | return None |
| 545 | |
Greg Stein | 3bb578c | 2000-02-18 13:04:10 +0000 | [diff] [blame] | 546 | for suffix, importFunc in self.suffixes: |
Greg Stein | f23aa1e | 2000-01-03 02:38:29 +0000 | [diff] [blame] | 547 | filename = pathname + suffix |
| 548 | try: |
| 549 | finfo = _os_stat(filename) |
| 550 | except OSError: |
| 551 | pass |
| 552 | else: |
Greg Stein | 3bb578c | 2000-02-18 13:04:10 +0000 | [diff] [blame] | 553 | return importFunc(filename, finfo, fqname) |
Greg Stein | f23aa1e | 2000-01-03 02:38:29 +0000 | [diff] [blame] | 554 | return None |
| 555 | |
| 556 | ###################################################################### |
| 557 | # |
| 558 | # SUFFIX-BASED IMPORTERS |
| 559 | # |
| 560 | |
Greg Stein | 3bb578c | 2000-02-18 13:04:10 +0000 | [diff] [blame] | 561 | def py_suffix_importer(filename, finfo, fqname): |
| 562 | file = filename[:-3] + _suffix |
| 563 | t_py = long(finfo[8]) |
| 564 | t_pyc = _timestamp(file) |
Greg Stein | f23aa1e | 2000-01-03 02:38:29 +0000 | [diff] [blame] | 565 | |
Greg Stein | 3bb578c | 2000-02-18 13:04:10 +0000 | [diff] [blame] | 566 | code = None |
| 567 | if t_pyc is not None and t_pyc >= t_py: |
| 568 | f = open(file, 'rb') |
| 569 | if f.read(4) == imp.get_magic(): |
| 570 | t = struct.unpack('<I', f.read(4))[0] |
| 571 | if t == t_py: |
| 572 | code = marshal.load(f) |
| 573 | f.close() |
| 574 | if code is None: |
| 575 | file = filename |
| 576 | code = _compile(file, t_py) |
Greg Stein | f23aa1e | 2000-01-03 02:38:29 +0000 | [diff] [blame] | 577 | |
Greg Stein | 3bb578c | 2000-02-18 13:04:10 +0000 | [diff] [blame] | 578 | return 0, code, { '__file__' : file } |
Greg Stein | f23aa1e | 2000-01-03 02:38:29 +0000 | [diff] [blame] | 579 | |
Greg Stein | 3bb578c | 2000-02-18 13:04:10 +0000 | [diff] [blame] | 580 | class DynLoadSuffixImporter: |
Greg Stein | f23aa1e | 2000-01-03 02:38:29 +0000 | [diff] [blame] | 581 | def __init__(self, desc): |
| 582 | self.desc = desc |
| 583 | |
| 584 | def import_file(self, filename, finfo, fqname): |
| 585 | fp = open(filename, self.desc[1]) |
| 586 | module = imp.load_module(fqname, fp, filename, self.desc) |
| 587 | module.__file__ = filename |
| 588 | return 0, module, { } |
| 589 | |
| 590 | |
| 591 | ###################################################################### |
Greg Stein | 63faa01 | 1999-11-20 11:22:37 +0000 | [diff] [blame] | 592 | |
Greg Stein | 63faa01 | 1999-11-20 11:22:37 +0000 | [diff] [blame] | 593 | def _print_importers(): |
| 594 | items = sys.modules.items() |
| 595 | items.sort() |
| 596 | for name, module in items: |
| 597 | if module: |
| 598 | print name, module.__dict__.get('__importer__', '-- no importer') |
| 599 | else: |
| 600 | print name, '-- non-existent module' |
| 601 | |
Greg Stein | f23aa1e | 2000-01-03 02:38:29 +0000 | [diff] [blame] | 602 | def _test_revamp(): |
| 603 | ImportManager().install() |
| 604 | sys.path.insert(0, BuiltinImporter()) |
| 605 | |
Greg Stein | 281b8d8 | 1999-11-07 12:54:45 +0000 | [diff] [blame] | 606 | ###################################################################### |
Greg Stein | 42b9bc7 | 2000-02-19 13:36:23 +0000 | [diff] [blame] | 607 | |
| 608 | # |
| 609 | # TODO |
| 610 | # |
| 611 | # from Finn Bock: |
| 612 | # remove use of "strop" -- not available in JPython |
| 613 | # type(sys) is not a module in JPython. what to use instead? |
| 614 | # imp.C_EXTENSION is not in JPython. same for get_suffixes and new_module |
| 615 | # |
| 616 | # given foo.py of: |
| 617 | # import sys |
| 618 | # sys.modules['foo'] = sys |
| 619 | # |
| 620 | # ---- standard import mechanism |
| 621 | # >>> import foo |
| 622 | # >>> foo |
| 623 | # <module 'sys' (built-in)> |
| 624 | # |
| 625 | # ---- revamped import mechanism |
| 626 | # >>> import imputil |
| 627 | # >>> imputil._test_revamp() |
| 628 | # >>> import foo |
| 629 | # >>> foo |
| 630 | # <module 'foo' from 'foo.py'> |
| 631 | # |
| 632 | # |
| 633 | # from MAL: |
| 634 | # should BuiltinImporter exist in sys.path or hard-wired in ImportManager? |
| 635 | # need __path__ processing |
| 636 | # performance |
| 637 | # move chaining to a subclass [gjs: it's been nuked] |
| 638 | # avoid strop |
| 639 | # deinstall should be possible |
| 640 | # query mechanism needed: is a specific Importer installed? |
| 641 | # py/pyc/pyo piping hooks to filter/process these files |
| 642 | # wish list: |
| 643 | # distutils importer hooked to list of standard Internet repositories |
| 644 | # module->file location mapper to speed FS-based imports |
| 645 | # relative imports |
| 646 | # keep chaining so that it can play nice with other import hooks |
| 647 | # |
| 648 | # from Gordon: |
| 649 | # push MAL's mapper into sys.path[0] as a cache (hard-coded for apps) |
| 650 | # |
| 651 | # from Guido: |
| 652 | # need to change sys.* references for rexec environs |
| 653 | # need hook for MAL's walk-me-up import strategy, or Tim's absolute strategy |
| 654 | # watch out for sys.modules[...] == None |
| 655 | # flag to force absolute imports? (speeds _determine_import_context and |
| 656 | # checking for a relative module) |
| 657 | # insert names of archives into sys.path (see quote below) |
| 658 | # note: reload does NOT blast module dict |
| 659 | # shift import mechanisms and policies around; provide for hooks, overrides |
| 660 | # (see quote below) |
| 661 | # add get_source stuff |
| 662 | # get_topcode and get_subcode |
| 663 | # CRLF handling in _compile |
| 664 | # race condition in _compile |
| 665 | # refactoring of os.py to deal with _os_bootstrap problem |
| 666 | # any special handling to do for importing a module with a SyntaxError? |
| 667 | # (e.g. clean up the traceback) |
| 668 | # implement "domain" for path-type functionality using pkg namespace |
| 669 | # (rather than FS-names like __path__) |
| 670 | # don't use the word "private"... maybe "internal" |
| 671 | # |
| 672 | # |
| 673 | # Guido's comments on sys.path caching: |
| 674 | # |
| 675 | # We could cache this in a dictionary: the ImportManager can have a |
| 676 | # cache dict mapping pathnames to importer objects, and a separate |
| 677 | # method for coming up with an importer given a pathname that's not yet |
| 678 | # in the cache. The method should do a stat and/or look at the |
| 679 | # extension to decide which importer class to use; you can register new |
| 680 | # importer classes by registering a suffix or a Boolean function, plus a |
| 681 | # class. If you register a new importer class, the cache is zapped. |
| 682 | # The cache is independent from sys.path (but maintained per |
| 683 | # ImportManager instance) so that rearrangements of sys.path do the |
| 684 | # right thing. If a path is dropped from sys.path the corresponding |
| 685 | # cache entry is simply no longer used. |
| 686 | # |
| 687 | # My/Guido's comments on factoring ImportManager and Importer: |
| 688 | # |
| 689 | # > However, we still have a tension occurring here: |
| 690 | # > |
| 691 | # > 1) implementing policy in ImportManager assists in single-point policy |
| 692 | # > changes for app/rexec situations |
| 693 | # > 2) implementing policy in Importer assists in package-private policy |
| 694 | # > changes for normal, operating conditions |
| 695 | # > |
| 696 | # > I'll see if I can sort out a way to do this. Maybe the Importer class will |
| 697 | # > implement the methods (which can be overridden to change policy) by |
| 698 | # > delegating to ImportManager. |
| 699 | # |
| 700 | # Maybe also think about what kind of policies an Importer would be |
| 701 | # likely to want to change. I have a feeling that a lot of the code |
| 702 | # there is actually not so much policy but a *necessity* to get things |
| 703 | # working given the calling conventions for the __import__ hook: whether |
| 704 | # to return the head or tail of a dotted name, or when to do the "finish |
| 705 | # fromlist" stuff. |
| 706 | # |