blob: dbf092228ccc70d8a6a938d52c5e98562b7e113c [file] [log] [blame]
Fred Drake05857df2001-09-04 18:39:45 +00001"""
2Import utilities
Greg Stein99a56212000-06-26 17:31:49 +00003
Fred Drake05857df2001-09-04 18:39:45 +00004Exported classes:
5 ImportManager Manage the import process
6
7 Importer Base class for replacing standard import functions
8 BuiltinImporter Emulate the import mechanism for builtin and frozen modules
9
10 DynLoadSuffixImporter
11"""
Greg Stein281b8d81999-11-07 12:54:45 +000012
Greg Stein281b8d81999-11-07 12:54:45 +000013# note: avoid importing non-builtin modules
Tim Peters07e99cb2001-01-14 23:47:14 +000014import imp ### not available in JPython?
Greg Stein281b8d81999-11-07 12:54:45 +000015import sys
Greg Stein7ec28d21999-11-20 12:31:07 +000016import __builtin__
Greg Stein281b8d81999-11-07 12:54:45 +000017
18# for the DirectoryImporter
19import struct
20import marshal
21
Skip Montanaro17ab1232001-01-24 06:27:27 +000022__all__ = ["ImportManager","Importer","BuiltinImporter"]
23
Greg Steinf23aa1e2000-01-03 02:38:29 +000024_StringType = type('')
Tim Peters07e99cb2001-01-14 23:47:14 +000025_ModuleType = type(sys) ### doesn't work in JPython...
Greg Steinf23aa1e2000-01-03 02:38:29 +000026
27class ImportManager:
Greg Steindd6eefb2000-07-18 09:09:48 +000028 "Manage the import process."
Greg Stein281b8d81999-11-07 12:54:45 +000029
Greg Steindd6eefb2000-07-18 09:09:48 +000030 def install(self, namespace=vars(__builtin__)):
31 "Install this ImportManager into the specified namespace."
Greg Steind4f1d202000-02-18 12:03:40 +000032
Greg Steindd6eefb2000-07-18 09:09:48 +000033 if isinstance(namespace, _ModuleType):
34 namespace = vars(namespace)
Greg Steind4f1d202000-02-18 12:03:40 +000035
Greg Stein76977bb2001-04-07 16:05:24 +000036 # Note: we have no notion of "chaining"
Greg Stein3bb578c2000-02-18 13:04:10 +000037
Greg Stein76977bb2001-04-07 16:05:24 +000038 # Record the previous import hook, then install our own.
39 self.previous_importer = namespace['__import__']
40 self.namespace = namespace
Greg Steindd6eefb2000-07-18 09:09:48 +000041 namespace['__import__'] = self._import_hook
Greg Stein76977bb2001-04-07 16:05:24 +000042
Greg Steindd6eefb2000-07-18 09:09:48 +000043 ### fix this
44 #namespace['reload'] = self._reload_hook
Greg Steinf23aa1e2000-01-03 02:38:29 +000045
Greg Stein76977bb2001-04-07 16:05:24 +000046 def uninstall(self):
47 "Restore the previous import mechanism."
48 self.namespace['__import__'] = self.previous_importer
49
Greg Steindd6eefb2000-07-18 09:09:48 +000050 def add_suffix(self, suffix, importFunc):
Guido van Rossumd59da4b2007-05-22 18:11:13 +000051 assert hasattr(importFunc, '__call__')
Greg Steindd6eefb2000-07-18 09:09:48 +000052 self.fs_imp.add_suffix(suffix, importFunc)
Greg Stein281b8d81999-11-07 12:54:45 +000053
Greg Steindd6eefb2000-07-18 09:09:48 +000054 ######################################################################
55 #
56 # PRIVATE METHODS
57 #
Greg Stein3bb578c2000-02-18 13:04:10 +000058
Greg Steindd6eefb2000-07-18 09:09:48 +000059 clsFilesystemImporter = None
Greg Stein281b8d81999-11-07 12:54:45 +000060
Greg Steindd6eefb2000-07-18 09:09:48 +000061 def __init__(self, fs_imp=None):
62 # we're definitely going to be importing something in the future,
63 # so let's just load the OS-related facilities.
64 if not _os_stat:
65 _os_bootstrap()
Greg Stein3bb578c2000-02-18 13:04:10 +000066
Greg Steindd6eefb2000-07-18 09:09:48 +000067 # This is the Importer that we use for grabbing stuff from the
68 # filesystem. It defines one more method (import_from_dir) for our use.
Raymond Hettinger936654b2002-06-01 03:06:31 +000069 if fs_imp is None:
Greg Steindd6eefb2000-07-18 09:09:48 +000070 cls = self.clsFilesystemImporter or _FilesystemImporter
71 fs_imp = cls()
72 self.fs_imp = fs_imp
Greg Stein281b8d81999-11-07 12:54:45 +000073
Greg Steindd6eefb2000-07-18 09:09:48 +000074 # Initialize the set of suffixes that we recognize and import.
75 # The default will import dynamic-load modules first, followed by
76 # .py files (or a .py file's cached bytecode)
77 for desc in imp.get_suffixes():
78 if desc[2] == imp.C_EXTENSION:
79 self.add_suffix(desc[0],
80 DynLoadSuffixImporter(desc).import_file)
81 self.add_suffix('.py', py_suffix_importer)
Greg Steinf23aa1e2000-01-03 02:38:29 +000082
Greg Steindd6eefb2000-07-18 09:09:48 +000083 def _import_hook(self, fqname, globals=None, locals=None, fromlist=None):
84 """Python calls this hook to locate and import a module."""
Greg Stein63faa011999-11-20 11:22:37 +000085
Martin v. Löwisd3011cd2001-07-28 17:59:34 +000086 parts = fqname.split('.')
Greg Stein281b8d81999-11-07 12:54:45 +000087
Greg Steindd6eefb2000-07-18 09:09:48 +000088 # determine the context of this import
89 parent = self._determine_import_context(globals)
Greg Stein281b8d81999-11-07 12:54:45 +000090
Greg Steindd6eefb2000-07-18 09:09:48 +000091 # if there is a parent, then its importer should manage this import
92 if parent:
93 module = parent.__importer__._do_import(parent, parts, fromlist)
94 if module:
95 return module
Greg Stein281b8d81999-11-07 12:54:45 +000096
Greg Steindd6eefb2000-07-18 09:09:48 +000097 # has the top module already been imported?
98 try:
99 top_module = sys.modules[parts[0]]
100 except KeyError:
101
102 # look for the topmost module
103 top_module = self._import_top_module(parts[0])
104 if not top_module:
105 # the topmost module wasn't found at all.
106 raise ImportError, 'No module named ' + fqname
107
108 # fast-path simple imports
109 if len(parts) == 1:
110 if not fromlist:
111 return top_module
112
113 if not top_module.__dict__.get('__ispkg__'):
114 # __ispkg__ isn't defined (the module was not imported by us),
115 # or it is zero.
116 #
117 # In the former case, there is no way that we could import
118 # sub-modules that occur in the fromlist (but we can't raise an
119 # error because it may just be names) because we don't know how
120 # to deal with packages that were imported by other systems.
121 #
122 # In the latter case (__ispkg__ == 0), there can't be any sub-
123 # modules present, so we can just return.
124 #
125 # In both cases, since len(parts) == 1, the top_module is also
126 # the "bottom" which is the defined return when a fromlist
127 # exists.
128 return top_module
129
130 importer = top_module.__dict__.get('__importer__')
131 if importer:
132 return importer._finish_import(top_module, parts[1:], fromlist)
133
Thomas Wouters477c8d52006-05-27 19:21:47 +0000134 # Grrr, some people "import os.path" or do "from os.path import ..."
Martin v. Löwis70195da2001-07-28 20:33:41 +0000135 if len(parts) == 2 and hasattr(top_module, parts[1]):
Thomas Wouters477c8d52006-05-27 19:21:47 +0000136 if fromlist:
137 return getattr(top_module, parts[1])
138 else:
139 return top_module
Martin v. Löwis70195da2001-07-28 20:33:41 +0000140
Greg Steindd6eefb2000-07-18 09:09:48 +0000141 # If the importer does not exist, then we have to bail. A missing
142 # importer means that something else imported the module, and we have
143 # no knowledge of how to get sub-modules out of the thing.
Greg Steinf23aa1e2000-01-03 02:38:29 +0000144 raise ImportError, 'No module named ' + fqname
Greg Steinf23aa1e2000-01-03 02:38:29 +0000145
Greg Steindd6eefb2000-07-18 09:09:48 +0000146 def _determine_import_context(self, globals):
147 """Returns the context in which a module should be imported.
Greg Steinf23aa1e2000-01-03 02:38:29 +0000148
Greg Steindd6eefb2000-07-18 09:09:48 +0000149 The context could be a loaded (package) module and the imported module
150 will be looked for within that package. The context could also be None,
151 meaning there is no context -- the module should be looked for as a
152 "top-level" module.
153 """
Greg Steinf23aa1e2000-01-03 02:38:29 +0000154
Greg Steindd6eefb2000-07-18 09:09:48 +0000155 if not globals or not globals.get('__importer__'):
156 # globals does not refer to one of our modules or packages. That
157 # implies there is no relative import context (as far as we are
158 # concerned), and it should just pick it off the standard path.
159 return None
Greg Steinf23aa1e2000-01-03 02:38:29 +0000160
Greg Steindd6eefb2000-07-18 09:09:48 +0000161 # The globals refer to a module or package of ours. It will define
162 # the context of the new import. Get the module/package fqname.
163 parent_fqname = globals['__name__']
Greg Steinf23aa1e2000-01-03 02:38:29 +0000164
Greg Steindd6eefb2000-07-18 09:09:48 +0000165 # if a package is performing the import, then return itself (imports
166 # refer to pkg contents)
167 if globals['__ispkg__']:
168 parent = sys.modules[parent_fqname]
169 assert globals is parent.__dict__
170 return parent
Greg Steinf23aa1e2000-01-03 02:38:29 +0000171
Martin v. Löwisd3011cd2001-07-28 17:59:34 +0000172 i = parent_fqname.rfind('.')
Greg Steinf23aa1e2000-01-03 02:38:29 +0000173
Greg Steindd6eefb2000-07-18 09:09:48 +0000174 # a module outside of a package has no particular import context
175 if i == -1:
176 return None
Greg Steinf23aa1e2000-01-03 02:38:29 +0000177
Greg Steindd6eefb2000-07-18 09:09:48 +0000178 # if a module in a package is performing the import, then return the
179 # package (imports refer to siblings)
180 parent_fqname = parent_fqname[:i]
181 parent = sys.modules[parent_fqname]
182 assert parent.__name__ == parent_fqname
183 return parent
Greg Steinf23aa1e2000-01-03 02:38:29 +0000184
Greg Steindd6eefb2000-07-18 09:09:48 +0000185 def _import_top_module(self, name):
186 # scan sys.path looking for a location in the filesystem that contains
187 # the module, or an Importer object that can import the module.
188 for item in sys.path:
189 if isinstance(item, _StringType):
190 module = self.fs_imp.import_from_dir(item, name)
191 else:
192 module = item.import_top(name)
193 if module:
194 return module
195 return None
Greg Steinf23aa1e2000-01-03 02:38:29 +0000196
Greg Steindd6eefb2000-07-18 09:09:48 +0000197 def _reload_hook(self, module):
198 "Python calls this hook to reload a module."
Greg Steinf23aa1e2000-01-03 02:38:29 +0000199
Greg Steindd6eefb2000-07-18 09:09:48 +0000200 # reloading of a module may or may not be possible (depending on the
201 # importer), but at least we can validate that it's ours to reload
202 importer = module.__dict__.get('__importer__')
203 if not importer:
204 ### oops. now what...
205 pass
Greg Steinf23aa1e2000-01-03 02:38:29 +0000206
Greg Steindd6eefb2000-07-18 09:09:48 +0000207 # okay. it is using the imputil system, and we must delegate it, but
208 # we don't know what to do (yet)
209 ### we should blast the module dict and do another get_code(). need to
210 ### flesh this out and add proper docco...
211 raise SystemError, "reload not yet implemented"
Greg Steinf23aa1e2000-01-03 02:38:29 +0000212
213
214class Importer:
Greg Steindd6eefb2000-07-18 09:09:48 +0000215 "Base class for replacing standard import functions."
Greg Steinf23aa1e2000-01-03 02:38:29 +0000216
Greg Steindd6eefb2000-07-18 09:09:48 +0000217 def import_top(self, name):
218 "Import a top-level module."
219 return self._import_one(None, name, name)
Greg Steinf23aa1e2000-01-03 02:38:29 +0000220
Greg Steindd6eefb2000-07-18 09:09:48 +0000221 ######################################################################
Greg Stein281b8d81999-11-07 12:54:45 +0000222 #
Greg Steindd6eefb2000-07-18 09:09:48 +0000223 # PRIVATE METHODS
Greg Stein281b8d81999-11-07 12:54:45 +0000224 #
Greg Steindd6eefb2000-07-18 09:09:48 +0000225 def _finish_import(self, top, parts, fromlist):
226 # if "a.b.c" was provided, then load the ".b.c" portion down from
227 # below the top-level module.
228 bottom = self._load_tail(top, parts)
Greg Stein281b8d81999-11-07 12:54:45 +0000229
Greg Steindd6eefb2000-07-18 09:09:48 +0000230 # if the form is "import a.b.c", then return "a"
231 if not fromlist:
232 # no fromlist: return the top of the import tree
233 return top
234
235 # the top module was imported by self.
236 #
237 # this means that the bottom module was also imported by self (just
238 # now, or in the past and we fetched it from sys.modules).
239 #
240 # since we imported/handled the bottom module, this means that we can
241 # also handle its fromlist (and reliably use __ispkg__).
242
243 # if the bottom node is a package, then (potentially) import some
244 # modules.
245 #
246 # note: if it is not a package, then "fromlist" refers to names in
247 # the bottom module rather than modules.
248 # note: for a mix of names and modules in the fromlist, we will
249 # import all modules and insert those into the namespace of
250 # the package module. Python will pick up all fromlist names
251 # from the bottom (package) module; some will be modules that
252 # we imported and stored in the namespace, others are expected
253 # to be present already.
254 if bottom.__ispkg__:
255 self._import_fromlist(bottom, fromlist)
256
257 # if the form is "from a.b import c, d" then return "b"
258 return bottom
259
260 def _import_one(self, parent, modname, fqname):
261 "Import a single module."
262
263 # has the module already been imported?
264 try:
265 return sys.modules[fqname]
266 except KeyError:
267 pass
268
269 # load the module's code, or fetch the module itself
270 result = self.get_code(parent, modname, fqname)
271 if result is None:
272 return None
273
274 module = self._process_result(result, fqname)
275
276 # insert the module into its parent
277 if parent:
278 setattr(parent, modname, module)
279 return module
280
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000281 def _process_result(self, result, fqname):
282 # unpack result
283 ispkg, code, values = result
284
Greg Steindd6eefb2000-07-18 09:09:48 +0000285 # did get_code() return an actual module? (rather than a code object)
286 is_module = isinstance(code, _ModuleType)
287
288 # use the returned module, or create a new one to exec code into
289 if is_module:
290 module = code
291 else:
292 module = imp.new_module(fqname)
293
294 ### record packages a bit differently??
295 module.__importer__ = self
296 module.__ispkg__ = ispkg
297
298 # insert additional values into the module (before executing the code)
299 module.__dict__.update(values)
300
301 # the module is almost ready... make it visible
302 sys.modules[fqname] = module
303
304 # execute the code within the module's namespace
305 if not is_module:
Tim Peters3d3cfdb2004-08-04 02:29:12 +0000306 try:
Georg Brandl7cae87c2006-09-06 06:51:57 +0000307 exec(code, module.__dict__)
Tim Peters3d3cfdb2004-08-04 02:29:12 +0000308 except:
309 if fqname in sys.modules:
310 del sys.modules[fqname]
311 raise
Greg Steindd6eefb2000-07-18 09:09:48 +0000312
Thomas Hellerbfae1962001-02-12 09:17:06 +0000313 # fetch from sys.modules instead of returning module directly.
Martin v. Löwis70195da2001-07-28 20:33:41 +0000314 # also make module's __name__ agree with fqname, in case
315 # the "exec code in module.__dict__" played games on us.
316 module = sys.modules[fqname]
317 module.__name__ = fqname
318 return module
Greg Steindd6eefb2000-07-18 09:09:48 +0000319
320 def _load_tail(self, m, parts):
321 """Import the rest of the modules, down from the top-level module.
322
323 Returns the last module in the dotted list of modules.
324 """
325 for part in parts:
326 fqname = "%s.%s" % (m.__name__, part)
327 m = self._import_one(m, part, fqname)
328 if not m:
329 raise ImportError, "No module named " + fqname
330 return m
331
332 def _import_fromlist(self, package, fromlist):
333 'Import any sub-modules in the "from" list.'
334
335 # if '*' is present in the fromlist, then look for the '__all__'
336 # variable to find additional items (modules) to import.
337 if '*' in fromlist:
338 fromlist = list(fromlist) + \
339 list(package.__dict__.get('__all__', []))
340
341 for sub in fromlist:
342 # if the name is already present, then don't try to import it (it
343 # might not be a module!).
344 if sub != '*' and not hasattr(package, sub):
345 subname = "%s.%s" % (package.__name__, sub)
346 submod = self._import_one(package, sub, subname)
347 if not submod:
348 raise ImportError, "cannot import name " + subname
349
350 def _do_import(self, parent, parts, fromlist):
351 """Attempt to import the module relative to parent.
352
353 This method is used when the import context specifies that <self>
354 imported the parent module.
355 """
356 top_name = parts[0]
357 top_fqname = parent.__name__ + '.' + top_name
358 top_module = self._import_one(parent, top_name, top_fqname)
359 if not top_module:
360 # this importer and parent could not find the module (relatively)
361 return None
362
363 return self._finish_import(top_module, parts[1:], fromlist)
364
365 ######################################################################
Greg Stein281b8d81999-11-07 12:54:45 +0000366 #
Greg Steindd6eefb2000-07-18 09:09:48 +0000367 # METHODS TO OVERRIDE
368 #
369 def get_code(self, parent, modname, fqname):
370 """Find and retrieve the code for the given module.
Greg Stein281b8d81999-11-07 12:54:45 +0000371
Greg Steindd6eefb2000-07-18 09:09:48 +0000372 parent specifies a parent module to define a context for importing. It
373 may be None, indicating no particular context for the search.
Greg Stein281b8d81999-11-07 12:54:45 +0000374
Greg Steindd6eefb2000-07-18 09:09:48 +0000375 modname specifies a single module (not dotted) within the parent.
Greg Stein281b8d81999-11-07 12:54:45 +0000376
Greg Steindd6eefb2000-07-18 09:09:48 +0000377 fqname specifies the fully-qualified module name. This is a
378 (potentially) dotted name from the "root" of the module namespace
379 down to the modname.
380 If there is no parent, then modname==fqname.
Greg Stein281b8d81999-11-07 12:54:45 +0000381
Greg Steindd6eefb2000-07-18 09:09:48 +0000382 This method should return None, or a 3-tuple.
Greg Stein281b8d81999-11-07 12:54:45 +0000383
Greg Steindd6eefb2000-07-18 09:09:48 +0000384 * If the module was not found, then None should be returned.
Greg Stein281b8d81999-11-07 12:54:45 +0000385
Greg Steindd6eefb2000-07-18 09:09:48 +0000386 * The first item of the 2- or 3-tuple should be the integer 0 or 1,
387 specifying whether the module that was found is a package or not.
Greg Stein281b8d81999-11-07 12:54:45 +0000388
Greg Steindd6eefb2000-07-18 09:09:48 +0000389 * The second item is the code object for the module (it will be
390 executed within the new module's namespace). This item can also
391 be a fully-loaded module object (e.g. loaded from a shared lib).
Greg Steinf23aa1e2000-01-03 02:38:29 +0000392
Greg Steindd6eefb2000-07-18 09:09:48 +0000393 * The third item is a dictionary of name/value pairs that will be
394 inserted into new module before the code object is executed. This
395 is provided in case the module's code expects certain values (such
396 as where the module was found). When the second item is a module
397 object, then these names/values will be inserted *after* the module
398 has been loaded/initialized.
399 """
400 raise RuntimeError, "get_code not implemented"
Greg Stein281b8d81999-11-07 12:54:45 +0000401
402
403######################################################################
404#
Greg Stein63faa011999-11-20 11:22:37 +0000405# Some handy stuff for the Importers
406#
407
Greg Steind4f1d202000-02-18 12:03:40 +0000408# byte-compiled file suffix character
Greg Stein63faa011999-11-20 11:22:37 +0000409_suffix_char = __debug__ and 'c' or 'o'
410
411# byte-compiled file suffix
412_suffix = '.py' + _suffix_char
413
Greg Stein63faa011999-11-20 11:22:37 +0000414def _compile(pathname, timestamp):
Greg Steindd6eefb2000-07-18 09:09:48 +0000415 """Compile (and cache) a Python source file.
Greg Stein63faa011999-11-20 11:22:37 +0000416
Greg Steindd6eefb2000-07-18 09:09:48 +0000417 The file specified by <pathname> is compiled to a code object and
418 returned.
Greg Stein63faa011999-11-20 11:22:37 +0000419
Greg Steindd6eefb2000-07-18 09:09:48 +0000420 Presuming the appropriate privileges exist, the bytecodes will be
421 saved back to the filesystem for future imports. The source file's
422 modification timestamp must be provided as a Long value.
423 """
Jeremy Hylton13f99d72002-06-28 23:32:51 +0000424 codestring = open(pathname, 'rU').read()
Greg Steindd6eefb2000-07-18 09:09:48 +0000425 if codestring and codestring[-1] != '\n':
426 codestring = codestring + '\n'
427 code = __builtin__.compile(codestring, pathname, 'exec')
Greg Stein63faa011999-11-20 11:22:37 +0000428
Greg Steindd6eefb2000-07-18 09:09:48 +0000429 # try to cache the compiled code
430 try:
431 f = open(pathname + _suffix_char, 'wb')
432 except IOError:
433 pass
434 else:
435 f.write('\0\0\0\0')
436 f.write(struct.pack('<I', timestamp))
437 marshal.dump(code, f)
438 f.flush()
439 f.seek(0, 0)
440 f.write(imp.get_magic())
441 f.close()
Greg Stein63faa011999-11-20 11:22:37 +0000442
Greg Steindd6eefb2000-07-18 09:09:48 +0000443 return code
Greg Stein63faa011999-11-20 11:22:37 +0000444
445_os_stat = _os_path_join = None
446def _os_bootstrap():
Greg Steindd6eefb2000-07-18 09:09:48 +0000447 "Set up 'os' module replacement functions for use during import bootstrap."
Greg Stein63faa011999-11-20 11:22:37 +0000448
Greg Steindd6eefb2000-07-18 09:09:48 +0000449 names = sys.builtin_module_names
Greg Stein63faa011999-11-20 11:22:37 +0000450
Greg Steindd6eefb2000-07-18 09:09:48 +0000451 join = None
452 if 'posix' in names:
453 sep = '/'
454 from posix import stat
455 elif 'nt' in names:
456 sep = '\\'
457 from nt import stat
458 elif 'dos' in names:
459 sep = '\\'
460 from dos import stat
461 elif 'os2' in names:
462 sep = '\\'
463 from os2 import stat
464 elif 'mac' in names:
465 from mac import stat
466 def join(a, b):
467 if a == '':
468 return b
Greg Steindd6eefb2000-07-18 09:09:48 +0000469 if ':' not in a:
470 a = ':' + a
Fred Drake8152d322000-12-12 23:20:45 +0000471 if a[-1:] != ':':
Greg Steindd6eefb2000-07-18 09:09:48 +0000472 a = a + ':'
473 return a + b
474 else:
475 raise ImportError, 'no os specific module found'
Greg Stein63faa011999-11-20 11:22:37 +0000476
Greg Steindd6eefb2000-07-18 09:09:48 +0000477 if join is None:
478 def join(a, b, sep=sep):
479 if a == '':
480 return b
481 lastchar = a[-1:]
482 if lastchar == '/' or lastchar == sep:
483 return a + b
484 return a + sep + b
Greg Stein63faa011999-11-20 11:22:37 +0000485
Greg Steindd6eefb2000-07-18 09:09:48 +0000486 global _os_stat
487 _os_stat = stat
488
489 global _os_path_join
490 _os_path_join = join
Greg Stein63faa011999-11-20 11:22:37 +0000491
492def _os_path_isdir(pathname):
Greg Steindd6eefb2000-07-18 09:09:48 +0000493 "Local replacement for os.path.isdir()."
494 try:
495 s = _os_stat(pathname)
496 except OSError:
497 return None
Raymond Hettinger32200ae2002-06-01 19:51:15 +0000498 return (s.st_mode & 0170000) == 0040000
Greg Stein63faa011999-11-20 11:22:37 +0000499
500def _timestamp(pathname):
Greg Steindd6eefb2000-07-18 09:09:48 +0000501 "Return the file modification time as a Long."
502 try:
503 s = _os_stat(pathname)
504 except OSError:
505 return None
Guido van Rossume2a383d2007-01-15 16:59:06 +0000506 return int(s.st_mtime)
Greg Stein63faa011999-11-20 11:22:37 +0000507
Greg Stein63faa011999-11-20 11:22:37 +0000508
509######################################################################
510#
511# Emulate the import mechanism for builtin and frozen modules
512#
513class BuiltinImporter(Importer):
Greg Steindd6eefb2000-07-18 09:09:48 +0000514 def get_code(self, parent, modname, fqname):
515 if parent:
516 # these modules definitely do not occur within a package context
517 return None
Greg Stein63faa011999-11-20 11:22:37 +0000518
Greg Steindd6eefb2000-07-18 09:09:48 +0000519 # look for the module
520 if imp.is_builtin(modname):
521 type = imp.C_BUILTIN
522 elif imp.is_frozen(modname):
523 type = imp.PY_FROZEN
524 else:
525 # not found
526 return None
Greg Stein63faa011999-11-20 11:22:37 +0000527
Greg Steindd6eefb2000-07-18 09:09:48 +0000528 # got it. now load and return it.
529 module = imp.load_module(modname, None, modname, ('', '', type))
530 return 0, module, { }
Greg Stein63faa011999-11-20 11:22:37 +0000531
532
533######################################################################
Greg Steinf23aa1e2000-01-03 02:38:29 +0000534#
535# Internal importer used for importing from the filesystem
536#
537class _FilesystemImporter(Importer):
Greg Steindd6eefb2000-07-18 09:09:48 +0000538 def __init__(self):
539 self.suffixes = [ ]
Greg Stein3bb578c2000-02-18 13:04:10 +0000540
Greg Steindd6eefb2000-07-18 09:09:48 +0000541 def add_suffix(self, suffix, importFunc):
Guido van Rossumd59da4b2007-05-22 18:11:13 +0000542 assert hasattr(importFunc, '__call__')
Greg Steindd6eefb2000-07-18 09:09:48 +0000543 self.suffixes.append((suffix, importFunc))
Greg Steinf23aa1e2000-01-03 02:38:29 +0000544
Greg Steindd6eefb2000-07-18 09:09:48 +0000545 def import_from_dir(self, dir, fqname):
546 result = self._import_pathname(_os_path_join(dir, fqname), fqname)
547 if result:
548 return self._process_result(result, fqname)
549 return None
Greg Steinf23aa1e2000-01-03 02:38:29 +0000550
Greg Steindd6eefb2000-07-18 09:09:48 +0000551 def get_code(self, parent, modname, fqname):
552 # This importer is never used with an empty parent. Its existence is
553 # private to the ImportManager. The ImportManager uses the
554 # import_from_dir() method to import top-level modules/packages.
555 # This method is only used when we look for a module within a package.
556 assert parent
Greg Steinf23aa1e2000-01-03 02:38:29 +0000557
Thomas Wouterscf297e42007-02-23 15:07:44 +0000558 for submodule_path in parent.__path__:
559 code = self._import_pathname(_os_path_join(submodule_path, modname), fqname)
560 if code is not None:
561 return code
Greg Steindd6eefb2000-07-18 09:09:48 +0000562 return self._import_pathname(_os_path_join(parent.__pkgdir__, modname),
Greg Steinf23aa1e2000-01-03 02:38:29 +0000563 fqname)
Greg Steinf23aa1e2000-01-03 02:38:29 +0000564
Greg Steindd6eefb2000-07-18 09:09:48 +0000565 def _import_pathname(self, pathname, fqname):
566 if _os_path_isdir(pathname):
567 result = self._import_pathname(_os_path_join(pathname, '__init__'),
568 fqname)
569 if result:
570 values = result[2]
571 values['__pkgdir__'] = pathname
572 values['__path__'] = [ pathname ]
573 return 1, result[1], values
574 return None
575
576 for suffix, importFunc in self.suffixes:
577 filename = pathname + suffix
578 try:
579 finfo = _os_stat(filename)
580 except OSError:
581 pass
582 else:
583 return importFunc(filename, finfo, fqname)
584 return None
Greg Steinf23aa1e2000-01-03 02:38:29 +0000585
586######################################################################
587#
588# SUFFIX-BASED IMPORTERS
589#
590
Greg Stein3bb578c2000-02-18 13:04:10 +0000591def py_suffix_importer(filename, finfo, fqname):
Greg Steindd6eefb2000-07-18 09:09:48 +0000592 file = filename[:-3] + _suffix
Guido van Rossume2a383d2007-01-15 16:59:06 +0000593 t_py = int(finfo[8])
Greg Steindd6eefb2000-07-18 09:09:48 +0000594 t_pyc = _timestamp(file)
Greg Steinf23aa1e2000-01-03 02:38:29 +0000595
Greg Steindd6eefb2000-07-18 09:09:48 +0000596 code = None
597 if t_pyc is not None and t_pyc >= t_py:
598 f = open(file, 'rb')
599 if f.read(4) == imp.get_magic():
600 t = struct.unpack('<I', f.read(4))[0]
601 if t == t_py:
602 code = marshal.load(f)
603 f.close()
604 if code is None:
605 file = filename
606 code = _compile(file, t_py)
Greg Steinf23aa1e2000-01-03 02:38:29 +0000607
Greg Steindd6eefb2000-07-18 09:09:48 +0000608 return 0, code, { '__file__' : file }
Greg Steinf23aa1e2000-01-03 02:38:29 +0000609
Greg Stein3bb578c2000-02-18 13:04:10 +0000610class DynLoadSuffixImporter:
Greg Steindd6eefb2000-07-18 09:09:48 +0000611 def __init__(self, desc):
612 self.desc = desc
Greg Steinf23aa1e2000-01-03 02:38:29 +0000613
Greg Steindd6eefb2000-07-18 09:09:48 +0000614 def import_file(self, filename, finfo, fqname):
615 fp = open(filename, self.desc[1])
616 module = imp.load_module(fqname, fp, filename, self.desc)
617 module.__file__ = filename
618 return 0, module, { }
Greg Steinf23aa1e2000-01-03 02:38:29 +0000619
620
621######################################################################
Greg Stein63faa011999-11-20 11:22:37 +0000622
Greg Stein63faa011999-11-20 11:22:37 +0000623def _print_importers():
Greg Steindd6eefb2000-07-18 09:09:48 +0000624 items = sys.modules.items()
625 items.sort()
626 for name, module in items:
627 if module:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000628 print(name, module.__dict__.get('__importer__', '-- no importer'))
Greg Steindd6eefb2000-07-18 09:09:48 +0000629 else:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000630 print(name, '-- non-existent module')
Greg Stein63faa011999-11-20 11:22:37 +0000631
Greg Steinf23aa1e2000-01-03 02:38:29 +0000632def _test_revamp():
Greg Steindd6eefb2000-07-18 09:09:48 +0000633 ImportManager().install()
634 sys.path.insert(0, BuiltinImporter())
Greg Steinf23aa1e2000-01-03 02:38:29 +0000635
Greg Stein281b8d81999-11-07 12:54:45 +0000636######################################################################
Greg Stein42b9bc72000-02-19 13:36:23 +0000637
638#
639# TODO
640#
641# from Finn Bock:
Greg Stein42b9bc72000-02-19 13:36:23 +0000642# type(sys) is not a module in JPython. what to use instead?
643# imp.C_EXTENSION is not in JPython. same for get_suffixes and new_module
644#
645# given foo.py of:
646# import sys
647# sys.modules['foo'] = sys
648#
649# ---- standard import mechanism
650# >>> import foo
651# >>> foo
652# <module 'sys' (built-in)>
653#
654# ---- revamped import mechanism
655# >>> import imputil
656# >>> imputil._test_revamp()
657# >>> import foo
658# >>> foo
659# <module 'foo' from 'foo.py'>
660#
661#
662# from MAL:
663# should BuiltinImporter exist in sys.path or hard-wired in ImportManager?
664# need __path__ processing
665# performance
666# move chaining to a subclass [gjs: it's been nuked]
Greg Stein42b9bc72000-02-19 13:36:23 +0000667# deinstall should be possible
668# query mechanism needed: is a specific Importer installed?
669# py/pyc/pyo piping hooks to filter/process these files
670# wish list:
671# distutils importer hooked to list of standard Internet repositories
672# module->file location mapper to speed FS-based imports
673# relative imports
674# keep chaining so that it can play nice with other import hooks
675#
676# from Gordon:
677# push MAL's mapper into sys.path[0] as a cache (hard-coded for apps)
678#
679# from Guido:
Greg Stein42b9bc72000-02-19 13:36:23 +0000680# need hook for MAL's walk-me-up import strategy, or Tim's absolute strategy
Fred Drake8152d322000-12-12 23:20:45 +0000681# watch out for sys.modules[...] is None
Greg Stein42b9bc72000-02-19 13:36:23 +0000682# flag to force absolute imports? (speeds _determine_import_context and
683# checking for a relative module)
684# insert names of archives into sys.path (see quote below)
685# note: reload does NOT blast module dict
686# shift import mechanisms and policies around; provide for hooks, overrides
687# (see quote below)
688# add get_source stuff
689# get_topcode and get_subcode
690# CRLF handling in _compile
691# race condition in _compile
692# refactoring of os.py to deal with _os_bootstrap problem
693# any special handling to do for importing a module with a SyntaxError?
694# (e.g. clean up the traceback)
695# implement "domain" for path-type functionality using pkg namespace
696# (rather than FS-names like __path__)
697# don't use the word "private"... maybe "internal"
698#
699#
700# Guido's comments on sys.path caching:
Tim Peters07e99cb2001-01-14 23:47:14 +0000701#
Greg Stein42b9bc72000-02-19 13:36:23 +0000702# We could cache this in a dictionary: the ImportManager can have a
703# cache dict mapping pathnames to importer objects, and a separate
704# method for coming up with an importer given a pathname that's not yet
705# in the cache. The method should do a stat and/or look at the
706# extension to decide which importer class to use; you can register new
707# importer classes by registering a suffix or a Boolean function, plus a
708# class. If you register a new importer class, the cache is zapped.
709# The cache is independent from sys.path (but maintained per
710# ImportManager instance) so that rearrangements of sys.path do the
711# right thing. If a path is dropped from sys.path the corresponding
712# cache entry is simply no longer used.
713#
714# My/Guido's comments on factoring ImportManager and Importer:
715#
716# > However, we still have a tension occurring here:
Tim Peters07e99cb2001-01-14 23:47:14 +0000717# >
Greg Stein42b9bc72000-02-19 13:36:23 +0000718# > 1) implementing policy in ImportManager assists in single-point policy
Guido van Rossuma8add0e2007-05-14 22:03:55 +0000719# > changes for app situations
Greg Stein42b9bc72000-02-19 13:36:23 +0000720# > 2) implementing policy in Importer assists in package-private policy
721# > changes for normal, operating conditions
Tim Peters07e99cb2001-01-14 23:47:14 +0000722# >
Greg Stein42b9bc72000-02-19 13:36:23 +0000723# > I'll see if I can sort out a way to do this. Maybe the Importer class will
724# > implement the methods (which can be overridden to change policy) by
725# > delegating to ImportManager.
Tim Peters07e99cb2001-01-14 23:47:14 +0000726#
Greg Stein42b9bc72000-02-19 13:36:23 +0000727# Maybe also think about what kind of policies an Importer would be
728# likely to want to change. I have a feeling that a lot of the code
729# there is actually not so much policy but a *necessity* to get things
730# working given the calling conventions for the __import__ hook: whether
731# to return the head or tail of a dotted name, or when to do the "finish
732# fromlist" stuff.
733#