blob: b245ec503509d448a6d9abb292c5f4a4362ec75b [file] [log] [blame]
Greg Stein281b8d81999-11-07 12:54:45 +00001#
Greg Stein99a56212000-06-26 17:31:49 +00002# imputil.py: import utilities
Greg Stein281b8d81999-11-07 12:54:45 +00003#
Greg Stein99a56212000-06-26 17:31:49 +00004
5### docco needed here and in Docs/ ...
Greg Stein281b8d81999-11-07 12:54:45 +00006
Greg Stein281b8d81999-11-07 12:54:45 +00007# note: avoid importing non-builtin modules
Tim Peters07e99cb2001-01-14 23:47:14 +00008import imp ### not available in JPython?
Greg Stein281b8d81999-11-07 12:54:45 +00009import sys
10import strop
Greg Stein7ec28d21999-11-20 12:31:07 +000011import __builtin__
Greg Stein281b8d81999-11-07 12:54:45 +000012
13# for the DirectoryImporter
14import struct
15import marshal
16
Skip Montanaro17ab1232001-01-24 06:27:27 +000017__all__ = ["ImportManager","Importer","BuiltinImporter"]
18
Greg Steinf23aa1e2000-01-03 02:38:29 +000019_StringType = type('')
Tim Peters07e99cb2001-01-14 23:47:14 +000020_ModuleType = type(sys) ### doesn't work in JPython...
Greg Steinf23aa1e2000-01-03 02:38:29 +000021
22class ImportManager:
Greg Steindd6eefb2000-07-18 09:09:48 +000023 "Manage the import process."
Greg Stein281b8d81999-11-07 12:54:45 +000024
Greg Steindd6eefb2000-07-18 09:09:48 +000025 def install(self, namespace=vars(__builtin__)):
26 "Install this ImportManager into the specified namespace."
Greg Steind4f1d202000-02-18 12:03:40 +000027
Greg Steindd6eefb2000-07-18 09:09:48 +000028 if isinstance(namespace, _ModuleType):
29 namespace = vars(namespace)
Greg Steind4f1d202000-02-18 12:03:40 +000030
Greg Stein76977bb2001-04-07 16:05:24 +000031 # Note: we have no notion of "chaining"
Greg Stein3bb578c2000-02-18 13:04:10 +000032
Greg Stein76977bb2001-04-07 16:05:24 +000033 # Record the previous import hook, then install our own.
34 self.previous_importer = namespace['__import__']
35 self.namespace = namespace
Greg Steindd6eefb2000-07-18 09:09:48 +000036 namespace['__import__'] = self._import_hook
Greg Stein76977bb2001-04-07 16:05:24 +000037
Greg Steindd6eefb2000-07-18 09:09:48 +000038 ### fix this
39 #namespace['reload'] = self._reload_hook
Greg Steinf23aa1e2000-01-03 02:38:29 +000040
Greg Stein76977bb2001-04-07 16:05:24 +000041 def uninstall(self):
42 "Restore the previous import mechanism."
43 self.namespace['__import__'] = self.previous_importer
44
Greg Steindd6eefb2000-07-18 09:09:48 +000045 def add_suffix(self, suffix, importFunc):
46 assert callable(importFunc)
47 self.fs_imp.add_suffix(suffix, importFunc)
Greg Stein281b8d81999-11-07 12:54:45 +000048
Greg Steindd6eefb2000-07-18 09:09:48 +000049 ######################################################################
50 #
51 # PRIVATE METHODS
52 #
Greg Stein3bb578c2000-02-18 13:04:10 +000053
Greg Steindd6eefb2000-07-18 09:09:48 +000054 clsFilesystemImporter = None
Greg Stein281b8d81999-11-07 12:54:45 +000055
Greg Steindd6eefb2000-07-18 09:09:48 +000056 def __init__(self, fs_imp=None):
57 # we're definitely going to be importing something in the future,
58 # so let's just load the OS-related facilities.
59 if not _os_stat:
60 _os_bootstrap()
Greg Stein3bb578c2000-02-18 13:04:10 +000061
Greg Steindd6eefb2000-07-18 09:09:48 +000062 # This is the Importer that we use for grabbing stuff from the
63 # filesystem. It defines one more method (import_from_dir) for our use.
64 if not fs_imp:
65 cls = self.clsFilesystemImporter or _FilesystemImporter
66 fs_imp = cls()
67 self.fs_imp = fs_imp
Greg Stein281b8d81999-11-07 12:54:45 +000068
Greg Steindd6eefb2000-07-18 09:09:48 +000069 # Initialize the set of suffixes that we recognize and import.
70 # The default will import dynamic-load modules first, followed by
71 # .py files (or a .py file's cached bytecode)
72 for desc in imp.get_suffixes():
73 if desc[2] == imp.C_EXTENSION:
74 self.add_suffix(desc[0],
75 DynLoadSuffixImporter(desc).import_file)
76 self.add_suffix('.py', py_suffix_importer)
Greg Steinf23aa1e2000-01-03 02:38:29 +000077
Greg Steindd6eefb2000-07-18 09:09:48 +000078 def _import_hook(self, fqname, globals=None, locals=None, fromlist=None):
79 """Python calls this hook to locate and import a module."""
Greg Stein63faa011999-11-20 11:22:37 +000080
Greg Steindd6eefb2000-07-18 09:09:48 +000081 parts = strop.split(fqname, '.')
Greg Stein281b8d81999-11-07 12:54:45 +000082
Greg Steindd6eefb2000-07-18 09:09:48 +000083 # determine the context of this import
84 parent = self._determine_import_context(globals)
Greg Stein281b8d81999-11-07 12:54:45 +000085
Greg Steindd6eefb2000-07-18 09:09:48 +000086 # if there is a parent, then its importer should manage this import
87 if parent:
88 module = parent.__importer__._do_import(parent, parts, fromlist)
89 if module:
90 return module
Greg Stein281b8d81999-11-07 12:54:45 +000091
Greg Steindd6eefb2000-07-18 09:09:48 +000092 # has the top module already been imported?
93 try:
94 top_module = sys.modules[parts[0]]
95 except KeyError:
96
97 # look for the topmost module
98 top_module = self._import_top_module(parts[0])
99 if not top_module:
100 # the topmost module wasn't found at all.
101 raise ImportError, 'No module named ' + fqname
102
103 # fast-path simple imports
104 if len(parts) == 1:
105 if not fromlist:
106 return top_module
107
108 if not top_module.__dict__.get('__ispkg__'):
109 # __ispkg__ isn't defined (the module was not imported by us),
110 # or it is zero.
111 #
112 # In the former case, there is no way that we could import
113 # sub-modules that occur in the fromlist (but we can't raise an
114 # error because it may just be names) because we don't know how
115 # to deal with packages that were imported by other systems.
116 #
117 # In the latter case (__ispkg__ == 0), there can't be any sub-
118 # modules present, so we can just return.
119 #
120 # In both cases, since len(parts) == 1, the top_module is also
121 # the "bottom" which is the defined return when a fromlist
122 # exists.
123 return top_module
124
125 importer = top_module.__dict__.get('__importer__')
126 if importer:
127 return importer._finish_import(top_module, parts[1:], fromlist)
128
129 # If the importer does not exist, then we have to bail. A missing
130 # importer means that something else imported the module, and we have
131 # no knowledge of how to get sub-modules out of the thing.
Greg Steinf23aa1e2000-01-03 02:38:29 +0000132 raise ImportError, 'No module named ' + fqname
Greg Steinf23aa1e2000-01-03 02:38:29 +0000133
Greg Steindd6eefb2000-07-18 09:09:48 +0000134 def _determine_import_context(self, globals):
135 """Returns the context in which a module should be imported.
Greg Steinf23aa1e2000-01-03 02:38:29 +0000136
Greg Steindd6eefb2000-07-18 09:09:48 +0000137 The context could be a loaded (package) module and the imported module
138 will be looked for within that package. The context could also be None,
139 meaning there is no context -- the module should be looked for as a
140 "top-level" module.
141 """
Greg Steinf23aa1e2000-01-03 02:38:29 +0000142
Greg Steindd6eefb2000-07-18 09:09:48 +0000143 if not globals or not globals.get('__importer__'):
144 # globals does not refer to one of our modules or packages. That
145 # implies there is no relative import context (as far as we are
146 # concerned), and it should just pick it off the standard path.
147 return None
Greg Steinf23aa1e2000-01-03 02:38:29 +0000148
Greg Steindd6eefb2000-07-18 09:09:48 +0000149 # The globals refer to a module or package of ours. It will define
150 # the context of the new import. Get the module/package fqname.
151 parent_fqname = globals['__name__']
Greg Steinf23aa1e2000-01-03 02:38:29 +0000152
Greg Steindd6eefb2000-07-18 09:09:48 +0000153 # if a package is performing the import, then return itself (imports
154 # refer to pkg contents)
155 if globals['__ispkg__']:
156 parent = sys.modules[parent_fqname]
157 assert globals is parent.__dict__
158 return parent
Greg Steinf23aa1e2000-01-03 02:38:29 +0000159
Greg Steindd6eefb2000-07-18 09:09:48 +0000160 i = strop.rfind(parent_fqname, '.')
Greg Steinf23aa1e2000-01-03 02:38:29 +0000161
Greg Steindd6eefb2000-07-18 09:09:48 +0000162 # a module outside of a package has no particular import context
163 if i == -1:
164 return None
Greg Steinf23aa1e2000-01-03 02:38:29 +0000165
Greg Steindd6eefb2000-07-18 09:09:48 +0000166 # if a module in a package is performing the import, then return the
167 # package (imports refer to siblings)
168 parent_fqname = parent_fqname[:i]
169 parent = sys.modules[parent_fqname]
170 assert parent.__name__ == parent_fqname
171 return parent
Greg Steinf23aa1e2000-01-03 02:38:29 +0000172
Greg Steindd6eefb2000-07-18 09:09:48 +0000173 def _import_top_module(self, name):
174 # scan sys.path looking for a location in the filesystem that contains
175 # the module, or an Importer object that can import the module.
176 for item in sys.path:
177 if isinstance(item, _StringType):
178 module = self.fs_imp.import_from_dir(item, name)
179 else:
180 module = item.import_top(name)
181 if module:
182 return module
183 return None
Greg Steinf23aa1e2000-01-03 02:38:29 +0000184
Greg Steindd6eefb2000-07-18 09:09:48 +0000185 def _reload_hook(self, module):
186 "Python calls this hook to reload a module."
Greg Steinf23aa1e2000-01-03 02:38:29 +0000187
Greg Steindd6eefb2000-07-18 09:09:48 +0000188 # reloading of a module may or may not be possible (depending on the
189 # importer), but at least we can validate that it's ours to reload
190 importer = module.__dict__.get('__importer__')
191 if not importer:
192 ### oops. now what...
193 pass
Greg Steinf23aa1e2000-01-03 02:38:29 +0000194
Greg Steindd6eefb2000-07-18 09:09:48 +0000195 # okay. it is using the imputil system, and we must delegate it, but
196 # we don't know what to do (yet)
197 ### we should blast the module dict and do another get_code(). need to
198 ### flesh this out and add proper docco...
199 raise SystemError, "reload not yet implemented"
Greg Steinf23aa1e2000-01-03 02:38:29 +0000200
201
202class Importer:
Greg Steindd6eefb2000-07-18 09:09:48 +0000203 "Base class for replacing standard import functions."
Greg Steinf23aa1e2000-01-03 02:38:29 +0000204
Greg Steindd6eefb2000-07-18 09:09:48 +0000205 def import_top(self, name):
206 "Import a top-level module."
207 return self._import_one(None, name, name)
Greg Steinf23aa1e2000-01-03 02:38:29 +0000208
Greg Steindd6eefb2000-07-18 09:09:48 +0000209 ######################################################################
Greg Stein281b8d81999-11-07 12:54:45 +0000210 #
Greg Steindd6eefb2000-07-18 09:09:48 +0000211 # PRIVATE METHODS
Greg Stein281b8d81999-11-07 12:54:45 +0000212 #
Greg Steindd6eefb2000-07-18 09:09:48 +0000213 def _finish_import(self, top, parts, fromlist):
214 # if "a.b.c" was provided, then load the ".b.c" portion down from
215 # below the top-level module.
216 bottom = self._load_tail(top, parts)
Greg Stein281b8d81999-11-07 12:54:45 +0000217
Greg Steindd6eefb2000-07-18 09:09:48 +0000218 # if the form is "import a.b.c", then return "a"
219 if not fromlist:
220 # no fromlist: return the top of the import tree
221 return top
222
223 # the top module was imported by self.
224 #
225 # this means that the bottom module was also imported by self (just
226 # now, or in the past and we fetched it from sys.modules).
227 #
228 # since we imported/handled the bottom module, this means that we can
229 # also handle its fromlist (and reliably use __ispkg__).
230
231 # if the bottom node is a package, then (potentially) import some
232 # 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.
242 if bottom.__ispkg__:
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
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
262 module = self._process_result(result, fqname)
263
264 # insert the module into its parent
265 if parent:
266 setattr(parent, modname, module)
267 return module
268
269 def _process_result(self, (ispkg, code, values), fqname):
270 # did get_code() return an actual module? (rather than a code object)
271 is_module = isinstance(code, _ModuleType)
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
Thomas Hellerbfae1962001-02-12 09:17:06 +0000293 # fetch from sys.modules instead of returning module directly.
294 return sys.modules[fqname]
Greg Steindd6eefb2000-07-18 09:09:48 +0000295
296 def _load_tail(self, m, parts):
297 """Import the rest of the modules, down from the top-level module.
298
299 Returns the last module in the dotted list of modules.
300 """
301 for part in parts:
302 fqname = "%s.%s" % (m.__name__, part)
303 m = self._import_one(m, part, fqname)
304 if not m:
305 raise ImportError, "No module named " + fqname
306 return m
307
308 def _import_fromlist(self, package, fromlist):
309 'Import any sub-modules in the "from" list.'
310
311 # if '*' is present in the fromlist, then look for the '__all__'
312 # variable to find additional items (modules) to import.
313 if '*' in fromlist:
314 fromlist = list(fromlist) + \
315 list(package.__dict__.get('__all__', []))
316
317 for sub in fromlist:
318 # if the name is already present, then don't try to import it (it
319 # might not be a module!).
320 if sub != '*' and not hasattr(package, sub):
321 subname = "%s.%s" % (package.__name__, sub)
322 submod = self._import_one(package, sub, subname)
323 if not submod:
324 raise ImportError, "cannot import name " + subname
325
326 def _do_import(self, parent, parts, fromlist):
327 """Attempt to import the module relative to parent.
328
329 This method is used when the import context specifies that <self>
330 imported the parent module.
331 """
332 top_name = parts[0]
333 top_fqname = parent.__name__ + '.' + top_name
334 top_module = self._import_one(parent, top_name, top_fqname)
335 if not top_module:
336 # this importer and parent could not find the module (relatively)
337 return None
338
339 return self._finish_import(top_module, parts[1:], fromlist)
340
341 ######################################################################
Greg Stein281b8d81999-11-07 12:54:45 +0000342 #
Greg Steindd6eefb2000-07-18 09:09:48 +0000343 # METHODS TO OVERRIDE
344 #
345 def get_code(self, parent, modname, fqname):
346 """Find and retrieve the code for the given module.
Greg Stein281b8d81999-11-07 12:54:45 +0000347
Greg Steindd6eefb2000-07-18 09:09:48 +0000348 parent specifies a parent module to define a context for importing. It
349 may be None, indicating no particular context for the search.
Greg Stein281b8d81999-11-07 12:54:45 +0000350
Greg Steindd6eefb2000-07-18 09:09:48 +0000351 modname specifies a single module (not dotted) within the parent.
Greg Stein281b8d81999-11-07 12:54:45 +0000352
Greg Steindd6eefb2000-07-18 09:09:48 +0000353 fqname specifies the fully-qualified module name. This is a
354 (potentially) dotted name from the "root" of the module namespace
355 down to the modname.
356 If there is no parent, then modname==fqname.
Greg Stein281b8d81999-11-07 12:54:45 +0000357
Greg Steindd6eefb2000-07-18 09:09:48 +0000358 This method should return None, or a 3-tuple.
Greg Stein281b8d81999-11-07 12:54:45 +0000359
Greg Steindd6eefb2000-07-18 09:09:48 +0000360 * If the module was not found, then None should be returned.
Greg Stein281b8d81999-11-07 12:54:45 +0000361
Greg Steindd6eefb2000-07-18 09:09:48 +0000362 * The first item of the 2- or 3-tuple should be the integer 0 or 1,
363 specifying whether the module that was found is a package or not.
Greg Stein281b8d81999-11-07 12:54:45 +0000364
Greg Steindd6eefb2000-07-18 09:09:48 +0000365 * The second item is the code object for the module (it will be
366 executed within the new module's namespace). This item can also
367 be a fully-loaded module object (e.g. loaded from a shared lib).
Greg Steinf23aa1e2000-01-03 02:38:29 +0000368
Greg Steindd6eefb2000-07-18 09:09:48 +0000369 * The third item is a dictionary of name/value pairs that will be
370 inserted into new module before the code object is executed. This
371 is provided in case the module's code expects certain values (such
372 as where the module was found). When the second item is a module
373 object, then these names/values will be inserted *after* the module
374 has been loaded/initialized.
375 """
376 raise RuntimeError, "get_code not implemented"
Greg Stein281b8d81999-11-07 12:54:45 +0000377
378
379######################################################################
380#
Greg Stein63faa011999-11-20 11:22:37 +0000381# Some handy stuff for the Importers
382#
383
Greg Steind4f1d202000-02-18 12:03:40 +0000384# byte-compiled file suffix character
Greg Stein63faa011999-11-20 11:22:37 +0000385_suffix_char = __debug__ and 'c' or 'o'
386
387# byte-compiled file suffix
388_suffix = '.py' + _suffix_char
389
Greg Stein63faa011999-11-20 11:22:37 +0000390def _compile(pathname, timestamp):
Greg Steindd6eefb2000-07-18 09:09:48 +0000391 """Compile (and cache) a Python source file.
Greg Stein63faa011999-11-20 11:22:37 +0000392
Greg Steindd6eefb2000-07-18 09:09:48 +0000393 The file specified by <pathname> is compiled to a code object and
394 returned.
Greg Stein63faa011999-11-20 11:22:37 +0000395
Greg Steindd6eefb2000-07-18 09:09:48 +0000396 Presuming the appropriate privileges exist, the bytecodes will be
397 saved back to the filesystem for future imports. The source file's
398 modification timestamp must be provided as a Long value.
399 """
400 codestring = open(pathname, 'r').read()
401 if codestring and codestring[-1] != '\n':
402 codestring = codestring + '\n'
403 code = __builtin__.compile(codestring, pathname, 'exec')
Greg Stein63faa011999-11-20 11:22:37 +0000404
Greg Steindd6eefb2000-07-18 09:09:48 +0000405 # try to cache the compiled code
406 try:
407 f = open(pathname + _suffix_char, 'wb')
408 except IOError:
409 pass
410 else:
411 f.write('\0\0\0\0')
412 f.write(struct.pack('<I', timestamp))
413 marshal.dump(code, f)
414 f.flush()
415 f.seek(0, 0)
416 f.write(imp.get_magic())
417 f.close()
Greg Stein63faa011999-11-20 11:22:37 +0000418
Greg Steindd6eefb2000-07-18 09:09:48 +0000419 return code
Greg Stein63faa011999-11-20 11:22:37 +0000420
421_os_stat = _os_path_join = None
422def _os_bootstrap():
Greg Steindd6eefb2000-07-18 09:09:48 +0000423 "Set up 'os' module replacement functions for use during import bootstrap."
Greg Stein63faa011999-11-20 11:22:37 +0000424
Greg Steindd6eefb2000-07-18 09:09:48 +0000425 names = sys.builtin_module_names
Greg Stein63faa011999-11-20 11:22:37 +0000426
Greg Steindd6eefb2000-07-18 09:09:48 +0000427 join = None
428 if 'posix' in names:
429 sep = '/'
430 from posix import stat
431 elif 'nt' in names:
432 sep = '\\'
433 from nt import stat
434 elif 'dos' in names:
435 sep = '\\'
436 from dos import stat
437 elif 'os2' in names:
438 sep = '\\'
439 from os2 import stat
440 elif 'mac' in names:
441 from mac import stat
442 def join(a, b):
443 if a == '':
444 return b
445 path = s
446 if ':' not in a:
447 a = ':' + a
Fred Drake8152d322000-12-12 23:20:45 +0000448 if a[-1:] != ':':
Greg Steindd6eefb2000-07-18 09:09:48 +0000449 a = a + ':'
450 return a + b
451 else:
452 raise ImportError, 'no os specific module found'
Greg Stein63faa011999-11-20 11:22:37 +0000453
Greg Steindd6eefb2000-07-18 09:09:48 +0000454 if join is None:
455 def join(a, b, sep=sep):
456 if a == '':
457 return b
458 lastchar = a[-1:]
459 if lastchar == '/' or lastchar == sep:
460 return a + b
461 return a + sep + b
Greg Stein63faa011999-11-20 11:22:37 +0000462
Greg Steindd6eefb2000-07-18 09:09:48 +0000463 global _os_stat
464 _os_stat = stat
465
466 global _os_path_join
467 _os_path_join = join
Greg Stein63faa011999-11-20 11:22:37 +0000468
469def _os_path_isdir(pathname):
Greg Steindd6eefb2000-07-18 09:09:48 +0000470 "Local replacement for os.path.isdir()."
471 try:
472 s = _os_stat(pathname)
473 except OSError:
474 return None
475 return (s[0] & 0170000) == 0040000
Greg Stein63faa011999-11-20 11:22:37 +0000476
477def _timestamp(pathname):
Greg Steindd6eefb2000-07-18 09:09:48 +0000478 "Return the file modification time as a Long."
479 try:
480 s = _os_stat(pathname)
481 except OSError:
482 return None
483 return long(s[8])
Greg Stein63faa011999-11-20 11:22:37 +0000484
Greg Stein63faa011999-11-20 11:22:37 +0000485
486######################################################################
487#
488# Emulate the import mechanism for builtin and frozen modules
489#
490class BuiltinImporter(Importer):
Greg Steindd6eefb2000-07-18 09:09:48 +0000491 def get_code(self, parent, modname, fqname):
492 if parent:
493 # these modules definitely do not occur within a package context
494 return None
Greg Stein63faa011999-11-20 11:22:37 +0000495
Greg Steindd6eefb2000-07-18 09:09:48 +0000496 # look for the module
497 if imp.is_builtin(modname):
498 type = imp.C_BUILTIN
499 elif imp.is_frozen(modname):
500 type = imp.PY_FROZEN
501 else:
502 # not found
503 return None
Greg Stein63faa011999-11-20 11:22:37 +0000504
Greg Steindd6eefb2000-07-18 09:09:48 +0000505 # got it. now load and return it.
506 module = imp.load_module(modname, None, modname, ('', '', type))
507 return 0, module, { }
Greg Stein63faa011999-11-20 11:22:37 +0000508
509
510######################################################################
Greg Steinf23aa1e2000-01-03 02:38:29 +0000511#
512# Internal importer used for importing from the filesystem
513#
514class _FilesystemImporter(Importer):
Greg Steindd6eefb2000-07-18 09:09:48 +0000515 def __init__(self):
516 self.suffixes = [ ]
Greg Stein3bb578c2000-02-18 13:04:10 +0000517
Greg Steindd6eefb2000-07-18 09:09:48 +0000518 def add_suffix(self, suffix, importFunc):
519 assert callable(importFunc)
520 self.suffixes.append((suffix, importFunc))
Greg Steinf23aa1e2000-01-03 02:38:29 +0000521
Greg Steindd6eefb2000-07-18 09:09:48 +0000522 def import_from_dir(self, dir, fqname):
523 result = self._import_pathname(_os_path_join(dir, fqname), fqname)
524 if result:
525 return self._process_result(result, fqname)
526 return None
Greg Steinf23aa1e2000-01-03 02:38:29 +0000527
Greg Steindd6eefb2000-07-18 09:09:48 +0000528 def get_code(self, parent, modname, fqname):
529 # This importer is never used with an empty parent. Its existence is
530 # private to the ImportManager. The ImportManager uses the
531 # import_from_dir() method to import top-level modules/packages.
532 # This method is only used when we look for a module within a package.
533 assert parent
Greg Steinf23aa1e2000-01-03 02:38:29 +0000534
Greg Steindd6eefb2000-07-18 09:09:48 +0000535 return self._import_pathname(_os_path_join(parent.__pkgdir__, modname),
Greg Steinf23aa1e2000-01-03 02:38:29 +0000536 fqname)
Greg Steinf23aa1e2000-01-03 02:38:29 +0000537
Greg Steindd6eefb2000-07-18 09:09:48 +0000538 def _import_pathname(self, pathname, fqname):
539 if _os_path_isdir(pathname):
540 result = self._import_pathname(_os_path_join(pathname, '__init__'),
541 fqname)
542 if result:
543 values = result[2]
544 values['__pkgdir__'] = pathname
545 values['__path__'] = [ pathname ]
546 return 1, result[1], values
547 return None
548
549 for suffix, importFunc in self.suffixes:
550 filename = pathname + suffix
551 try:
552 finfo = _os_stat(filename)
553 except OSError:
554 pass
555 else:
556 return importFunc(filename, finfo, fqname)
557 return None
Greg Steinf23aa1e2000-01-03 02:38:29 +0000558
559######################################################################
560#
561# SUFFIX-BASED IMPORTERS
562#
563
Greg Stein3bb578c2000-02-18 13:04:10 +0000564def py_suffix_importer(filename, finfo, fqname):
Greg Steindd6eefb2000-07-18 09:09:48 +0000565 file = filename[:-3] + _suffix
566 t_py = long(finfo[8])
567 t_pyc = _timestamp(file)
Greg Steinf23aa1e2000-01-03 02:38:29 +0000568
Greg Steindd6eefb2000-07-18 09:09:48 +0000569 code = None
570 if t_pyc is not None and t_pyc >= t_py:
571 f = open(file, 'rb')
572 if f.read(4) == imp.get_magic():
573 t = struct.unpack('<I', f.read(4))[0]
574 if t == t_py:
575 code = marshal.load(f)
576 f.close()
577 if code is None:
578 file = filename
579 code = _compile(file, t_py)
Greg Steinf23aa1e2000-01-03 02:38:29 +0000580
Greg Steindd6eefb2000-07-18 09:09:48 +0000581 return 0, code, { '__file__' : file }
Greg Steinf23aa1e2000-01-03 02:38:29 +0000582
Greg Stein3bb578c2000-02-18 13:04:10 +0000583class DynLoadSuffixImporter:
Greg Steindd6eefb2000-07-18 09:09:48 +0000584 def __init__(self, desc):
585 self.desc = desc
Greg Steinf23aa1e2000-01-03 02:38:29 +0000586
Greg Steindd6eefb2000-07-18 09:09:48 +0000587 def import_file(self, filename, finfo, fqname):
588 fp = open(filename, self.desc[1])
589 module = imp.load_module(fqname, fp, filename, self.desc)
590 module.__file__ = filename
591 return 0, module, { }
Greg Steinf23aa1e2000-01-03 02:38:29 +0000592
593
594######################################################################
Greg Stein63faa011999-11-20 11:22:37 +0000595
Greg Stein63faa011999-11-20 11:22:37 +0000596def _print_importers():
Greg Steindd6eefb2000-07-18 09:09:48 +0000597 items = sys.modules.items()
598 items.sort()
599 for name, module in items:
600 if module:
601 print name, module.__dict__.get('__importer__', '-- no importer')
602 else:
603 print name, '-- non-existent module'
Greg Stein63faa011999-11-20 11:22:37 +0000604
Greg Steinf23aa1e2000-01-03 02:38:29 +0000605def _test_revamp():
Greg Steindd6eefb2000-07-18 09:09:48 +0000606 ImportManager().install()
607 sys.path.insert(0, BuiltinImporter())
Greg Steinf23aa1e2000-01-03 02:38:29 +0000608
Greg Stein281b8d81999-11-07 12:54:45 +0000609######################################################################
Greg Stein42b9bc72000-02-19 13:36:23 +0000610
611#
612# TODO
613#
614# from Finn Bock:
615# remove use of "strop" -- not available in JPython
616# type(sys) is not a module in JPython. what to use instead?
617# imp.C_EXTENSION is not in JPython. same for get_suffixes and new_module
618#
619# given foo.py of:
620# import sys
621# sys.modules['foo'] = sys
622#
623# ---- standard import mechanism
624# >>> import foo
625# >>> foo
626# <module 'sys' (built-in)>
627#
628# ---- revamped import mechanism
629# >>> import imputil
630# >>> imputil._test_revamp()
631# >>> import foo
632# >>> foo
633# <module 'foo' from 'foo.py'>
634#
635#
636# from MAL:
637# should BuiltinImporter exist in sys.path or hard-wired in ImportManager?
638# need __path__ processing
639# performance
640# move chaining to a subclass [gjs: it's been nuked]
641# avoid strop
642# deinstall should be possible
643# query mechanism needed: is a specific Importer installed?
644# py/pyc/pyo piping hooks to filter/process these files
645# wish list:
646# distutils importer hooked to list of standard Internet repositories
647# module->file location mapper to speed FS-based imports
648# relative imports
649# keep chaining so that it can play nice with other import hooks
650#
651# from Gordon:
652# push MAL's mapper into sys.path[0] as a cache (hard-coded for apps)
653#
654# from Guido:
655# need to change sys.* references for rexec environs
656# need hook for MAL's walk-me-up import strategy, or Tim's absolute strategy
Fred Drake8152d322000-12-12 23:20:45 +0000657# watch out for sys.modules[...] is None
Greg Stein42b9bc72000-02-19 13:36:23 +0000658# flag to force absolute imports? (speeds _determine_import_context and
659# checking for a relative module)
660# insert names of archives into sys.path (see quote below)
661# note: reload does NOT blast module dict
662# shift import mechanisms and policies around; provide for hooks, overrides
663# (see quote below)
664# add get_source stuff
665# get_topcode and get_subcode
666# CRLF handling in _compile
667# race condition in _compile
668# refactoring of os.py to deal with _os_bootstrap problem
669# any special handling to do for importing a module with a SyntaxError?
670# (e.g. clean up the traceback)
671# implement "domain" for path-type functionality using pkg namespace
672# (rather than FS-names like __path__)
673# don't use the word "private"... maybe "internal"
674#
675#
676# Guido's comments on sys.path caching:
Tim Peters07e99cb2001-01-14 23:47:14 +0000677#
Greg Stein42b9bc72000-02-19 13:36:23 +0000678# We could cache this in a dictionary: the ImportManager can have a
679# cache dict mapping pathnames to importer objects, and a separate
680# method for coming up with an importer given a pathname that's not yet
681# in the cache. The method should do a stat and/or look at the
682# extension to decide which importer class to use; you can register new
683# importer classes by registering a suffix or a Boolean function, plus a
684# class. If you register a new importer class, the cache is zapped.
685# The cache is independent from sys.path (but maintained per
686# ImportManager instance) so that rearrangements of sys.path do the
687# right thing. If a path is dropped from sys.path the corresponding
688# cache entry is simply no longer used.
689#
690# My/Guido's comments on factoring ImportManager and Importer:
691#
692# > However, we still have a tension occurring here:
Tim Peters07e99cb2001-01-14 23:47:14 +0000693# >
Greg Stein42b9bc72000-02-19 13:36:23 +0000694# > 1) implementing policy in ImportManager assists in single-point policy
695# > changes for app/rexec situations
696# > 2) implementing policy in Importer assists in package-private policy
697# > changes for normal, operating conditions
Tim Peters07e99cb2001-01-14 23:47:14 +0000698# >
Greg Stein42b9bc72000-02-19 13:36:23 +0000699# > I'll see if I can sort out a way to do this. Maybe the Importer class will
700# > implement the methods (which can be overridden to change policy) by
701# > delegating to ImportManager.
Tim Peters07e99cb2001-01-14 23:47:14 +0000702#
Greg Stein42b9bc72000-02-19 13:36:23 +0000703# Maybe also think about what kind of policies an Importer would be
704# likely to want to change. I have a feeling that a lot of the code
705# there is actually not so much policy but a *necessity* to get things
706# working given the calling conventions for the __import__ hook: whether
707# to return the head or tail of a dotted name, or when to do the "finish
708# fromlist" stuff.
709#