Greg Stein | 281b8d8 | 1999-11-07 12:54:45 +0000 | [diff] [blame] | 1 | # |
Greg Stein | 99a5621 | 2000-06-26 17:31:49 +0000 | [diff] [blame] | 2 | # imputil.py: import utilities |
Greg Stein | 281b8d8 | 1999-11-07 12:54:45 +0000 | [diff] [blame] | 3 | # |
Greg Stein | 99a5621 | 2000-06-26 17:31:49 +0000 | [diff] [blame] | 4 | |
| 5 | ### docco needed here and in Docs/ ... |
Greg Stein | 281b8d8 | 1999-11-07 12:54:45 +0000 | [diff] [blame] | 6 | |
Greg Stein | 281b8d8 | 1999-11-07 12:54:45 +0000 | [diff] [blame] | 7 | # note: avoid importing non-builtin modules |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 8 | import imp ### not available in JPython? |
Greg Stein | 281b8d8 | 1999-11-07 12:54:45 +0000 | [diff] [blame] | 9 | import sys |
| 10 | import strop |
Greg Stein | 7ec28d2 | 1999-11-20 12:31:07 +0000 | [diff] [blame] | 11 | import __builtin__ |
Greg Stein | 281b8d8 | 1999-11-07 12:54:45 +0000 | [diff] [blame] | 12 | |
| 13 | # for the DirectoryImporter |
| 14 | import struct |
| 15 | import marshal |
| 16 | |
Skip Montanaro | 17ab123 | 2001-01-24 06:27:27 +0000 | [diff] [blame] | 17 | __all__ = ["ImportManager","Importer","BuiltinImporter"] |
| 18 | |
Greg Stein | f23aa1e | 2000-01-03 02:38:29 +0000 | [diff] [blame] | 19 | _StringType = type('') |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 20 | _ModuleType = type(sys) ### doesn't work in JPython... |
Greg Stein | f23aa1e | 2000-01-03 02:38:29 +0000 | [diff] [blame] | 21 | |
| 22 | class ImportManager: |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 23 | "Manage the import process." |
Greg Stein | 281b8d8 | 1999-11-07 12:54:45 +0000 | [diff] [blame] | 24 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 25 | def install(self, namespace=vars(__builtin__)): |
| 26 | "Install this ImportManager into the specified namespace." |
Greg Stein | d4f1d20 | 2000-02-18 12:03:40 +0000 | [diff] [blame] | 27 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 28 | if isinstance(namespace, _ModuleType): |
| 29 | namespace = vars(namespace) |
Greg Stein | d4f1d20 | 2000-02-18 12:03:40 +0000 | [diff] [blame] | 30 | |
Greg Stein | 76977bb | 2001-04-07 16:05:24 +0000 | [diff] [blame] | 31 | # Note: we have no notion of "chaining" |
Greg Stein | 3bb578c | 2000-02-18 13:04:10 +0000 | [diff] [blame] | 32 | |
Greg Stein | 76977bb | 2001-04-07 16:05:24 +0000 | [diff] [blame] | 33 | # Record the previous import hook, then install our own. |
| 34 | self.previous_importer = namespace['__import__'] |
| 35 | self.namespace = namespace |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 36 | namespace['__import__'] = self._import_hook |
Greg Stein | 76977bb | 2001-04-07 16:05:24 +0000 | [diff] [blame] | 37 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 38 | ### fix this |
| 39 | #namespace['reload'] = self._reload_hook |
Greg Stein | f23aa1e | 2000-01-03 02:38:29 +0000 | [diff] [blame] | 40 | |
Greg Stein | 76977bb | 2001-04-07 16:05:24 +0000 | [diff] [blame] | 41 | def uninstall(self): |
| 42 | "Restore the previous import mechanism." |
| 43 | self.namespace['__import__'] = self.previous_importer |
| 44 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 45 | def add_suffix(self, suffix, importFunc): |
| 46 | assert callable(importFunc) |
| 47 | self.fs_imp.add_suffix(suffix, importFunc) |
Greg Stein | 281b8d8 | 1999-11-07 12:54:45 +0000 | [diff] [blame] | 48 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 49 | ###################################################################### |
| 50 | # |
| 51 | # PRIVATE METHODS |
| 52 | # |
Greg Stein | 3bb578c | 2000-02-18 13:04:10 +0000 | [diff] [blame] | 53 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 54 | clsFilesystemImporter = None |
Greg Stein | 281b8d8 | 1999-11-07 12:54:45 +0000 | [diff] [blame] | 55 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 56 | 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 Stein | 3bb578c | 2000-02-18 13:04:10 +0000 | [diff] [blame] | 61 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 62 | # 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 Stein | 281b8d8 | 1999-11-07 12:54:45 +0000 | [diff] [blame] | 68 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 69 | # 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 Stein | f23aa1e | 2000-01-03 02:38:29 +0000 | [diff] [blame] | 77 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 78 | def _import_hook(self, fqname, globals=None, locals=None, fromlist=None): |
| 79 | """Python calls this hook to locate and import a module.""" |
Greg Stein | 63faa01 | 1999-11-20 11:22:37 +0000 | [diff] [blame] | 80 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 81 | parts = strop.split(fqname, '.') |
Greg Stein | 281b8d8 | 1999-11-07 12:54:45 +0000 | [diff] [blame] | 82 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 83 | # determine the context of this import |
| 84 | parent = self._determine_import_context(globals) |
Greg Stein | 281b8d8 | 1999-11-07 12:54:45 +0000 | [diff] [blame] | 85 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 86 | # 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 Stein | 281b8d8 | 1999-11-07 12:54:45 +0000 | [diff] [blame] | 91 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 92 | # 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 Stein | f23aa1e | 2000-01-03 02:38:29 +0000 | [diff] [blame] | 132 | raise ImportError, 'No module named ' + fqname |
Greg Stein | f23aa1e | 2000-01-03 02:38:29 +0000 | [diff] [blame] | 133 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 134 | def _determine_import_context(self, globals): |
| 135 | """Returns the context in which a module should be imported. |
Greg Stein | f23aa1e | 2000-01-03 02:38:29 +0000 | [diff] [blame] | 136 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 137 | 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 Stein | f23aa1e | 2000-01-03 02:38:29 +0000 | [diff] [blame] | 142 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 143 | 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 Stein | f23aa1e | 2000-01-03 02:38:29 +0000 | [diff] [blame] | 148 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 149 | # 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 Stein | f23aa1e | 2000-01-03 02:38:29 +0000 | [diff] [blame] | 152 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 153 | # 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 Stein | f23aa1e | 2000-01-03 02:38:29 +0000 | [diff] [blame] | 159 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 160 | i = strop.rfind(parent_fqname, '.') |
Greg Stein | f23aa1e | 2000-01-03 02:38:29 +0000 | [diff] [blame] | 161 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 162 | # a module outside of a package has no particular import context |
| 163 | if i == -1: |
| 164 | return None |
Greg Stein | f23aa1e | 2000-01-03 02:38:29 +0000 | [diff] [blame] | 165 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 166 | # 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 Stein | f23aa1e | 2000-01-03 02:38:29 +0000 | [diff] [blame] | 172 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 173 | 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 Stein | f23aa1e | 2000-01-03 02:38:29 +0000 | [diff] [blame] | 184 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 185 | def _reload_hook(self, module): |
| 186 | "Python calls this hook to reload a module." |
Greg Stein | f23aa1e | 2000-01-03 02:38:29 +0000 | [diff] [blame] | 187 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 188 | # 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 Stein | f23aa1e | 2000-01-03 02:38:29 +0000 | [diff] [blame] | 194 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 195 | # 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 Stein | f23aa1e | 2000-01-03 02:38:29 +0000 | [diff] [blame] | 200 | |
| 201 | |
| 202 | class Importer: |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 203 | "Base class for replacing standard import functions." |
Greg Stein | f23aa1e | 2000-01-03 02:38:29 +0000 | [diff] [blame] | 204 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 205 | def import_top(self, name): |
| 206 | "Import a top-level module." |
| 207 | return self._import_one(None, name, name) |
Greg Stein | f23aa1e | 2000-01-03 02:38:29 +0000 | [diff] [blame] | 208 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 209 | ###################################################################### |
Greg Stein | 281b8d8 | 1999-11-07 12:54:45 +0000 | [diff] [blame] | 210 | # |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 211 | # PRIVATE METHODS |
Greg Stein | 281b8d8 | 1999-11-07 12:54:45 +0000 | [diff] [blame] | 212 | # |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 213 | 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 Stein | 281b8d8 | 1999-11-07 12:54:45 +0000 | [diff] [blame] | 217 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 218 | # 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 Heller | bfae196 | 2001-02-12 09:17:06 +0000 | [diff] [blame] | 293 | # fetch from sys.modules instead of returning module directly. |
| 294 | return sys.modules[fqname] |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 295 | |
| 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 Stein | 281b8d8 | 1999-11-07 12:54:45 +0000 | [diff] [blame] | 342 | # |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 343 | # METHODS TO OVERRIDE |
| 344 | # |
| 345 | def get_code(self, parent, modname, fqname): |
| 346 | """Find and retrieve the code for the given module. |
Greg Stein | 281b8d8 | 1999-11-07 12:54:45 +0000 | [diff] [blame] | 347 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 348 | parent specifies a parent module to define a context for importing. It |
| 349 | may be None, indicating no particular context for the search. |
Greg Stein | 281b8d8 | 1999-11-07 12:54:45 +0000 | [diff] [blame] | 350 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 351 | modname specifies a single module (not dotted) within the parent. |
Greg Stein | 281b8d8 | 1999-11-07 12:54:45 +0000 | [diff] [blame] | 352 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 353 | 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 Stein | 281b8d8 | 1999-11-07 12:54:45 +0000 | [diff] [blame] | 357 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 358 | This method should return None, or a 3-tuple. |
Greg Stein | 281b8d8 | 1999-11-07 12:54:45 +0000 | [diff] [blame] | 359 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 360 | * If the module was not found, then None should be returned. |
Greg Stein | 281b8d8 | 1999-11-07 12:54:45 +0000 | [diff] [blame] | 361 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 362 | * 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 Stein | 281b8d8 | 1999-11-07 12:54:45 +0000 | [diff] [blame] | 364 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 365 | * 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 Stein | f23aa1e | 2000-01-03 02:38:29 +0000 | [diff] [blame] | 368 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 369 | * 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 Stein | 281b8d8 | 1999-11-07 12:54:45 +0000 | [diff] [blame] | 377 | |
| 378 | |
| 379 | ###################################################################### |
| 380 | # |
Greg Stein | 63faa01 | 1999-11-20 11:22:37 +0000 | [diff] [blame] | 381 | # Some handy stuff for the Importers |
| 382 | # |
| 383 | |
Greg Stein | d4f1d20 | 2000-02-18 12:03:40 +0000 | [diff] [blame] | 384 | # byte-compiled file suffix character |
Greg Stein | 63faa01 | 1999-11-20 11:22:37 +0000 | [diff] [blame] | 385 | _suffix_char = __debug__ and 'c' or 'o' |
| 386 | |
| 387 | # byte-compiled file suffix |
| 388 | _suffix = '.py' + _suffix_char |
| 389 | |
Greg Stein | 63faa01 | 1999-11-20 11:22:37 +0000 | [diff] [blame] | 390 | def _compile(pathname, timestamp): |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 391 | """Compile (and cache) a Python source file. |
Greg Stein | 63faa01 | 1999-11-20 11:22:37 +0000 | [diff] [blame] | 392 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 393 | The file specified by <pathname> is compiled to a code object and |
| 394 | returned. |
Greg Stein | 63faa01 | 1999-11-20 11:22:37 +0000 | [diff] [blame] | 395 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 396 | 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 Stein | 63faa01 | 1999-11-20 11:22:37 +0000 | [diff] [blame] | 404 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 405 | # 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 Stein | 63faa01 | 1999-11-20 11:22:37 +0000 | [diff] [blame] | 418 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 419 | return code |
Greg Stein | 63faa01 | 1999-11-20 11:22:37 +0000 | [diff] [blame] | 420 | |
| 421 | _os_stat = _os_path_join = None |
| 422 | def _os_bootstrap(): |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 423 | "Set up 'os' module replacement functions for use during import bootstrap." |
Greg Stein | 63faa01 | 1999-11-20 11:22:37 +0000 | [diff] [blame] | 424 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 425 | names = sys.builtin_module_names |
Greg Stein | 63faa01 | 1999-11-20 11:22:37 +0000 | [diff] [blame] | 426 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 427 | 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 Drake | 8152d32 | 2000-12-12 23:20:45 +0000 | [diff] [blame] | 448 | if a[-1:] != ':': |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 449 | a = a + ':' |
| 450 | return a + b |
| 451 | else: |
| 452 | raise ImportError, 'no os specific module found' |
Greg Stein | 63faa01 | 1999-11-20 11:22:37 +0000 | [diff] [blame] | 453 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 454 | 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 Stein | 63faa01 | 1999-11-20 11:22:37 +0000 | [diff] [blame] | 462 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 463 | global _os_stat |
| 464 | _os_stat = stat |
| 465 | |
| 466 | global _os_path_join |
| 467 | _os_path_join = join |
Greg Stein | 63faa01 | 1999-11-20 11:22:37 +0000 | [diff] [blame] | 468 | |
| 469 | def _os_path_isdir(pathname): |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 470 | "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 Stein | 63faa01 | 1999-11-20 11:22:37 +0000 | [diff] [blame] | 476 | |
| 477 | def _timestamp(pathname): |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 478 | "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 Stein | 63faa01 | 1999-11-20 11:22:37 +0000 | [diff] [blame] | 484 | |
Greg Stein | 63faa01 | 1999-11-20 11:22:37 +0000 | [diff] [blame] | 485 | |
| 486 | ###################################################################### |
| 487 | # |
| 488 | # Emulate the import mechanism for builtin and frozen modules |
| 489 | # |
| 490 | class BuiltinImporter(Importer): |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 491 | 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 Stein | 63faa01 | 1999-11-20 11:22:37 +0000 | [diff] [blame] | 495 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 496 | # 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 Stein | 63faa01 | 1999-11-20 11:22:37 +0000 | [diff] [blame] | 504 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 505 | # got it. now load and return it. |
| 506 | module = imp.load_module(modname, None, modname, ('', '', type)) |
| 507 | return 0, module, { } |
Greg Stein | 63faa01 | 1999-11-20 11:22:37 +0000 | [diff] [blame] | 508 | |
| 509 | |
| 510 | ###################################################################### |
Greg Stein | f23aa1e | 2000-01-03 02:38:29 +0000 | [diff] [blame] | 511 | # |
| 512 | # Internal importer used for importing from the filesystem |
| 513 | # |
| 514 | class _FilesystemImporter(Importer): |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 515 | def __init__(self): |
| 516 | self.suffixes = [ ] |
Greg Stein | 3bb578c | 2000-02-18 13:04:10 +0000 | [diff] [blame] | 517 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 518 | def add_suffix(self, suffix, importFunc): |
| 519 | assert callable(importFunc) |
| 520 | self.suffixes.append((suffix, importFunc)) |
Greg Stein | f23aa1e | 2000-01-03 02:38:29 +0000 | [diff] [blame] | 521 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 522 | 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 Stein | f23aa1e | 2000-01-03 02:38:29 +0000 | [diff] [blame] | 527 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 528 | 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 Stein | f23aa1e | 2000-01-03 02:38:29 +0000 | [diff] [blame] | 534 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 535 | return self._import_pathname(_os_path_join(parent.__pkgdir__, modname), |
Greg Stein | f23aa1e | 2000-01-03 02:38:29 +0000 | [diff] [blame] | 536 | fqname) |
Greg Stein | f23aa1e | 2000-01-03 02:38:29 +0000 | [diff] [blame] | 537 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 538 | 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 Stein | f23aa1e | 2000-01-03 02:38:29 +0000 | [diff] [blame] | 558 | |
| 559 | ###################################################################### |
| 560 | # |
| 561 | # SUFFIX-BASED IMPORTERS |
| 562 | # |
| 563 | |
Greg Stein | 3bb578c | 2000-02-18 13:04:10 +0000 | [diff] [blame] | 564 | def py_suffix_importer(filename, finfo, fqname): |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 565 | file = filename[:-3] + _suffix |
| 566 | t_py = long(finfo[8]) |
| 567 | t_pyc = _timestamp(file) |
Greg Stein | f23aa1e | 2000-01-03 02:38:29 +0000 | [diff] [blame] | 568 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 569 | 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 Stein | f23aa1e | 2000-01-03 02:38:29 +0000 | [diff] [blame] | 580 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 581 | return 0, code, { '__file__' : file } |
Greg Stein | f23aa1e | 2000-01-03 02:38:29 +0000 | [diff] [blame] | 582 | |
Greg Stein | 3bb578c | 2000-02-18 13:04:10 +0000 | [diff] [blame] | 583 | class DynLoadSuffixImporter: |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 584 | def __init__(self, desc): |
| 585 | self.desc = desc |
Greg Stein | f23aa1e | 2000-01-03 02:38:29 +0000 | [diff] [blame] | 586 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 587 | 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 Stein | f23aa1e | 2000-01-03 02:38:29 +0000 | [diff] [blame] | 592 | |
| 593 | |
| 594 | ###################################################################### |
Greg Stein | 63faa01 | 1999-11-20 11:22:37 +0000 | [diff] [blame] | 595 | |
Greg Stein | 63faa01 | 1999-11-20 11:22:37 +0000 | [diff] [blame] | 596 | def _print_importers(): |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 597 | 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 Stein | 63faa01 | 1999-11-20 11:22:37 +0000 | [diff] [blame] | 604 | |
Greg Stein | f23aa1e | 2000-01-03 02:38:29 +0000 | [diff] [blame] | 605 | def _test_revamp(): |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 606 | ImportManager().install() |
| 607 | sys.path.insert(0, BuiltinImporter()) |
Greg Stein | f23aa1e | 2000-01-03 02:38:29 +0000 | [diff] [blame] | 608 | |
Greg Stein | 281b8d8 | 1999-11-07 12:54:45 +0000 | [diff] [blame] | 609 | ###################################################################### |
Greg Stein | 42b9bc7 | 2000-02-19 13:36:23 +0000 | [diff] [blame] | 610 | |
| 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 Drake | 8152d32 | 2000-12-12 23:20:45 +0000 | [diff] [blame] | 657 | # watch out for sys.modules[...] is None |
Greg Stein | 42b9bc7 | 2000-02-19 13:36:23 +0000 | [diff] [blame] | 658 | # 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 Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 677 | # |
Greg Stein | 42b9bc7 | 2000-02-19 13:36:23 +0000 | [diff] [blame] | 678 | # 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 Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 693 | # > |
Greg Stein | 42b9bc7 | 2000-02-19 13:36:23 +0000 | [diff] [blame] | 694 | # > 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 Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 698 | # > |
Greg Stein | 42b9bc7 | 2000-02-19 13:36:23 +0000 | [diff] [blame] | 699 | # > 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 Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 702 | # |
Greg Stein | 42b9bc7 | 2000-02-19 13:36:23 +0000 | [diff] [blame] | 703 | # 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 | # |