Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 1 | """Core implementation of import. |
| 2 | |
| 3 | This module is NOT meant to be directly imported! It has been designed such |
| 4 | that it can be bootstrapped into Python as the implementation of import. As |
| 5 | such it requires the injection of specific modules and attributes in order to |
| 6 | work. One should use importlib as the public-facing version of this module. |
| 7 | |
| 8 | """ |
| 9 | |
Brett Cannon | 7c9875c | 2009-03-04 01:10:09 +0000 | [diff] [blame] | 10 | # Injected modules are '_warnings', 'imp', 'sys', 'marshal', 'errno', '_io', |
| 11 | # and '_os' (a.k.a. 'posix', 'nt' or 'os2'). |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 12 | # Injected attribute is path_sep. |
| 13 | # |
| 14 | # When editing this code be aware that code executed at import time CANNOT |
| 15 | # reference any injected objects! This includes not only global code but also |
| 16 | # anything specified at the class level. |
| 17 | |
| 18 | |
Brett Cannon | ce43ddf | 2009-03-12 22:28:55 +0000 | [diff] [blame] | 19 | # Bootstrap-related code ###################################################### |
| 20 | |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 21 | # XXX Could also expose Modules/getpath.c:joinpath() |
| 22 | def _path_join(*args): |
| 23 | """Replacement for os.path.join.""" |
| 24 | return path_sep.join(x[:-len(path_sep)] if x.endswith(path_sep) else x |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 25 | for x in args if x) |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 26 | |
| 27 | |
| 28 | def _path_exists(path): |
| 29 | """Replacement for os.path.exists.""" |
| 30 | try: |
| 31 | _os.stat(path) |
| 32 | except OSError: |
| 33 | return False |
| 34 | else: |
| 35 | return True |
| 36 | |
| 37 | |
| 38 | def _path_is_mode_type(path, mode): |
| 39 | """Test whether the path is the specified mode type.""" |
| 40 | try: |
| 41 | stat_info = _os.stat(path) |
| 42 | except OSError: |
| 43 | return False |
| 44 | return (stat_info.st_mode & 0o170000) == mode |
| 45 | |
| 46 | |
| 47 | # XXX Could also expose Modules/getpath.c:isfile() |
| 48 | def _path_isfile(path): |
| 49 | """Replacement for os.path.isfile.""" |
| 50 | return _path_is_mode_type(path, 0o100000) |
| 51 | |
| 52 | |
| 53 | # XXX Could also expose Modules/getpath.c:isdir() |
| 54 | def _path_isdir(path): |
| 55 | """Replacement for os.path.isdir.""" |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 56 | if not path: |
| 57 | path = _os.getcwd() |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 58 | return _path_is_mode_type(path, 0o040000) |
| 59 | |
| 60 | |
| 61 | def _path_without_ext(path, ext_type): |
| 62 | """Replacement for os.path.splitext()[0].""" |
Brett Cannon | 3eeaa0a | 2009-03-12 22:07:17 +0000 | [diff] [blame] | 63 | for suffix in _suffix_list(ext_type): |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 64 | if path.endswith(suffix): |
| 65 | return path[:-len(suffix)] |
| 66 | else: |
| 67 | raise ValueError("path is not of the specified type") |
| 68 | |
| 69 | |
| 70 | def _path_absolute(path): |
| 71 | """Replacement for os.path.abspath.""" |
| 72 | if not path: |
| 73 | path = _os.getcwd() |
| 74 | try: |
| 75 | return _os._getfullpathname(path) |
| 76 | except AttributeError: |
| 77 | if path.startswith('/'): |
| 78 | return path |
| 79 | else: |
| 80 | return _path_join(_os.getcwd(), path) |
| 81 | |
| 82 | |
Brett Cannon | 3eeaa0a | 2009-03-12 22:07:17 +0000 | [diff] [blame] | 83 | def _wrap(new, old): |
Brett Cannon | 51d8bfc | 2009-02-07 02:13:28 +0000 | [diff] [blame] | 84 | """Simple substitute for functools.wraps.""" |
| 85 | for replace in ['__module__', '__name__', '__doc__']: |
| 86 | setattr(new, replace, getattr(old, replace)) |
| 87 | new.__dict__.update(old.__dict__) |
| 88 | |
| 89 | |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 90 | code_type = type(_wrap.__code__) |
| 91 | |
Brett Cannon | ce43ddf | 2009-03-12 22:28:55 +0000 | [diff] [blame] | 92 | # Finder/loader utility code ################################################## |
| 93 | |
Brett Cannon | 435aad8 | 2009-03-04 16:07:00 +0000 | [diff] [blame] | 94 | def set_package(fxn): |
Brett Cannon | 06c9d96 | 2009-02-07 01:52:25 +0000 | [diff] [blame] | 95 | """Set __package__ on the returned module.""" |
| 96 | def wrapper(*args, **kwargs): |
| 97 | module = fxn(*args, **kwargs) |
| 98 | if not hasattr(module, '__package__') or module.__package__ is None: |
| 99 | module.__package__ = module.__name__ |
| 100 | if not hasattr(module, '__path__'): |
| 101 | module.__package__ = module.__package__.rpartition('.')[0] |
| 102 | return module |
Brett Cannon | 3eeaa0a | 2009-03-12 22:07:17 +0000 | [diff] [blame] | 103 | _wrap(wrapper, fxn) |
Brett Cannon | 06c9d96 | 2009-02-07 01:52:25 +0000 | [diff] [blame] | 104 | return wrapper |
| 105 | |
| 106 | |
Brett Cannon | 2cf03a8 | 2009-03-10 05:17:37 +0000 | [diff] [blame] | 107 | def set_loader(fxn): |
| 108 | """Set __loader__ on the returned module.""" |
| 109 | def wrapper(self, *args, **kwargs): |
| 110 | module = fxn(self, *args, **kwargs) |
| 111 | if not hasattr(module, '__loader__'): |
| 112 | module.__loader__ = self |
| 113 | return module |
Brett Cannon | 3eeaa0a | 2009-03-12 22:07:17 +0000 | [diff] [blame] | 114 | _wrap(wrapper, fxn) |
Brett Cannon | 2cf03a8 | 2009-03-10 05:17:37 +0000 | [diff] [blame] | 115 | return wrapper |
| 116 | |
| 117 | |
Brett Cannon | ce43ddf | 2009-03-12 22:28:55 +0000 | [diff] [blame] | 118 | def module_for_loader(fxn): |
| 119 | """Decorator to handle selecting the proper module for loaders. |
| 120 | |
Brett Cannon | 0e0d8a6 | 2009-03-15 00:00:19 +0000 | [diff] [blame] | 121 | The decorated function is passed the module to use instead of the module |
| 122 | name. The module passed in to the function is either from sys.modules if |
| 123 | it already exists or is a new module which has __name__ set and is inserted |
| 124 | into sys.modules. If an exception is raised and the decorator created the |
| 125 | module it is subsequently removed from sys.modules. |
Brett Cannon | ce43ddf | 2009-03-12 22:28:55 +0000 | [diff] [blame] | 126 | |
Brett Cannon | 0e0d8a6 | 2009-03-15 00:00:19 +0000 | [diff] [blame] | 127 | The decorator assumes that the decorated function takes the module name as |
| 128 | the second argument. |
Brett Cannon | ce43ddf | 2009-03-12 22:28:55 +0000 | [diff] [blame] | 129 | |
| 130 | """ |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 131 | def decorated(self, fullname, *args, **kwargs): |
Brett Cannon | ce43ddf | 2009-03-12 22:28:55 +0000 | [diff] [blame] | 132 | module = sys.modules.get(fullname) |
| 133 | is_reload = bool(module) |
| 134 | if not is_reload: |
| 135 | # This must be done before open() is called as the 'io' module |
| 136 | # implicitly imports 'locale' and would otherwise trigger an |
| 137 | # infinite loop. |
| 138 | module = imp.new_module(fullname) |
| 139 | sys.modules[fullname] = module |
| 140 | try: |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 141 | return fxn(self, module, *args, **kwargs) |
Brett Cannon | ce43ddf | 2009-03-12 22:28:55 +0000 | [diff] [blame] | 142 | except: |
| 143 | if not is_reload: |
| 144 | del sys.modules[fullname] |
| 145 | raise |
| 146 | _wrap(decorated, fxn) |
| 147 | return decorated |
| 148 | |
| 149 | |
| 150 | def _check_name(method): |
| 151 | """Decorator to verify that the module being requested matches the one the |
| 152 | loader can handle. |
| 153 | |
| 154 | The first argument (self) must define _name which the second argument is |
| 155 | comapred against. If the comparison fails then ImportError is raised. |
| 156 | |
| 157 | """ |
| 158 | def inner(self, name, *args, **kwargs): |
| 159 | if self._name != name: |
| 160 | raise ImportError("loader cannot handle %s" % name) |
| 161 | return method(self, name, *args, **kwargs) |
| 162 | _wrap(inner, method) |
| 163 | return inner |
| 164 | |
| 165 | |
Brett Cannon | a113ac5 | 2009-03-15 01:41:33 +0000 | [diff] [blame] | 166 | def _requires_builtin(fxn): |
| 167 | """Decorator to verify the named module is built-in.""" |
| 168 | def wrapper(self, fullname): |
| 169 | if fullname not in sys.builtin_module_names: |
| 170 | raise ImportError("{0} is not a built-in module".format(fullname)) |
| 171 | return fxn(self, fullname) |
| 172 | _wrap(wrapper, fxn) |
| 173 | return wrapper |
| 174 | |
| 175 | |
Brett Cannon | 8d11013 | 2009-03-15 02:20:16 +0000 | [diff] [blame] | 176 | def _requires_frozen(fxn): |
| 177 | """Decorator to verify the named module is frozen.""" |
| 178 | def wrapper(self, fullname): |
| 179 | if not imp.is_frozen(fullname): |
| 180 | raise ImportError("{0} is not a frozen module".format(fullname)) |
| 181 | return fxn(self, fullname) |
| 182 | _wrap(wrapper, fxn) |
| 183 | return wrapper |
| 184 | |
| 185 | |
Brett Cannon | e9103d2 | 2009-03-12 22:37:06 +0000 | [diff] [blame] | 186 | def _suffix_list(suffix_type): |
| 187 | """Return a list of file suffixes based on the imp file type.""" |
| 188 | return [suffix[0] for suffix in imp.get_suffixes() |
| 189 | if suffix[2] == suffix_type] |
| 190 | |
| 191 | |
| 192 | # Loaders ##################################################################### |
Brett Cannon | ce43ddf | 2009-03-12 22:28:55 +0000 | [diff] [blame] | 193 | |
Brett Cannon | 5abdc93 | 2009-01-22 22:43:07 +0000 | [diff] [blame] | 194 | class BuiltinImporter: |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 195 | |
Brett Cannon | 7aa21f7 | 2009-03-15 00:53:05 +0000 | [diff] [blame] | 196 | """Meta path import for built-in modules. |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 197 | |
Brett Cannon | 7aa21f7 | 2009-03-15 00:53:05 +0000 | [diff] [blame] | 198 | All methods are either class or static methods to avoid the need to |
| 199 | instantiate the class. |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 200 | |
| 201 | """ |
| 202 | |
Brett Cannon | 5abdc93 | 2009-01-22 22:43:07 +0000 | [diff] [blame] | 203 | @classmethod |
| 204 | def find_module(cls, fullname, path=None): |
Brett Cannon | 7aa21f7 | 2009-03-15 00:53:05 +0000 | [diff] [blame] | 205 | """Find the built-in module. |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 206 | |
| 207 | If 'path' is ever specified then the search is considered a failure. |
| 208 | |
| 209 | """ |
| 210 | if path is not None: |
| 211 | return None |
Brett Cannon | 5abdc93 | 2009-01-22 22:43:07 +0000 | [diff] [blame] | 212 | return cls if imp.is_builtin(fullname) else None |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 213 | |
Brett Cannon | 78246b6 | 2009-01-25 04:56:30 +0000 | [diff] [blame] | 214 | @classmethod |
Brett Cannon | 435aad8 | 2009-03-04 16:07:00 +0000 | [diff] [blame] | 215 | @set_package |
Brett Cannon | 2cf03a8 | 2009-03-10 05:17:37 +0000 | [diff] [blame] | 216 | @set_loader |
Brett Cannon | a113ac5 | 2009-03-15 01:41:33 +0000 | [diff] [blame] | 217 | @_requires_builtin |
Brett Cannon | 78246b6 | 2009-01-25 04:56:30 +0000 | [diff] [blame] | 218 | def load_module(cls, fullname): |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 219 | """Load a built-in module.""" |
Brett Cannon | d2e7b33 | 2009-02-17 02:45:03 +0000 | [diff] [blame] | 220 | is_reload = fullname in sys.modules |
| 221 | try: |
| 222 | return imp.init_builtin(fullname) |
| 223 | except: |
| 224 | if not is_reload and fullname in sys.modules: |
| 225 | del sys.modules[fullname] |
| 226 | raise |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 227 | |
Brett Cannon | a113ac5 | 2009-03-15 01:41:33 +0000 | [diff] [blame] | 228 | @classmethod |
| 229 | @_requires_builtin |
| 230 | def get_code(cls, fullname): |
| 231 | """Return None as built-in modules do not have code objects.""" |
| 232 | return None |
| 233 | |
| 234 | @classmethod |
| 235 | @_requires_builtin |
| 236 | def get_source(cls, fullname): |
| 237 | """Return None as built-in modules do not have source code.""" |
| 238 | return None |
| 239 | |
| 240 | @classmethod |
| 241 | @_requires_builtin |
| 242 | def is_package(cls, fullname): |
| 243 | """Return None as built-in module are never packages.""" |
| 244 | return False |
| 245 | |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 246 | |
Brett Cannon | 5abdc93 | 2009-01-22 22:43:07 +0000 | [diff] [blame] | 247 | class FrozenImporter: |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 248 | |
Brett Cannon | 7aa21f7 | 2009-03-15 00:53:05 +0000 | [diff] [blame] | 249 | """Meta path import for frozen modules. |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 250 | |
Brett Cannon | 7aa21f7 | 2009-03-15 00:53:05 +0000 | [diff] [blame] | 251 | All methods are either class or static methods to avoid the need to |
| 252 | instantiate the class. |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 253 | |
Brett Cannon | 5abdc93 | 2009-01-22 22:43:07 +0000 | [diff] [blame] | 254 | """ |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 255 | |
Brett Cannon | 5abdc93 | 2009-01-22 22:43:07 +0000 | [diff] [blame] | 256 | @classmethod |
| 257 | def find_module(cls, fullname, path=None): |
| 258 | """Find a frozen module.""" |
| 259 | return cls if imp.is_frozen(fullname) else None |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 260 | |
Brett Cannon | 5abdc93 | 2009-01-22 22:43:07 +0000 | [diff] [blame] | 261 | @classmethod |
Brett Cannon | 435aad8 | 2009-03-04 16:07:00 +0000 | [diff] [blame] | 262 | @set_package |
Brett Cannon | 2cf03a8 | 2009-03-10 05:17:37 +0000 | [diff] [blame] | 263 | @set_loader |
Brett Cannon | 8d11013 | 2009-03-15 02:20:16 +0000 | [diff] [blame] | 264 | @_requires_frozen |
Brett Cannon | 5abdc93 | 2009-01-22 22:43:07 +0000 | [diff] [blame] | 265 | def load_module(cls, fullname): |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 266 | """Load a frozen module.""" |
Brett Cannon | d2e7b33 | 2009-02-17 02:45:03 +0000 | [diff] [blame] | 267 | is_reload = fullname in sys.modules |
| 268 | try: |
| 269 | return imp.init_frozen(fullname) |
| 270 | except: |
| 271 | if not is_reload and fullname in sys.modules: |
| 272 | del sys.modules[fullname] |
| 273 | raise |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 274 | |
Brett Cannon | 8d11013 | 2009-03-15 02:20:16 +0000 | [diff] [blame] | 275 | @classmethod |
| 276 | @_requires_frozen |
| 277 | def get_code(cls, fullname): |
| 278 | """Return the code object for the frozen module.""" |
| 279 | return imp.get_frozen_object(fullname) |
| 280 | |
| 281 | @classmethod |
| 282 | @_requires_frozen |
| 283 | def get_source(cls, fullname): |
| 284 | """Return None as frozen modules do not have source code.""" |
| 285 | return None |
| 286 | |
| 287 | @classmethod |
| 288 | @_requires_frozen |
| 289 | def is_package(cls, fullname): |
| 290 | """Return if the frozen module is a package.""" |
| 291 | return imp.is_frozen_package(fullname) |
| 292 | |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 293 | |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 294 | class _LoaderBasics: |
| 295 | |
| 296 | """Base class of common code needed by both SourceLoader and |
| 297 | _SourcelessFileLoader.""" |
| 298 | |
| 299 | def is_package(self, fullname): |
| 300 | """Concrete implementation of InspectLoader.is_package by checking if |
| 301 | the path returned by get_filename has a filename of '__init__.py'.""" |
| 302 | filename = self.get_filename(fullname).rpartition(path_sep)[2] |
| 303 | return filename.rsplit('.', 1)[0] == '__init__' |
| 304 | |
| 305 | def _bytes_from_bytecode(self, fullname, data, source_mtime): |
| 306 | """Return the marshalled bytes from bytecode, verifying the magic |
| 307 | number and timestamp alon the way. |
| 308 | |
| 309 | If source_mtime is None then skip the timestamp check. |
| 310 | |
| 311 | """ |
| 312 | magic = data[:4] |
| 313 | raw_timestamp = data[4:8] |
| 314 | if len(magic) != 4 or magic != imp.get_magic(): |
| 315 | raise ImportError("bad magic number in {}".format(fullname)) |
| 316 | elif len(raw_timestamp) != 4: |
| 317 | raise EOFError("bad timestamp in {}".format(fullname)) |
| 318 | elif source_mtime is not None: |
| 319 | if marshal._r_long(raw_timestamp) != source_mtime: |
| 320 | raise ImportError("bytecode is stale for {}".format(fullname)) |
| 321 | # Can't return the code object as errors from marshal loading need to |
| 322 | # propagate even when source is available. |
| 323 | return data[8:] |
| 324 | |
| 325 | @module_for_loader |
| 326 | def _load_module(self, module, *, sourceless=False): |
| 327 | """Helper for load_module able to handle either source or sourceless |
| 328 | loading.""" |
| 329 | name = module.__name__ |
| 330 | code_object = self.get_code(name) |
| 331 | module.__file__ = self.get_filename(name) |
| 332 | if not sourceless: |
| 333 | module.__cached__ = imp.cache_from_source(module.__file__) |
| 334 | else: |
| 335 | module.__cached__ = module.__file__ |
| 336 | module.__package__ = name |
| 337 | if self.is_package(name): |
| 338 | module.__path__ = [module.__file__.rsplit(path_sep, 1)[0]] |
| 339 | else: |
| 340 | module.__package__ = module.__package__.rpartition('.')[0] |
| 341 | module.__loader__ = self |
| 342 | exec(code_object, module.__dict__) |
| 343 | return module |
| 344 | |
| 345 | |
| 346 | class SourceLoader(_LoaderBasics): |
Brett Cannon | 0cf9e6a | 2010-06-28 04:57:24 +0000 | [diff] [blame] | 347 | |
Raymond Hettinger | cd92f37 | 2011-01-13 02:31:25 +0000 | [diff] [blame] | 348 | def path_mtime(self, path): |
Raymond Hettinger | d958ea7 | 2011-01-13 19:08:04 +0000 | [diff] [blame^] | 349 | """Optional method that returns the modification time (an int) for the |
| 350 | specified path, where path is a str. |
Brett Cannon | 0cf9e6a | 2010-06-28 04:57:24 +0000 | [diff] [blame] | 351 | |
| 352 | Implementing this method allows the loader to read bytecode files. |
| 353 | |
| 354 | """ |
| 355 | raise NotImplementedError |
| 356 | |
Raymond Hettinger | cd92f37 | 2011-01-13 02:31:25 +0000 | [diff] [blame] | 357 | def set_data(self, path, data): |
Raymond Hettinger | d958ea7 | 2011-01-13 19:08:04 +0000 | [diff] [blame^] | 358 | """Optional method which writes data (bytes) to a file path (a str). |
Brett Cannon | 0cf9e6a | 2010-06-28 04:57:24 +0000 | [diff] [blame] | 359 | |
| 360 | Implementing this method allows for the writing of bytecode files. |
| 361 | |
| 362 | """ |
| 363 | raise NotImplementedError |
| 364 | |
Brett Cannon | 0cf9e6a | 2010-06-28 04:57:24 +0000 | [diff] [blame] | 365 | |
| 366 | def get_source(self, fullname): |
| 367 | """Concrete implementation of InspectLoader.get_source.""" |
| 368 | import tokenize |
| 369 | path = self.get_filename(fullname) |
| 370 | try: |
| 371 | source_bytes = self.get_data(path) |
| 372 | except IOError: |
| 373 | raise ImportError("source not available through get_data()") |
| 374 | encoding = tokenize.detect_encoding(_io.BytesIO(source_bytes).readline) |
Brett Cannon | 418182e | 2010-07-03 22:32:41 +0000 | [diff] [blame] | 375 | newline_decoder = _io.IncrementalNewlineDecoder(None, True) |
| 376 | return newline_decoder.decode(source_bytes.decode(encoding[0])) |
Brett Cannon | 0cf9e6a | 2010-06-28 04:57:24 +0000 | [diff] [blame] | 377 | |
| 378 | def get_code(self, fullname): |
| 379 | """Concrete implementation of InspectLoader.get_code. |
| 380 | |
| 381 | Reading of bytecode requires path_mtime to be implemented. To write |
| 382 | bytecode, set_data must also be implemented. |
| 383 | |
| 384 | """ |
| 385 | source_path = self.get_filename(fullname) |
| 386 | bytecode_path = imp.cache_from_source(source_path) |
| 387 | source_mtime = None |
| 388 | if bytecode_path is not None: |
| 389 | try: |
| 390 | source_mtime = self.path_mtime(source_path) |
| 391 | except NotImplementedError: |
| 392 | pass |
| 393 | else: |
| 394 | try: |
| 395 | data = self.get_data(bytecode_path) |
| 396 | except IOError: |
| 397 | pass |
| 398 | else: |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 399 | try: |
| 400 | bytes_data = self._bytes_from_bytecode(fullname, data, |
| 401 | source_mtime) |
| 402 | except (ImportError, EOFError): |
| 403 | pass |
| 404 | else: |
| 405 | found = marshal.loads(bytes_data) |
| 406 | if isinstance(found, code_type): |
| 407 | return found |
| 408 | else: |
| 409 | msg = "Non-code object in {}" |
| 410 | raise ImportError(msg.format(bytecode_path)) |
Brett Cannon | 0cf9e6a | 2010-06-28 04:57:24 +0000 | [diff] [blame] | 411 | source_bytes = self.get_data(source_path) |
| 412 | code_object = compile(source_bytes, source_path, 'exec', |
| 413 | dont_inherit=True) |
| 414 | if (not sys.dont_write_bytecode and bytecode_path is not None and |
| 415 | source_mtime is not None): |
| 416 | # If e.g. Jython ever implements imp.cache_from_source to have |
| 417 | # their own cached file format, this block of code will most likely |
| 418 | # throw an exception. |
| 419 | data = bytearray(imp.get_magic()) |
| 420 | data.extend(marshal._w_long(source_mtime)) |
| 421 | data.extend(marshal.dumps(code_object)) |
| 422 | try: |
| 423 | self.set_data(bytecode_path, data) |
| 424 | except NotImplementedError: |
| 425 | pass |
| 426 | return code_object |
| 427 | |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 428 | def load_module(self, fullname): |
Brett Cannon | 0cf9e6a | 2010-06-28 04:57:24 +0000 | [diff] [blame] | 429 | """Concrete implementation of Loader.load_module. |
| 430 | |
| 431 | Requires ExecutionLoader.get_filename and ResourceLoader.get_data to be |
| 432 | implemented to load source code. Use of bytecode is dictated by whether |
| 433 | get_code uses/writes bytecode. |
| 434 | |
| 435 | """ |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 436 | return self._load_module(fullname) |
Brett Cannon | 0cf9e6a | 2010-06-28 04:57:24 +0000 | [diff] [blame] | 437 | |
| 438 | |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 439 | class _FileLoader: |
Brett Cannon | 0cf9e6a | 2010-06-28 04:57:24 +0000 | [diff] [blame] | 440 | |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 441 | """Base file loader class which implements the loader protocol methods that |
| 442 | require file system usage.""" |
Brett Cannon | 0cf9e6a | 2010-06-28 04:57:24 +0000 | [diff] [blame] | 443 | |
| 444 | def __init__(self, fullname, path): |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 445 | """Cache the module name and the path to the file found by the |
| 446 | finder.""" |
Brett Cannon | 0cf9e6a | 2010-06-28 04:57:24 +0000 | [diff] [blame] | 447 | self._name = fullname |
| 448 | self._path = path |
| 449 | |
| 450 | @_check_name |
| 451 | def get_filename(self, fullname): |
| 452 | """Return the path to the source file as found by the finder.""" |
| 453 | return self._path |
| 454 | |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 455 | def get_data(self, path): |
| 456 | """Return the data from path as raw bytes.""" |
Florent Xicluna | 764d612 | 2010-09-03 19:55:26 +0000 | [diff] [blame] | 457 | with _io.FileIO(path, 'r') as file: |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 458 | return file.read() |
| 459 | |
| 460 | |
| 461 | class _SourceFileLoader(_FileLoader, SourceLoader): |
| 462 | |
| 463 | """Concrete implementation of SourceLoader using the file system.""" |
| 464 | |
Brett Cannon | 0cf9e6a | 2010-06-28 04:57:24 +0000 | [diff] [blame] | 465 | def path_mtime(self, path): |
| 466 | """Return the modification time for the path.""" |
| 467 | return int(_os.stat(path).st_mtime) |
| 468 | |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 469 | def set_data(self, path, data): |
Brett Cannon | 0cf9e6a | 2010-06-28 04:57:24 +0000 | [diff] [blame] | 470 | """Write bytes data to a file.""" |
Brett Cannon | ee6d647 | 2010-08-22 22:19:11 +0000 | [diff] [blame] | 471 | parent, _, filename = path.rpartition(path_sep) |
| 472 | path_parts = [] |
| 473 | # Figure out what directories are missing. |
| 474 | while parent and not _path_isdir(parent): |
| 475 | parent, _, part = parent.rpartition(path_sep) |
| 476 | path_parts.append(part) |
| 477 | # Create needed directories. |
| 478 | for part in reversed(path_parts): |
| 479 | parent = _path_join(parent, part) |
| 480 | try: |
| 481 | _os.mkdir(parent) |
Brett Cannon | a7ceeb3 | 2010-08-26 21:07:13 +0000 | [diff] [blame] | 482 | except OSError as exc: |
Brett Cannon | ee6d647 | 2010-08-22 22:19:11 +0000 | [diff] [blame] | 483 | # Probably another Python process already created the dir. |
| 484 | if exc.errno == errno.EEXIST: |
| 485 | continue |
Brett Cannon | a7ceeb3 | 2010-08-26 21:07:13 +0000 | [diff] [blame] | 486 | else: |
| 487 | raise |
| 488 | except IOError as exc: |
Brett Cannon | ee6d647 | 2010-08-22 22:19:11 +0000 | [diff] [blame] | 489 | # If can't get proper access, then just forget about writing |
| 490 | # the data. |
Brett Cannon | a7ceeb3 | 2010-08-26 21:07:13 +0000 | [diff] [blame] | 491 | if exc.errno == errno.EACCES: |
Brett Cannon | ee6d647 | 2010-08-22 22:19:11 +0000 | [diff] [blame] | 492 | return |
| 493 | else: |
| 494 | raise |
Brett Cannon | 0cf9e6a | 2010-06-28 04:57:24 +0000 | [diff] [blame] | 495 | try: |
Brett Cannon | ee6d647 | 2010-08-22 22:19:11 +0000 | [diff] [blame] | 496 | with _io.FileIO(path, 'wb') as file: |
Brett Cannon | 0cf9e6a | 2010-06-28 04:57:24 +0000 | [diff] [blame] | 497 | file.write(data) |
| 498 | except IOError as exc: |
Brett Cannon | ee6d647 | 2010-08-22 22:19:11 +0000 | [diff] [blame] | 499 | # Don't worry if you can't write bytecode. |
| 500 | if exc.errno == errno.EACCES: |
| 501 | return |
| 502 | else: |
Brett Cannon | 0cf9e6a | 2010-06-28 04:57:24 +0000 | [diff] [blame] | 503 | raise |
| 504 | |
| 505 | |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 506 | class _SourcelessFileLoader(_FileLoader, _LoaderBasics): |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 507 | |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 508 | """Loader which handles sourceless file imports.""" |
Brett Cannon | 91cf882 | 2009-02-21 05:41:15 +0000 | [diff] [blame] | 509 | |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 510 | def load_module(self, fullname): |
| 511 | return self._load_module(fullname, sourceless=True) |
Brett Cannon | 6919427 | 2009-07-20 04:23:48 +0000 | [diff] [blame] | 512 | |
Brett Cannon | 91cf882 | 2009-02-21 05:41:15 +0000 | [diff] [blame] | 513 | def get_code(self, fullname): |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 514 | path = self.get_filename(fullname) |
| 515 | data = self.get_data(path) |
| 516 | bytes_data = self._bytes_from_bytecode(fullname, data, None) |
| 517 | found = marshal.loads(bytes_data) |
| 518 | if isinstance(found, code_type): |
| 519 | return found |
| 520 | else: |
| 521 | raise ImportError("Non-code object in {}".format(path)) |
Brett Cannon | 91cf882 | 2009-02-21 05:41:15 +0000 | [diff] [blame] | 522 | |
Brett Cannon | d43b30b | 2009-03-10 03:29:23 +0000 | [diff] [blame] | 523 | def get_source(self, fullname): |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 524 | """Return None as there is no source code.""" |
| 525 | return None |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 526 | |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 527 | |
Brett Cannon | e9103d2 | 2009-03-12 22:37:06 +0000 | [diff] [blame] | 528 | class _ExtensionFileLoader: |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 529 | |
Brett Cannon | e9103d2 | 2009-03-12 22:37:06 +0000 | [diff] [blame] | 530 | """Loader for extension modules. |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 531 | |
Brett Cannon | e9103d2 | 2009-03-12 22:37:06 +0000 | [diff] [blame] | 532 | The constructor is designed to work with FileFinder. |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 533 | |
| 534 | """ |
| 535 | |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 536 | def __init__(self, name, path): |
Brett Cannon | e9103d2 | 2009-03-12 22:37:06 +0000 | [diff] [blame] | 537 | """Initialize the loader. |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 538 | |
Brett Cannon | e9103d2 | 2009-03-12 22:37:06 +0000 | [diff] [blame] | 539 | If is_pkg is True then an exception is raised as extension modules |
| 540 | cannot be the __init__ module for an extension module. |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 541 | |
| 542 | """ |
Brett Cannon | e9103d2 | 2009-03-12 22:37:06 +0000 | [diff] [blame] | 543 | self._name = name |
| 544 | self._path = path |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 545 | |
Brett Cannon | e9103d2 | 2009-03-12 22:37:06 +0000 | [diff] [blame] | 546 | @_check_name |
| 547 | @set_package |
| 548 | @set_loader |
| 549 | def load_module(self, fullname): |
| 550 | """Load an extension module.""" |
| 551 | is_reload = fullname in sys.modules |
| 552 | try: |
| 553 | return imp.load_dynamic(fullname, self._path) |
| 554 | except: |
| 555 | if not is_reload and fullname in sys.modules: |
| 556 | del sys.modules[fullname] |
| 557 | raise |
| 558 | |
| 559 | @_check_name |
| 560 | def is_package(self, fullname): |
| 561 | """Return False as an extension module can never be a package.""" |
| 562 | return False |
| 563 | |
| 564 | @_check_name |
| 565 | def get_code(self, fullname): |
| 566 | """Return None as an extension module cannot create a code object.""" |
| 567 | return None |
| 568 | |
| 569 | @_check_name |
| 570 | def get_source(self, fullname): |
| 571 | """Return None as extension modules have no source code.""" |
| 572 | return None |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 573 | |
| 574 | |
Brett Cannon | e9103d2 | 2009-03-12 22:37:06 +0000 | [diff] [blame] | 575 | # Finders ##################################################################### |
Brett Cannon | 4afab6b | 2009-02-21 03:31:35 +0000 | [diff] [blame] | 576 | |
Brett Cannon | f7e5a8c | 2009-02-05 02:52:18 +0000 | [diff] [blame] | 577 | class PathFinder: |
Brett Cannon | 1d37668 | 2009-02-02 19:19:36 +0000 | [diff] [blame] | 578 | |
| 579 | """Meta path finder for sys.(path|path_hooks|path_importer_cache).""" |
| 580 | |
Brett Cannon | f7e5a8c | 2009-02-05 02:52:18 +0000 | [diff] [blame] | 581 | @classmethod |
Brett Cannon | 32732e3 | 2009-02-15 05:48:13 +0000 | [diff] [blame] | 582 | def _path_hooks(cls, path, hooks=None): |
| 583 | """Search sequence of hooks for a finder for 'path'. |
Brett Cannon | 1d37668 | 2009-02-02 19:19:36 +0000 | [diff] [blame] | 584 | |
Brett Cannon | 32732e3 | 2009-02-15 05:48:13 +0000 | [diff] [blame] | 585 | If 'hooks' is false then use sys.path_hooks. |
Brett Cannon | 1d37668 | 2009-02-02 19:19:36 +0000 | [diff] [blame] | 586 | |
| 587 | """ |
Brett Cannon | 32732e3 | 2009-02-15 05:48:13 +0000 | [diff] [blame] | 588 | if not hooks: |
| 589 | hooks = sys.path_hooks |
| 590 | for hook in hooks: |
Brett Cannon | 1d37668 | 2009-02-02 19:19:36 +0000 | [diff] [blame] | 591 | try: |
| 592 | return hook(path) |
| 593 | except ImportError: |
| 594 | continue |
| 595 | else: |
Brett Cannon | 32732e3 | 2009-02-15 05:48:13 +0000 | [diff] [blame] | 596 | raise ImportError("no path hook found for {0}".format(path)) |
Brett Cannon | 1d37668 | 2009-02-02 19:19:36 +0000 | [diff] [blame] | 597 | |
Brett Cannon | f7e5a8c | 2009-02-05 02:52:18 +0000 | [diff] [blame] | 598 | @classmethod |
Brett Cannon | 32732e3 | 2009-02-15 05:48:13 +0000 | [diff] [blame] | 599 | def _path_importer_cache(cls, path, default=None): |
Brett Cannon | 1d37668 | 2009-02-02 19:19:36 +0000 | [diff] [blame] | 600 | """Get the finder for the path from sys.path_importer_cache. |
| 601 | |
| 602 | If the path is not in the cache, find the appropriate finder and cache |
| 603 | it. If None is cached, get the default finder and cache that |
| 604 | (if applicable). |
| 605 | |
| 606 | Because of NullImporter, some finder should be returned. The only |
| 607 | explicit fail case is if None is cached but the path cannot be used for |
| 608 | the default hook, for which ImportError is raised. |
| 609 | |
| 610 | """ |
| 611 | try: |
Brett Cannon | f7e5a8c | 2009-02-05 02:52:18 +0000 | [diff] [blame] | 612 | finder = sys.path_importer_cache[path] |
Brett Cannon | 1d37668 | 2009-02-02 19:19:36 +0000 | [diff] [blame] | 613 | except KeyError: |
Brett Cannon | f7e5a8c | 2009-02-05 02:52:18 +0000 | [diff] [blame] | 614 | finder = cls._path_hooks(path) |
Brett Cannon | 1d37668 | 2009-02-02 19:19:36 +0000 | [diff] [blame] | 615 | sys.path_importer_cache[path] = finder |
| 616 | else: |
Brett Cannon | 32732e3 | 2009-02-15 05:48:13 +0000 | [diff] [blame] | 617 | if finder is None and default: |
Brett Cannon | 1d37668 | 2009-02-02 19:19:36 +0000 | [diff] [blame] | 618 | # Raises ImportError on failure. |
Brett Cannon | 32732e3 | 2009-02-15 05:48:13 +0000 | [diff] [blame] | 619 | finder = default(path) |
Brett Cannon | 1d37668 | 2009-02-02 19:19:36 +0000 | [diff] [blame] | 620 | sys.path_importer_cache[path] = finder |
| 621 | return finder |
| 622 | |
Brett Cannon | f7e5a8c | 2009-02-05 02:52:18 +0000 | [diff] [blame] | 623 | @classmethod |
| 624 | def find_module(cls, fullname, path=None): |
Brett Cannon | 7aa21f7 | 2009-03-15 00:53:05 +0000 | [diff] [blame] | 625 | """Find the module on sys.path or 'path' based on sys.path_hooks and |
| 626 | sys.path_importer_cache.""" |
Brett Cannon | 1d37668 | 2009-02-02 19:19:36 +0000 | [diff] [blame] | 627 | if not path: |
| 628 | path = sys.path |
| 629 | for entry in path: |
| 630 | try: |
Brett Cannon | f7e5a8c | 2009-02-05 02:52:18 +0000 | [diff] [blame] | 631 | finder = cls._path_importer_cache(entry) |
Brett Cannon | 1d37668 | 2009-02-02 19:19:36 +0000 | [diff] [blame] | 632 | except ImportError: |
| 633 | continue |
Brett Cannon | 8593a75 | 2009-03-30 19:57:15 +0000 | [diff] [blame] | 634 | if finder: |
| 635 | loader = finder.find_module(fullname) |
| 636 | if loader: |
| 637 | return loader |
Brett Cannon | 1d37668 | 2009-02-02 19:19:36 +0000 | [diff] [blame] | 638 | else: |
| 639 | return None |
| 640 | |
| 641 | |
Brett Cannon | f87e04d | 2009-03-12 22:47:53 +0000 | [diff] [blame] | 642 | class _FileFinder: |
Brett Cannon | e9103d2 | 2009-03-12 22:37:06 +0000 | [diff] [blame] | 643 | |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 644 | """File-based finder. |
Brett Cannon | e9103d2 | 2009-03-12 22:37:06 +0000 | [diff] [blame] | 645 | |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 646 | Constructor takes a list of objects detailing what file extensions their |
| 647 | loader supports along with whether it can be used for a package. |
Brett Cannon | e9103d2 | 2009-03-12 22:37:06 +0000 | [diff] [blame] | 648 | |
| 649 | """ |
| 650 | |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 651 | def __init__(self, path, *details): |
| 652 | """Initialize with finder details.""" |
| 653 | packages = [] |
| 654 | modules = [] |
| 655 | for detail in details: |
| 656 | modules.extend((suffix, detail.loader) for suffix in detail.suffixes) |
| 657 | if detail.supports_packages: |
| 658 | packages.extend((suffix, detail.loader) |
| 659 | for suffix in detail.suffixes) |
| 660 | self.packages = packages |
| 661 | self.modules = modules |
| 662 | self.path = path |
Brett Cannon | e9103d2 | 2009-03-12 22:37:06 +0000 | [diff] [blame] | 663 | |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 664 | def find_module(self, fullname): |
| 665 | """Try to find a loader for the specified module.""" |
Brett Cannon | e9103d2 | 2009-03-12 22:37:06 +0000 | [diff] [blame] | 666 | tail_module = fullname.rpartition('.')[2] |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 667 | base_path = _path_join(self.path, tail_module) |
| 668 | if _path_isdir(base_path) and _case_ok(self.path, tail_module): |
| 669 | for suffix, loader in self.packages: |
| 670 | init_filename = '__init__' + suffix |
| 671 | full_path = _path_join(base_path, init_filename) |
| 672 | if (_path_isfile(full_path) and |
| 673 | _case_ok(base_path, init_filename)): |
| 674 | return loader(fullname, full_path) |
| 675 | else: |
| 676 | msg = "Not importing directory {}: missing __init__" |
| 677 | _warnings.warn(msg.format(base_path), ImportWarning) |
| 678 | for suffix, loader in self.modules: |
| 679 | mod_filename = tail_module + suffix |
| 680 | full_path = _path_join(self.path, mod_filename) |
| 681 | if _path_isfile(full_path) and _case_ok(self.path, mod_filename): |
| 682 | return loader(fullname, full_path) |
| 683 | return None |
| 684 | |
| 685 | class _SourceFinderDetails: |
| 686 | |
| 687 | loader = _SourceFileLoader |
| 688 | supports_packages = True |
| 689 | |
| 690 | def __init__(self): |
| 691 | self.suffixes = _suffix_list(imp.PY_SOURCE) |
| 692 | |
| 693 | class _SourcelessFinderDetails: |
| 694 | |
| 695 | loader = _SourcelessFileLoader |
| 696 | supports_packages = True |
| 697 | |
| 698 | def __init__(self): |
| 699 | self.suffixes = _suffix_list(imp.PY_COMPILED) |
Brett Cannon | e9103d2 | 2009-03-12 22:37:06 +0000 | [diff] [blame] | 700 | |
| 701 | |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 702 | class _ExtensionFinderDetails: |
Brett Cannon | e9103d2 | 2009-03-12 22:37:06 +0000 | [diff] [blame] | 703 | |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 704 | loader = _ExtensionFileLoader |
| 705 | supports_packages = False |
Brett Cannon | e9103d2 | 2009-03-12 22:37:06 +0000 | [diff] [blame] | 706 | |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 707 | def __init__(self): |
| 708 | self.suffixes = _suffix_list(imp.C_EXTENSION) |
Brett Cannon | e9103d2 | 2009-03-12 22:37:06 +0000 | [diff] [blame] | 709 | |
| 710 | |
Brett Cannon | ce43ddf | 2009-03-12 22:28:55 +0000 | [diff] [blame] | 711 | # Import itself ############################################################### |
| 712 | |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 713 | def _file_path_hook(path): |
| 714 | """If the path is a directory, return a file-based finder.""" |
| 715 | if _path_isdir(path): |
| 716 | return _FileFinder(path, _ExtensionFinderDetails(), |
| 717 | _SourceFinderDetails(), |
| 718 | _SourcelessFinderDetails()) |
| 719 | else: |
| 720 | raise ImportError("only directories are supported") |
Brett Cannon | ce43ddf | 2009-03-12 22:28:55 +0000 | [diff] [blame] | 721 | |
| 722 | |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 723 | _DEFAULT_PATH_HOOK = _file_path_hook |
Brett Cannon | 2dee597 | 2009-02-21 03:15:37 +0000 | [diff] [blame] | 724 | |
Brett Cannon | 32732e3 | 2009-02-15 05:48:13 +0000 | [diff] [blame] | 725 | class _DefaultPathFinder(PathFinder): |
| 726 | |
| 727 | """Subclass of PathFinder that implements implicit semantics for |
| 728 | __import__.""" |
| 729 | |
Brett Cannon | 32732e3 | 2009-02-15 05:48:13 +0000 | [diff] [blame] | 730 | @classmethod |
| 731 | def _path_hooks(cls, path): |
| 732 | """Search sys.path_hooks as well as implicit path hooks.""" |
| 733 | try: |
| 734 | return super()._path_hooks(path) |
| 735 | except ImportError: |
Brett Cannon | 2dee597 | 2009-02-21 03:15:37 +0000 | [diff] [blame] | 736 | implicit_hooks = [_DEFAULT_PATH_HOOK, imp.NullImporter] |
Brett Cannon | 32732e3 | 2009-02-15 05:48:13 +0000 | [diff] [blame] | 737 | return super()._path_hooks(path, implicit_hooks) |
| 738 | |
| 739 | @classmethod |
| 740 | def _path_importer_cache(cls, path): |
| 741 | """Use the default path hook when None is stored in |
| 742 | sys.path_importer_cache.""" |
Brett Cannon | 2dee597 | 2009-02-21 03:15:37 +0000 | [diff] [blame] | 743 | return super()._path_importer_cache(path, _DEFAULT_PATH_HOOK) |
Brett Cannon | 32732e3 | 2009-02-15 05:48:13 +0000 | [diff] [blame] | 744 | |
| 745 | |
Brett Cannon | e9103d2 | 2009-03-12 22:37:06 +0000 | [diff] [blame] | 746 | class _ImportLockContext: |
| 747 | |
| 748 | """Context manager for the import lock.""" |
| 749 | |
| 750 | def __enter__(self): |
| 751 | """Acquire the import lock.""" |
| 752 | imp.acquire_lock() |
| 753 | |
| 754 | def __exit__(self, exc_type, exc_value, exc_traceback): |
| 755 | """Release the import lock regardless of any raised exceptions.""" |
| 756 | imp.release_lock() |
| 757 | |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 758 | |
Brett Cannon | 32732e3 | 2009-02-15 05:48:13 +0000 | [diff] [blame] | 759 | _IMPLICIT_META_PATH = [BuiltinImporter, FrozenImporter, _DefaultPathFinder] |
| 760 | |
Brett Cannon | 0ffe6a9 | 2010-11-18 03:03:04 +0000 | [diff] [blame] | 761 | _ERR_MSG = 'No module named {}' |
| 762 | |
Brett Cannon | 7f9876c | 2009-02-06 02:47:33 +0000 | [diff] [blame] | 763 | def _gcd_import(name, package=None, level=0): |
| 764 | """Import and return the module based on its name, the package the call is |
| 765 | being made from, and the level adjustment. |
| 766 | |
| 767 | This function represents the greatest common denominator of functionality |
Brett Cannon | 2c318a1 | 2009-02-07 01:15:27 +0000 | [diff] [blame] | 768 | between import_module and __import__. This includes settting __package__ if |
| 769 | the loader did not. |
| 770 | |
Brett Cannon | 7f9876c | 2009-02-06 02:47:33 +0000 | [diff] [blame] | 771 | """ |
Brett Cannon | 2c318a1 | 2009-02-07 01:15:27 +0000 | [diff] [blame] | 772 | if package: |
| 773 | if not hasattr(package, 'rindex'): |
| 774 | raise ValueError("__package__ not set to a string") |
| 775 | elif package not in sys.modules: |
| 776 | msg = ("Parent module {0!r} not loaded, cannot perform relative " |
| 777 | "import") |
| 778 | raise SystemError(msg.format(package)) |
| 779 | if not name and level == 0: |
| 780 | raise ValueError("Empty module name") |
Brett Cannon | 7f9876c | 2009-02-06 02:47:33 +0000 | [diff] [blame] | 781 | if level > 0: |
Brett Cannon | 2c318a1 | 2009-02-07 01:15:27 +0000 | [diff] [blame] | 782 | dot = len(package) |
Brett Cannon | 7f9876c | 2009-02-06 02:47:33 +0000 | [diff] [blame] | 783 | for x in range(level, 1, -1): |
| 784 | try: |
| 785 | dot = package.rindex('.', 0, dot) |
Brett Cannon | 7f9876c | 2009-02-06 02:47:33 +0000 | [diff] [blame] | 786 | except ValueError: |
Brett Cannon | 2c318a1 | 2009-02-07 01:15:27 +0000 | [diff] [blame] | 787 | raise ValueError("attempted relative import beyond " |
| 788 | "top-level package") |
| 789 | if name: |
| 790 | name = "{0}.{1}".format(package[:dot], name) |
| 791 | else: |
| 792 | name = package[:dot] |
Brett Cannon | 3eeaa0a | 2009-03-12 22:07:17 +0000 | [diff] [blame] | 793 | with _ImportLockContext(): |
Brett Cannon | 7f9876c | 2009-02-06 02:47:33 +0000 | [diff] [blame] | 794 | try: |
Brett Cannon | 4d75fc1 | 2009-08-30 03:47:36 +0000 | [diff] [blame] | 795 | module = sys.modules[name] |
| 796 | if module is None: |
| 797 | message = ("import of {} halted; " |
| 798 | "None in sys.modules".format(name)) |
| 799 | raise ImportError(message) |
| 800 | return module |
Brett Cannon | 7f9876c | 2009-02-06 02:47:33 +0000 | [diff] [blame] | 801 | except KeyError: |
| 802 | pass |
| 803 | parent = name.rpartition('.')[0] |
| 804 | path = None |
| 805 | if parent: |
| 806 | if parent not in sys.modules: |
Brett Cannon | 2c318a1 | 2009-02-07 01:15:27 +0000 | [diff] [blame] | 807 | _gcd_import(parent) |
| 808 | # Backwards-compatibility; be nicer to skip the dict lookup. |
| 809 | parent_module = sys.modules[parent] |
Brett Cannon | 1c1dcbf | 2009-08-30 20:22:21 +0000 | [diff] [blame] | 810 | try: |
| 811 | path = parent_module.__path__ |
| 812 | except AttributeError: |
Brett Cannon | 0ffe6a9 | 2010-11-18 03:03:04 +0000 | [diff] [blame] | 813 | msg = (_ERR_MSG + '; {} is not a package').format(name, parent) |
| 814 | raise ImportError(msg) |
Brett Cannon | 32732e3 | 2009-02-15 05:48:13 +0000 | [diff] [blame] | 815 | meta_path = sys.meta_path + _IMPLICIT_META_PATH |
Brett Cannon | 2c318a1 | 2009-02-07 01:15:27 +0000 | [diff] [blame] | 816 | for finder in meta_path: |
Brett Cannon | 7f9876c | 2009-02-06 02:47:33 +0000 | [diff] [blame] | 817 | loader = finder.find_module(name, path) |
Brett Cannon | 2c318a1 | 2009-02-07 01:15:27 +0000 | [diff] [blame] | 818 | if loader is not None: |
| 819 | loader.load_module(name) |
| 820 | break |
Brett Cannon | 7f9876c | 2009-02-06 02:47:33 +0000 | [diff] [blame] | 821 | else: |
Brett Cannon | 0ffe6a9 | 2010-11-18 03:03:04 +0000 | [diff] [blame] | 822 | raise ImportError(_ERR_MSG.format(name)) |
Brett Cannon | 2c318a1 | 2009-02-07 01:15:27 +0000 | [diff] [blame] | 823 | # Backwards-compatibility; be nicer to skip the dict lookup. |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 824 | module = sys.modules[name] |
| 825 | if parent: |
Brett Cannon | 2c318a1 | 2009-02-07 01:15:27 +0000 | [diff] [blame] | 826 | # Set the module as an attribute on its parent. |
| 827 | setattr(parent_module, name.rpartition('.')[2], module) |
| 828 | # Set __package__ if the loader did not. |
| 829 | if not hasattr(module, '__package__') or module.__package__ is None: |
| 830 | # Watch out for what comes out of sys.modules to not be a module, |
| 831 | # e.g. an int. |
| 832 | try: |
| 833 | module.__package__ = module.__name__ |
| 834 | if not hasattr(module, '__path__'): |
| 835 | module.__package__ = module.__package__.rpartition('.')[0] |
| 836 | except AttributeError: |
| 837 | pass |
| 838 | return module |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 839 | |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 840 | |
Brett Cannon | 7aa21f7 | 2009-03-15 00:53:05 +0000 | [diff] [blame] | 841 | def __import__(name, globals={}, locals={}, fromlist=[], level=0): |
Brett Cannon | 2c318a1 | 2009-02-07 01:15:27 +0000 | [diff] [blame] | 842 | """Import a module. |
| 843 | |
| 844 | The 'globals' argument is used to infer where the import is occuring from |
| 845 | to handle relative imports. The 'locals' argument is ignored. The |
| 846 | 'fromlist' argument specifies what should exist as attributes on the module |
| 847 | being imported (e.g. ``from module import <fromlist>``). The 'level' |
| 848 | argument represents the package location to import from in a relative |
| 849 | import (e.g. ``from ..pkg import mod`` would have a 'level' of 2). |
| 850 | |
| 851 | """ |
Brett Cannon | 6afbaef | 2009-08-30 19:08:58 +0000 | [diff] [blame] | 852 | if not hasattr(name, 'rpartition'): |
| 853 | raise TypeError("module name must be str, not {}".format(type(name))) |
Brett Cannon | 2c318a1 | 2009-02-07 01:15:27 +0000 | [diff] [blame] | 854 | if level == 0: |
| 855 | module = _gcd_import(name) |
| 856 | else: |
Brett Cannon | de4ebfe | 2009-08-30 19:53:48 +0000 | [diff] [blame] | 857 | # __package__ is not guaranteed to be defined or could be set to None |
| 858 | # to represent that it's proper value is unknown |
| 859 | package = globals.get('__package__') |
| 860 | if package is None: |
Brett Cannon | 2c318a1 | 2009-02-07 01:15:27 +0000 | [diff] [blame] | 861 | package = globals['__name__'] |
| 862 | if '__path__' not in globals: |
| 863 | package = package.rpartition('.')[0] |
| 864 | module = _gcd_import(name, package, level) |
| 865 | # The hell that is fromlist ... |
| 866 | if not fromlist: |
| 867 | # Return up to the first dot in 'name'. This is complicated by the fact |
| 868 | # that 'name' may be relative. |
| 869 | if level == 0: |
| 870 | return sys.modules[name.partition('.')[0]] |
| 871 | elif not name: |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 872 | return module |
Brett Cannon | 2c318a1 | 2009-02-07 01:15:27 +0000 | [diff] [blame] | 873 | else: |
| 874 | cut_off = len(name) - len(name.partition('.')[0]) |
| 875 | return sys.modules[module.__name__[:-cut_off]] |
| 876 | else: |
| 877 | # If a package was imported, try to import stuff from fromlist. |
| 878 | if hasattr(module, '__path__'): |
| 879 | if '*' in fromlist and hasattr(module, '__all__'): |
Brett Cannon | 9e0e1a6 | 2009-08-30 18:28:46 +0000 | [diff] [blame] | 880 | fromlist = list(fromlist) |
Brett Cannon | 2c318a1 | 2009-02-07 01:15:27 +0000 | [diff] [blame] | 881 | fromlist.remove('*') |
| 882 | fromlist.extend(module.__all__) |
| 883 | for x in (y for y in fromlist if not hasattr(module,y)): |
| 884 | try: |
| 885 | _gcd_import('{0}.{1}'.format(module.__name__, x)) |
| 886 | except ImportError: |
| 887 | pass |
| 888 | return module |