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