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 |
| 25 | for x in args) |
| 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.""" |
| 56 | return _path_is_mode_type(path, 0o040000) |
| 57 | |
| 58 | |
| 59 | def _path_without_ext(path, ext_type): |
| 60 | """Replacement for os.path.splitext()[0].""" |
Brett Cannon | 3eeaa0a | 2009-03-12 22:07:17 +0000 | [diff] [blame] | 61 | for suffix in _suffix_list(ext_type): |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 62 | if path.endswith(suffix): |
| 63 | return path[:-len(suffix)] |
| 64 | else: |
| 65 | raise ValueError("path is not of the specified type") |
| 66 | |
| 67 | |
| 68 | def _path_absolute(path): |
| 69 | """Replacement for os.path.abspath.""" |
| 70 | if not path: |
| 71 | path = _os.getcwd() |
| 72 | try: |
| 73 | return _os._getfullpathname(path) |
| 74 | except AttributeError: |
| 75 | if path.startswith('/'): |
| 76 | return path |
| 77 | else: |
| 78 | return _path_join(_os.getcwd(), path) |
| 79 | |
| 80 | |
Brett Cannon | 3eeaa0a | 2009-03-12 22:07:17 +0000 | [diff] [blame] | 81 | class _closing: |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 82 | |
| 83 | """Simple replacement for contextlib.closing.""" |
| 84 | |
| 85 | def __init__(self, obj): |
| 86 | self.obj = obj |
| 87 | |
| 88 | def __enter__(self): |
| 89 | return self.obj |
| 90 | |
| 91 | def __exit__(self, *args): |
| 92 | self.obj.close() |
| 93 | |
| 94 | |
Brett Cannon | 3eeaa0a | 2009-03-12 22:07:17 +0000 | [diff] [blame] | 95 | def _wrap(new, old): |
Brett Cannon | 51d8bfc | 2009-02-07 02:13:28 +0000 | [diff] [blame] | 96 | """Simple substitute for functools.wraps.""" |
| 97 | for replace in ['__module__', '__name__', '__doc__']: |
| 98 | setattr(new, replace, getattr(old, replace)) |
| 99 | new.__dict__.update(old.__dict__) |
| 100 | |
| 101 | |
Brett Cannon | ce43ddf | 2009-03-12 22:28:55 +0000 | [diff] [blame] | 102 | # Finder/loader utility code ################################################## |
| 103 | |
Brett Cannon | 435aad8 | 2009-03-04 16:07:00 +0000 | [diff] [blame] | 104 | def set_package(fxn): |
Brett Cannon | 06c9d96 | 2009-02-07 01:52:25 +0000 | [diff] [blame] | 105 | """Set __package__ on the returned module.""" |
| 106 | def wrapper(*args, **kwargs): |
| 107 | module = fxn(*args, **kwargs) |
| 108 | if not hasattr(module, '__package__') or module.__package__ is None: |
| 109 | module.__package__ = module.__name__ |
| 110 | if not hasattr(module, '__path__'): |
| 111 | module.__package__ = module.__package__.rpartition('.')[0] |
| 112 | return module |
Brett Cannon | 3eeaa0a | 2009-03-12 22:07:17 +0000 | [diff] [blame] | 113 | _wrap(wrapper, fxn) |
Brett Cannon | 06c9d96 | 2009-02-07 01:52:25 +0000 | [diff] [blame] | 114 | return wrapper |
| 115 | |
| 116 | |
Brett Cannon | 2cf03a8 | 2009-03-10 05:17:37 +0000 | [diff] [blame] | 117 | def set_loader(fxn): |
| 118 | """Set __loader__ on the returned module.""" |
| 119 | def wrapper(self, *args, **kwargs): |
| 120 | module = fxn(self, *args, **kwargs) |
| 121 | if not hasattr(module, '__loader__'): |
| 122 | module.__loader__ = self |
| 123 | return module |
Brett Cannon | 3eeaa0a | 2009-03-12 22:07:17 +0000 | [diff] [blame] | 124 | _wrap(wrapper, fxn) |
Brett Cannon | 2cf03a8 | 2009-03-10 05:17:37 +0000 | [diff] [blame] | 125 | return wrapper |
| 126 | |
| 127 | |
Brett Cannon | ce43ddf | 2009-03-12 22:28:55 +0000 | [diff] [blame] | 128 | def module_for_loader(fxn): |
| 129 | """Decorator to handle selecting the proper module for loaders. |
| 130 | |
Brett Cannon | 0e0d8a6 | 2009-03-15 00:00:19 +0000 | [diff] [blame] | 131 | The decorated function is passed the module to use instead of the module |
| 132 | name. The module passed in to the function is either from sys.modules if |
| 133 | it already exists or is a new module which has __name__ set and is inserted |
| 134 | into sys.modules. If an exception is raised and the decorator created the |
| 135 | module it is subsequently removed from sys.modules. |
Brett Cannon | ce43ddf | 2009-03-12 22:28:55 +0000 | [diff] [blame] | 136 | |
Brett Cannon | 0e0d8a6 | 2009-03-15 00:00:19 +0000 | [diff] [blame] | 137 | The decorator assumes that the decorated function takes the module name as |
| 138 | the second argument. |
Brett Cannon | ce43ddf | 2009-03-12 22:28:55 +0000 | [diff] [blame] | 139 | |
| 140 | """ |
| 141 | def decorated(self, fullname): |
| 142 | module = sys.modules.get(fullname) |
| 143 | is_reload = bool(module) |
| 144 | if not is_reload: |
| 145 | # This must be done before open() is called as the 'io' module |
| 146 | # implicitly imports 'locale' and would otherwise trigger an |
| 147 | # infinite loop. |
| 148 | module = imp.new_module(fullname) |
| 149 | sys.modules[fullname] = module |
| 150 | try: |
| 151 | return fxn(self, module) |
| 152 | except: |
| 153 | if not is_reload: |
| 154 | del sys.modules[fullname] |
| 155 | raise |
| 156 | _wrap(decorated, fxn) |
| 157 | return decorated |
| 158 | |
| 159 | |
| 160 | def _check_name(method): |
| 161 | """Decorator to verify that the module being requested matches the one the |
| 162 | loader can handle. |
| 163 | |
| 164 | The first argument (self) must define _name which the second argument is |
| 165 | comapred against. If the comparison fails then ImportError is raised. |
| 166 | |
| 167 | """ |
| 168 | def inner(self, name, *args, **kwargs): |
| 169 | if self._name != name: |
| 170 | raise ImportError("loader cannot handle %s" % name) |
| 171 | return method(self, name, *args, **kwargs) |
| 172 | _wrap(inner, method) |
| 173 | return inner |
| 174 | |
| 175 | |
Brett Cannon | a113ac5 | 2009-03-15 01:41:33 +0000 | [diff] [blame] | 176 | def _requires_builtin(fxn): |
| 177 | """Decorator to verify the named module is built-in.""" |
| 178 | def wrapper(self, fullname): |
| 179 | if fullname not in sys.builtin_module_names: |
| 180 | raise ImportError("{0} is not a built-in module".format(fullname)) |
| 181 | return fxn(self, fullname) |
| 182 | _wrap(wrapper, fxn) |
| 183 | return wrapper |
| 184 | |
| 185 | |
Brett Cannon | 8d11013 | 2009-03-15 02:20:16 +0000 | [diff] [blame^] | 186 | def _requires_frozen(fxn): |
| 187 | """Decorator to verify the named module is frozen.""" |
| 188 | def wrapper(self, fullname): |
| 189 | if not imp.is_frozen(fullname): |
| 190 | raise ImportError("{0} is not a frozen module".format(fullname)) |
| 191 | return fxn(self, fullname) |
| 192 | _wrap(wrapper, fxn) |
| 193 | return wrapper |
| 194 | |
| 195 | |
Brett Cannon | e9103d2 | 2009-03-12 22:37:06 +0000 | [diff] [blame] | 196 | def _suffix_list(suffix_type): |
| 197 | """Return a list of file suffixes based on the imp file type.""" |
| 198 | return [suffix[0] for suffix in imp.get_suffixes() |
| 199 | if suffix[2] == suffix_type] |
| 200 | |
| 201 | |
| 202 | # Loaders ##################################################################### |
Brett Cannon | ce43ddf | 2009-03-12 22:28:55 +0000 | [diff] [blame] | 203 | |
Brett Cannon | 5abdc93 | 2009-01-22 22:43:07 +0000 | [diff] [blame] | 204 | class BuiltinImporter: |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 205 | |
Brett Cannon | 7aa21f7 | 2009-03-15 00:53:05 +0000 | [diff] [blame] | 206 | """Meta path import for built-in modules. |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 207 | |
Brett Cannon | 7aa21f7 | 2009-03-15 00:53:05 +0000 | [diff] [blame] | 208 | All methods are either class or static methods to avoid the need to |
| 209 | instantiate the class. |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 210 | |
| 211 | """ |
| 212 | |
Brett Cannon | 5abdc93 | 2009-01-22 22:43:07 +0000 | [diff] [blame] | 213 | @classmethod |
| 214 | def find_module(cls, fullname, path=None): |
Brett Cannon | 7aa21f7 | 2009-03-15 00:53:05 +0000 | [diff] [blame] | 215 | """Find the built-in module. |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 216 | |
| 217 | If 'path' is ever specified then the search is considered a failure. |
| 218 | |
| 219 | """ |
| 220 | if path is not None: |
| 221 | return None |
Brett Cannon | 5abdc93 | 2009-01-22 22:43:07 +0000 | [diff] [blame] | 222 | return cls if imp.is_builtin(fullname) else None |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 223 | |
Brett Cannon | 78246b6 | 2009-01-25 04:56:30 +0000 | [diff] [blame] | 224 | @classmethod |
Brett Cannon | 435aad8 | 2009-03-04 16:07:00 +0000 | [diff] [blame] | 225 | @set_package |
Brett Cannon | 2cf03a8 | 2009-03-10 05:17:37 +0000 | [diff] [blame] | 226 | @set_loader |
Brett Cannon | a113ac5 | 2009-03-15 01:41:33 +0000 | [diff] [blame] | 227 | @_requires_builtin |
Brett Cannon | 78246b6 | 2009-01-25 04:56:30 +0000 | [diff] [blame] | 228 | def load_module(cls, fullname): |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 229 | """Load a built-in module.""" |
Brett Cannon | d2e7b33 | 2009-02-17 02:45:03 +0000 | [diff] [blame] | 230 | is_reload = fullname in sys.modules |
| 231 | try: |
| 232 | return imp.init_builtin(fullname) |
| 233 | except: |
| 234 | if not is_reload and fullname in sys.modules: |
| 235 | del sys.modules[fullname] |
| 236 | raise |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 237 | |
Brett Cannon | a113ac5 | 2009-03-15 01:41:33 +0000 | [diff] [blame] | 238 | @classmethod |
| 239 | @_requires_builtin |
| 240 | def get_code(cls, fullname): |
| 241 | """Return None as built-in modules do not have code objects.""" |
| 242 | return None |
| 243 | |
| 244 | @classmethod |
| 245 | @_requires_builtin |
| 246 | def get_source(cls, fullname): |
| 247 | """Return None as built-in modules do not have source code.""" |
| 248 | return None |
| 249 | |
| 250 | @classmethod |
| 251 | @_requires_builtin |
| 252 | def is_package(cls, fullname): |
| 253 | """Return None as built-in module are never packages.""" |
| 254 | return False |
| 255 | |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 256 | |
Brett Cannon | 5abdc93 | 2009-01-22 22:43:07 +0000 | [diff] [blame] | 257 | class FrozenImporter: |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 258 | |
Brett Cannon | 7aa21f7 | 2009-03-15 00:53:05 +0000 | [diff] [blame] | 259 | """Meta path import for frozen modules. |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 260 | |
Brett Cannon | 7aa21f7 | 2009-03-15 00:53:05 +0000 | [diff] [blame] | 261 | All methods are either class or static methods to avoid the need to |
| 262 | instantiate the class. |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 263 | |
Brett Cannon | 5abdc93 | 2009-01-22 22:43:07 +0000 | [diff] [blame] | 264 | """ |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 265 | |
Brett Cannon | 5abdc93 | 2009-01-22 22:43:07 +0000 | [diff] [blame] | 266 | @classmethod |
| 267 | def find_module(cls, fullname, path=None): |
| 268 | """Find a frozen module.""" |
| 269 | return cls if imp.is_frozen(fullname) else None |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 270 | |
Brett Cannon | 5abdc93 | 2009-01-22 22:43:07 +0000 | [diff] [blame] | 271 | @classmethod |
Brett Cannon | 435aad8 | 2009-03-04 16:07:00 +0000 | [diff] [blame] | 272 | @set_package |
Brett Cannon | 2cf03a8 | 2009-03-10 05:17:37 +0000 | [diff] [blame] | 273 | @set_loader |
Brett Cannon | 8d11013 | 2009-03-15 02:20:16 +0000 | [diff] [blame^] | 274 | @_requires_frozen |
Brett Cannon | 5abdc93 | 2009-01-22 22:43:07 +0000 | [diff] [blame] | 275 | def load_module(cls, fullname): |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 276 | """Load a frozen module.""" |
Brett Cannon | d2e7b33 | 2009-02-17 02:45:03 +0000 | [diff] [blame] | 277 | is_reload = fullname in sys.modules |
| 278 | try: |
| 279 | return imp.init_frozen(fullname) |
| 280 | except: |
| 281 | if not is_reload and fullname in sys.modules: |
| 282 | del sys.modules[fullname] |
| 283 | raise |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 284 | |
Brett Cannon | 8d11013 | 2009-03-15 02:20:16 +0000 | [diff] [blame^] | 285 | @classmethod |
| 286 | @_requires_frozen |
| 287 | def get_code(cls, fullname): |
| 288 | """Return the code object for the frozen module.""" |
| 289 | return imp.get_frozen_object(fullname) |
| 290 | |
| 291 | @classmethod |
| 292 | @_requires_frozen |
| 293 | def get_source(cls, fullname): |
| 294 | """Return None as frozen modules do not have source code.""" |
| 295 | return None |
| 296 | |
| 297 | @classmethod |
| 298 | @_requires_frozen |
| 299 | def is_package(cls, fullname): |
| 300 | """Return if the frozen module is a package.""" |
| 301 | return imp.is_frozen_package(fullname) |
| 302 | |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 303 | |
Brett Cannon | 91cf882 | 2009-02-21 05:41:15 +0000 | [diff] [blame] | 304 | class PyLoader: |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 305 | |
Brett Cannon | 7aa21f7 | 2009-03-15 00:53:05 +0000 | [diff] [blame] | 306 | """Loader base class for Python source code. |
Brett Cannon | 91cf882 | 2009-02-21 05:41:15 +0000 | [diff] [blame] | 307 | |
Brett Cannon | 7aa21f7 | 2009-03-15 00:53:05 +0000 | [diff] [blame] | 308 | Subclasses need to implement the methods: |
| 309 | |
| 310 | - source_path |
| 311 | - get_data |
| 312 | - is_package |
Brett Cannon | 91cf882 | 2009-02-21 05:41:15 +0000 | [diff] [blame] | 313 | |
| 314 | """ |
| 315 | |
| 316 | @module_for_loader |
| 317 | def load_module(self, module): |
| 318 | """Load a source module.""" |
Brett Cannon | 1014d42 | 2009-03-08 20:53:50 +0000 | [diff] [blame] | 319 | return self._load_module(module) |
Brett Cannon | 91cf882 | 2009-02-21 05:41:15 +0000 | [diff] [blame] | 320 | |
| 321 | def _load_module(self, module): |
| 322 | """Initialize a module from source.""" |
| 323 | name = module.__name__ |
Brett Cannon | 91cf882 | 2009-02-21 05:41:15 +0000 | [diff] [blame] | 324 | code_object = self.get_code(module.__name__) |
Brett Cannon | 1014d42 | 2009-03-08 20:53:50 +0000 | [diff] [blame] | 325 | # __file__ may have been set by the caller, e.g. bytecode path. |
Brett Cannon | 91cf882 | 2009-02-21 05:41:15 +0000 | [diff] [blame] | 326 | if not hasattr(module, '__file__'): |
Brett Cannon | 1014d42 | 2009-03-08 20:53:50 +0000 | [diff] [blame] | 327 | module.__file__ = self.source_path(name) |
Brett Cannon | 91cf882 | 2009-02-21 05:41:15 +0000 | [diff] [blame] | 328 | if self.is_package(name): |
| 329 | module.__path__ = [module.__file__.rsplit(path_sep, 1)[0]] |
| 330 | module.__package__ = module.__name__ |
| 331 | if not hasattr(module, '__path__'): |
| 332 | module.__package__ = module.__package__.rpartition('.')[0] |
Brett Cannon | 1014d42 | 2009-03-08 20:53:50 +0000 | [diff] [blame] | 333 | module.__loader__ = self |
Brett Cannon | 91cf882 | 2009-02-21 05:41:15 +0000 | [diff] [blame] | 334 | exec(code_object, module.__dict__) |
| 335 | return module |
| 336 | |
| 337 | def get_code(self, fullname): |
| 338 | """Get a code object from source.""" |
| 339 | source_path = self.source_path(fullname) |
Brett Cannon | 1014d42 | 2009-03-08 20:53:50 +0000 | [diff] [blame] | 340 | if source_path is None: |
| 341 | message = "a source path must exist to load {0}".format(fullname) |
| 342 | raise ImportError(message) |
Brett Cannon | 91cf882 | 2009-02-21 05:41:15 +0000 | [diff] [blame] | 343 | source = self.get_data(source_path) |
| 344 | # Convert to universal newlines. |
| 345 | line_endings = b'\n' |
| 346 | for index, c in enumerate(source): |
| 347 | if c == ord(b'\n'): |
| 348 | break |
| 349 | elif c == ord(b'\r'): |
| 350 | line_endings = b'\r' |
| 351 | try: |
| 352 | if source[index+1] == ord(b'\n'): |
| 353 | line_endings += b'\n' |
| 354 | except IndexError: |
| 355 | pass |
| 356 | break |
| 357 | if line_endings != b'\n': |
| 358 | source = source.replace(line_endings, b'\n') |
| 359 | return compile(source, source_path, 'exec', dont_inherit=True) |
| 360 | |
Brett Cannon | d43b30b | 2009-03-10 03:29:23 +0000 | [diff] [blame] | 361 | # Never use in implementing import! Imports code within the method. |
| 362 | def get_source(self, fullname): |
| 363 | """Return the source code for a module. |
| 364 | |
| 365 | self.source_path() and self.get_data() are used to implement this |
| 366 | method. |
| 367 | |
| 368 | """ |
| 369 | path = self.source_path(fullname) |
| 370 | if path is None: |
| 371 | return None |
| 372 | try: |
| 373 | source_bytes = self.get_data(path) |
| 374 | except IOError: |
| 375 | return ImportError("source not available through get_data()") |
| 376 | import io |
| 377 | import tokenize |
| 378 | encoding = tokenize.detect_encoding(io.BytesIO(source_bytes).readline) |
| 379 | return source_bytes.decode(encoding[0]) |
| 380 | |
Brett Cannon | 91cf882 | 2009-02-21 05:41:15 +0000 | [diff] [blame] | 381 | |
| 382 | class PyPycLoader(PyLoader): |
| 383 | |
| 384 | """Loader base class for Python source and bytecode. |
| 385 | |
| 386 | Requires implementing the methods needed for PyLoader as well as |
Brett Cannon | 94aaf9e | 2009-02-21 23:12:24 +0000 | [diff] [blame] | 387 | source_mtime, bytecode_path, and write_bytecode. |
Brett Cannon | 91cf882 | 2009-02-21 05:41:15 +0000 | [diff] [blame] | 388 | |
| 389 | """ |
| 390 | |
| 391 | @module_for_loader |
| 392 | def load_module(self, module): |
| 393 | """Load a module from source or bytecode.""" |
| 394 | name = module.__name__ |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 395 | source_path = self.source_path(name) |
| 396 | bytecode_path = self.bytecode_path(name) |
Brett Cannon | 29dff8a | 2009-03-09 00:14:37 +0000 | [diff] [blame] | 397 | # get_code can worry about no viable paths existing. |
| 398 | module.__file__ = source_path or bytecode_path |
Brett Cannon | 91cf882 | 2009-02-21 05:41:15 +0000 | [diff] [blame] | 399 | return self._load_module(module) |
| 400 | |
| 401 | def get_code(self, fullname): |
| 402 | """Get a code object from source or bytecode.""" |
| 403 | # XXX Care enough to make sure this call does not happen if the magic |
| 404 | # number is bad? |
| 405 | source_timestamp = self.source_mtime(fullname) |
| 406 | # Try to use bytecode if it is available. |
| 407 | bytecode_path = self.bytecode_path(fullname) |
| 408 | if bytecode_path: |
| 409 | data = self.get_data(bytecode_path) |
| 410 | magic = data[:4] |
| 411 | pyc_timestamp = marshal._r_long(data[4:8]) |
| 412 | bytecode = data[8:] |
| 413 | try: |
| 414 | # Verify that the magic number is valid. |
| 415 | if imp.get_magic() != magic: |
| 416 | raise ImportError("bad magic number") |
| 417 | # Verify that the bytecode is not stale (only matters when |
| 418 | # there is source to fall back on. |
| 419 | if source_timestamp: |
| 420 | if pyc_timestamp < source_timestamp: |
| 421 | raise ImportError("bytecode is stale") |
| 422 | except ImportError: |
| 423 | # If source is available give it a shot. |
| 424 | if source_timestamp is not None: |
| 425 | pass |
| 426 | else: |
| 427 | raise |
| 428 | else: |
| 429 | # Bytecode seems fine, so try to use it. |
| 430 | # XXX If the bytecode is ill-formed, would it be beneficial to |
| 431 | # try for using source if available and issue a warning? |
| 432 | return marshal.loads(bytecode) |
| 433 | elif source_timestamp is None: |
| 434 | raise ImportError("no source or bytecode available to create code " |
| 435 | "object for {0!r}".format(fullname)) |
| 436 | # Use the source. |
| 437 | code_object = super().get_code(fullname) |
| 438 | # Generate bytecode and write it out. |
| 439 | if not sys.dont_write_bytecode: |
| 440 | data = bytearray(imp.get_magic()) |
| 441 | data.extend(marshal._w_long(source_timestamp)) |
| 442 | data.extend(marshal.dumps(code_object)) |
| 443 | self.write_bytecode(fullname, data) |
| 444 | return code_object |
| 445 | |
| 446 | |
Brett Cannon | f87e04d | 2009-03-12 22:47:53 +0000 | [diff] [blame] | 447 | class _PyFileLoader(PyLoader): |
Brett Cannon | 91cf882 | 2009-02-21 05:41:15 +0000 | [diff] [blame] | 448 | |
| 449 | """Load a Python source file.""" |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 450 | |
| 451 | def __init__(self, name, path, is_pkg): |
| 452 | self._name = name |
| 453 | self._is_pkg = is_pkg |
| 454 | # Figure out the base path based on whether it was source or bytecode |
| 455 | # that was found. |
| 456 | try: |
| 457 | self._base_path = _path_without_ext(path, imp.PY_SOURCE) |
| 458 | except ValueError: |
| 459 | self._base_path = _path_without_ext(path, imp.PY_COMPILED) |
| 460 | |
| 461 | def _find_path(self, ext_type): |
| 462 | """Find a path from the base path and the specified extension type that |
| 463 | exists, returning None if one is not found.""" |
Brett Cannon | 3eeaa0a | 2009-03-12 22:07:17 +0000 | [diff] [blame] | 464 | for suffix in _suffix_list(ext_type): |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 465 | path = self._base_path + suffix |
| 466 | if _path_exists(path): |
| 467 | return path |
| 468 | else: |
| 469 | return None |
| 470 | |
Brett Cannon | 3eeaa0a | 2009-03-12 22:07:17 +0000 | [diff] [blame] | 471 | @_check_name |
Brett Cannon | 51c5026 | 2009-02-01 05:33:17 +0000 | [diff] [blame] | 472 | def source_path(self, fullname): |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 473 | """Return the path to an existing source file for the module, or None |
| 474 | if one cannot be found.""" |
| 475 | # Not a property so that it is easy to override. |
| 476 | return self._find_path(imp.PY_SOURCE) |
| 477 | |
Brett Cannon | 3eeaa0a | 2009-03-12 22:07:17 +0000 | [diff] [blame] | 478 | @_check_name |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 479 | def get_source(self, fullname): |
| 480 | """Return the source for the module as a string. |
| 481 | |
| 482 | Return None if the source is not available. Raise ImportError if the |
| 483 | laoder cannot handle the specified module. |
| 484 | |
| 485 | """ |
Brett Cannon | 51c5026 | 2009-02-01 05:33:17 +0000 | [diff] [blame] | 486 | source_path = self._source_path(name) |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 487 | if source_path is None: |
| 488 | return None |
| 489 | import tokenize |
Brett Cannon | 3eeaa0a | 2009-03-12 22:07:17 +0000 | [diff] [blame] | 490 | with _closing(_io.FileIO(source_path, 'r')) as file: # Assuming bytes. |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 491 | encoding, lines = tokenize.detect_encoding(file.readline) |
| 492 | # XXX Will fail when passed to compile() if the encoding is |
| 493 | # anything other than UTF-8. |
| 494 | return open(source_path, encoding=encoding).read() |
| 495 | |
Brett Cannon | 91cf882 | 2009-02-21 05:41:15 +0000 | [diff] [blame] | 496 | |
| 497 | def get_data(self, path): |
| 498 | """Return the data from path as raw bytes.""" |
Brett Cannon | 7c9875c | 2009-03-04 01:10:09 +0000 | [diff] [blame] | 499 | return _io.FileIO(path, 'r').read() # Assuming bytes. |
Brett Cannon | 91cf882 | 2009-02-21 05:41:15 +0000 | [diff] [blame] | 500 | |
Brett Cannon | 3eeaa0a | 2009-03-12 22:07:17 +0000 | [diff] [blame] | 501 | @_check_name |
Brett Cannon | 91cf882 | 2009-02-21 05:41:15 +0000 | [diff] [blame] | 502 | def is_package(self, fullname): |
| 503 | """Return a boolean based on whether the module is a package. |
| 504 | |
| 505 | Raises ImportError (like get_source) if the loader cannot handle the |
| 506 | package. |
| 507 | |
| 508 | """ |
| 509 | return self._is_pkg |
| 510 | |
| 511 | |
Brett Cannon | f87e04d | 2009-03-12 22:47:53 +0000 | [diff] [blame] | 512 | class _PyPycFileLoader(PyPycLoader, _PyFileLoader): |
Brett Cannon | 91cf882 | 2009-02-21 05:41:15 +0000 | [diff] [blame] | 513 | |
| 514 | """Load a module from a source or bytecode file.""" |
| 515 | |
Brett Cannon | 3eeaa0a | 2009-03-12 22:07:17 +0000 | [diff] [blame] | 516 | @_check_name |
Brett Cannon | 94aaf9e | 2009-02-21 23:12:24 +0000 | [diff] [blame] | 517 | def source_mtime(self, name): |
| 518 | """Return the modification time of the source for the specified |
| 519 | module.""" |
| 520 | source_path = self.source_path(name) |
| 521 | if not source_path: |
| 522 | return None |
| 523 | return int(_os.stat(source_path).st_mtime) |
| 524 | |
Brett Cannon | 3eeaa0a | 2009-03-12 22:07:17 +0000 | [diff] [blame] | 525 | @_check_name |
Brett Cannon | 91cf882 | 2009-02-21 05:41:15 +0000 | [diff] [blame] | 526 | def bytecode_path(self, fullname): |
| 527 | """Return the path to a bytecode file, or None if one does not |
| 528 | exist.""" |
| 529 | # Not a property for easy overriding. |
| 530 | return self._find_path(imp.PY_COMPILED) |
| 531 | |
Brett Cannon | 3eeaa0a | 2009-03-12 22:07:17 +0000 | [diff] [blame] | 532 | @_check_name |
Brett Cannon | 776e701 | 2009-02-01 06:07:57 +0000 | [diff] [blame] | 533 | def write_bytecode(self, name, data): |
| 534 | """Write out 'data' for the specified module, returning a boolean |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 535 | signifying if the write-out actually occurred. |
| 536 | |
| 537 | Raises ImportError (just like get_source) if the specified module |
| 538 | cannot be handled by the loader. |
| 539 | |
| 540 | """ |
Brett Cannon | 51c5026 | 2009-02-01 05:33:17 +0000 | [diff] [blame] | 541 | bytecode_path = self.bytecode_path(name) |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 542 | if not bytecode_path: |
Brett Cannon | 3eeaa0a | 2009-03-12 22:07:17 +0000 | [diff] [blame] | 543 | bytecode_path = self._base_path + _suffix_list(imp.PY_COMPILED)[0] |
Brett Cannon | 7c9875c | 2009-03-04 01:10:09 +0000 | [diff] [blame] | 544 | file = _io.FileIO(bytecode_path, 'w') # Assuming bytes. |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 545 | try: |
Brett Cannon | 3eeaa0a | 2009-03-12 22:07:17 +0000 | [diff] [blame] | 546 | with _closing(file) as bytecode_file: |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 547 | bytecode_file.write(data) |
| 548 | return True |
| 549 | except IOError as exc: |
| 550 | if exc.errno == errno.EACCES: |
| 551 | return False |
| 552 | else: |
| 553 | raise |
| 554 | |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 555 | |
Brett Cannon | e9103d2 | 2009-03-12 22:37:06 +0000 | [diff] [blame] | 556 | class _ExtensionFileLoader: |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 557 | |
Brett Cannon | e9103d2 | 2009-03-12 22:37:06 +0000 | [diff] [blame] | 558 | """Loader for extension modules. |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 559 | |
Brett Cannon | e9103d2 | 2009-03-12 22:37:06 +0000 | [diff] [blame] | 560 | The constructor is designed to work with FileFinder. |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 561 | |
| 562 | """ |
| 563 | |
Brett Cannon | e9103d2 | 2009-03-12 22:37:06 +0000 | [diff] [blame] | 564 | def __init__(self, name, path, is_pkg): |
| 565 | """Initialize the loader. |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 566 | |
Brett Cannon | e9103d2 | 2009-03-12 22:37:06 +0000 | [diff] [blame] | 567 | If is_pkg is True then an exception is raised as extension modules |
| 568 | cannot be the __init__ module for an extension module. |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 569 | |
| 570 | """ |
Brett Cannon | e9103d2 | 2009-03-12 22:37:06 +0000 | [diff] [blame] | 571 | self._name = name |
| 572 | self._path = path |
| 573 | if is_pkg: |
| 574 | raise ValueError("extension modules cannot be packages") |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 575 | |
Brett Cannon | e9103d2 | 2009-03-12 22:37:06 +0000 | [diff] [blame] | 576 | @_check_name |
| 577 | @set_package |
| 578 | @set_loader |
| 579 | def load_module(self, fullname): |
| 580 | """Load an extension module.""" |
| 581 | is_reload = fullname in sys.modules |
| 582 | try: |
| 583 | return imp.load_dynamic(fullname, self._path) |
| 584 | except: |
| 585 | if not is_reload and fullname in sys.modules: |
| 586 | del sys.modules[fullname] |
| 587 | raise |
| 588 | |
| 589 | @_check_name |
| 590 | def is_package(self, fullname): |
| 591 | """Return False as an extension module can never be a package.""" |
| 592 | return False |
| 593 | |
| 594 | @_check_name |
| 595 | def get_code(self, fullname): |
| 596 | """Return None as an extension module cannot create a code object.""" |
| 597 | return None |
| 598 | |
| 599 | @_check_name |
| 600 | def get_source(self, fullname): |
| 601 | """Return None as extension modules have no source code.""" |
| 602 | return None |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 603 | |
| 604 | |
Brett Cannon | e9103d2 | 2009-03-12 22:37:06 +0000 | [diff] [blame] | 605 | # Finders ##################################################################### |
Brett Cannon | 4afab6b | 2009-02-21 03:31:35 +0000 | [diff] [blame] | 606 | |
Brett Cannon | f7e5a8c | 2009-02-05 02:52:18 +0000 | [diff] [blame] | 607 | class PathFinder: |
Brett Cannon | 1d37668 | 2009-02-02 19:19:36 +0000 | [diff] [blame] | 608 | |
| 609 | """Meta path finder for sys.(path|path_hooks|path_importer_cache).""" |
| 610 | |
Brett Cannon | f7e5a8c | 2009-02-05 02:52:18 +0000 | [diff] [blame] | 611 | @classmethod |
Brett Cannon | 32732e3 | 2009-02-15 05:48:13 +0000 | [diff] [blame] | 612 | def _path_hooks(cls, path, hooks=None): |
| 613 | """Search sequence of hooks for a finder for 'path'. |
Brett Cannon | 1d37668 | 2009-02-02 19:19:36 +0000 | [diff] [blame] | 614 | |
Brett Cannon | 32732e3 | 2009-02-15 05:48:13 +0000 | [diff] [blame] | 615 | If 'hooks' is false then use sys.path_hooks. |
Brett Cannon | 1d37668 | 2009-02-02 19:19:36 +0000 | [diff] [blame] | 616 | |
| 617 | """ |
Brett Cannon | 32732e3 | 2009-02-15 05:48:13 +0000 | [diff] [blame] | 618 | if not hooks: |
| 619 | hooks = sys.path_hooks |
| 620 | for hook in hooks: |
Brett Cannon | 1d37668 | 2009-02-02 19:19:36 +0000 | [diff] [blame] | 621 | try: |
| 622 | return hook(path) |
| 623 | except ImportError: |
| 624 | continue |
| 625 | else: |
Brett Cannon | 32732e3 | 2009-02-15 05:48:13 +0000 | [diff] [blame] | 626 | raise ImportError("no path hook found for {0}".format(path)) |
Brett Cannon | 1d37668 | 2009-02-02 19:19:36 +0000 | [diff] [blame] | 627 | |
Brett Cannon | f7e5a8c | 2009-02-05 02:52:18 +0000 | [diff] [blame] | 628 | @classmethod |
Brett Cannon | 32732e3 | 2009-02-15 05:48:13 +0000 | [diff] [blame] | 629 | def _path_importer_cache(cls, path, default=None): |
Brett Cannon | 1d37668 | 2009-02-02 19:19:36 +0000 | [diff] [blame] | 630 | """Get the finder for the path from sys.path_importer_cache. |
| 631 | |
| 632 | If the path is not in the cache, find the appropriate finder and cache |
| 633 | it. If None is cached, get the default finder and cache that |
| 634 | (if applicable). |
| 635 | |
| 636 | Because of NullImporter, some finder should be returned. The only |
| 637 | explicit fail case is if None is cached but the path cannot be used for |
| 638 | the default hook, for which ImportError is raised. |
| 639 | |
| 640 | """ |
| 641 | try: |
Brett Cannon | f7e5a8c | 2009-02-05 02:52:18 +0000 | [diff] [blame] | 642 | finder = sys.path_importer_cache[path] |
Brett Cannon | 1d37668 | 2009-02-02 19:19:36 +0000 | [diff] [blame] | 643 | except KeyError: |
Brett Cannon | f7e5a8c | 2009-02-05 02:52:18 +0000 | [diff] [blame] | 644 | finder = cls._path_hooks(path) |
Brett Cannon | 1d37668 | 2009-02-02 19:19:36 +0000 | [diff] [blame] | 645 | sys.path_importer_cache[path] = finder |
| 646 | else: |
Brett Cannon | 32732e3 | 2009-02-15 05:48:13 +0000 | [diff] [blame] | 647 | if finder is None and default: |
Brett Cannon | 1d37668 | 2009-02-02 19:19:36 +0000 | [diff] [blame] | 648 | # Raises ImportError on failure. |
Brett Cannon | 32732e3 | 2009-02-15 05:48:13 +0000 | [diff] [blame] | 649 | finder = default(path) |
Brett Cannon | 1d37668 | 2009-02-02 19:19:36 +0000 | [diff] [blame] | 650 | sys.path_importer_cache[path] = finder |
| 651 | return finder |
| 652 | |
Brett Cannon | f7e5a8c | 2009-02-05 02:52:18 +0000 | [diff] [blame] | 653 | @classmethod |
| 654 | def find_module(cls, fullname, path=None): |
Brett Cannon | 7aa21f7 | 2009-03-15 00:53:05 +0000 | [diff] [blame] | 655 | """Find the module on sys.path or 'path' based on sys.path_hooks and |
| 656 | sys.path_importer_cache.""" |
Brett Cannon | 1d37668 | 2009-02-02 19:19:36 +0000 | [diff] [blame] | 657 | if not path: |
| 658 | path = sys.path |
| 659 | for entry in path: |
| 660 | try: |
Brett Cannon | f7e5a8c | 2009-02-05 02:52:18 +0000 | [diff] [blame] | 661 | finder = cls._path_importer_cache(entry) |
Brett Cannon | 1d37668 | 2009-02-02 19:19:36 +0000 | [diff] [blame] | 662 | except ImportError: |
| 663 | continue |
| 664 | loader = finder.find_module(fullname) |
| 665 | if loader: |
| 666 | return loader |
| 667 | else: |
| 668 | return None |
| 669 | |
| 670 | |
Brett Cannon | e9103d2 | 2009-03-12 22:37:06 +0000 | [diff] [blame] | 671 | class _ChainedFinder: |
| 672 | |
| 673 | """Finder that sequentially calls other finders.""" |
| 674 | |
| 675 | def __init__(self, *finders): |
| 676 | self._finders = finders |
| 677 | |
| 678 | def find_module(self, fullname, path=None): |
| 679 | for finder in self._finders: |
| 680 | result = finder.find_module(fullname, path) |
| 681 | if result: |
| 682 | return result |
| 683 | else: |
| 684 | return None |
| 685 | |
| 686 | |
Brett Cannon | f87e04d | 2009-03-12 22:47:53 +0000 | [diff] [blame] | 687 | class _FileFinder: |
Brett Cannon | e9103d2 | 2009-03-12 22:37:06 +0000 | [diff] [blame] | 688 | |
| 689 | """Base class for file finders. |
| 690 | |
| 691 | Subclasses are expected to define the following attributes: |
| 692 | |
| 693 | * _suffixes |
| 694 | Sequence of file suffixes whose order will be followed. |
| 695 | |
| 696 | * _possible_package |
| 697 | True if importer should check for packages. |
| 698 | |
| 699 | * _loader |
| 700 | A callable that takes the module name, a file path, and whether |
| 701 | the path points to a package and returns a loader for the module |
| 702 | found at that path. |
| 703 | |
| 704 | """ |
| 705 | |
| 706 | def __init__(self, path_entry): |
| 707 | """Initialize an importer for the passed-in sys.path entry (which is |
| 708 | assumed to have already been verified as an existing directory). |
| 709 | |
| 710 | Can be used as an entry on sys.path_hook. |
| 711 | |
| 712 | """ |
| 713 | absolute_path = _path_absolute(path_entry) |
| 714 | if not _path_isdir(absolute_path): |
| 715 | raise ImportError("only directories are supported") |
| 716 | self._path_entry = absolute_path |
| 717 | |
| 718 | def find_module(self, fullname, path=None): |
| 719 | tail_module = fullname.rpartition('.')[2] |
| 720 | package_directory = None |
| 721 | if self._possible_package: |
| 722 | for ext in self._suffixes: |
| 723 | package_directory = _path_join(self._path_entry, tail_module) |
| 724 | init_filename = '__init__' + ext |
| 725 | package_init = _path_join(package_directory, init_filename) |
| 726 | if (_path_isfile(package_init) and |
| 727 | _case_ok(self._path_entry, tail_module) and |
| 728 | _case_ok(package_directory, init_filename)): |
| 729 | return self._loader(fullname, package_init, True) |
| 730 | for ext in self._suffixes: |
| 731 | file_name = tail_module + ext |
| 732 | file_path = _path_join(self._path_entry, file_name) |
| 733 | if (_path_isfile(file_path) and |
| 734 | _case_ok(self._path_entry, file_name)): |
| 735 | return self._loader(fullname, file_path, False) |
| 736 | else: |
| 737 | # Raise a warning if it matches a directory w/o an __init__ file. |
| 738 | if (package_directory is not None and |
| 739 | _path_isdir(package_directory) and |
| 740 | _case_ok(self._path_entry, tail_module)): |
| 741 | _warnings.warn("Not importing directory %s: missing __init__" |
| 742 | % package_directory, ImportWarning) |
| 743 | return None |
| 744 | |
| 745 | |
Brett Cannon | f87e04d | 2009-03-12 22:47:53 +0000 | [diff] [blame] | 746 | class _PyFileFinder(_FileFinder): |
Brett Cannon | e9103d2 | 2009-03-12 22:37:06 +0000 | [diff] [blame] | 747 | |
| 748 | """Importer for source/bytecode files.""" |
| 749 | |
| 750 | _possible_package = True |
Brett Cannon | f87e04d | 2009-03-12 22:47:53 +0000 | [diff] [blame] | 751 | _loader = _PyFileLoader |
Brett Cannon | e9103d2 | 2009-03-12 22:37:06 +0000 | [diff] [blame] | 752 | |
| 753 | def __init__(self, path_entry): |
| 754 | # Lack of imp during class creation means _suffixes is set here. |
| 755 | # Make sure that Python source files are listed first! Needed for an |
| 756 | # optimization by the loader. |
| 757 | self._suffixes = _suffix_list(imp.PY_SOURCE) |
| 758 | super().__init__(path_entry) |
| 759 | |
| 760 | |
Brett Cannon | f87e04d | 2009-03-12 22:47:53 +0000 | [diff] [blame] | 761 | class _PyPycFileFinder(_PyFileFinder): |
Brett Cannon | e9103d2 | 2009-03-12 22:37:06 +0000 | [diff] [blame] | 762 | |
| 763 | """Finder for source and bytecode files.""" |
| 764 | |
Brett Cannon | f87e04d | 2009-03-12 22:47:53 +0000 | [diff] [blame] | 765 | _loader = _PyPycFileLoader |
Brett Cannon | e9103d2 | 2009-03-12 22:37:06 +0000 | [diff] [blame] | 766 | |
| 767 | def __init__(self, path_entry): |
| 768 | super().__init__(path_entry) |
| 769 | self._suffixes += _suffix_list(imp.PY_COMPILED) |
| 770 | |
| 771 | |
| 772 | |
| 773 | |
Brett Cannon | f87e04d | 2009-03-12 22:47:53 +0000 | [diff] [blame] | 774 | class _ExtensionFileFinder(_FileFinder): |
Brett Cannon | e9103d2 | 2009-03-12 22:37:06 +0000 | [diff] [blame] | 775 | |
| 776 | """Importer for extension files.""" |
| 777 | |
| 778 | _possible_package = False |
| 779 | _loader = _ExtensionFileLoader |
| 780 | |
| 781 | def __init__(self, path_entry): |
| 782 | # Assigning to _suffixes here instead of at the class level because |
| 783 | # imp is not imported at the time of class creation. |
| 784 | self._suffixes = _suffix_list(imp.C_EXTENSION) |
| 785 | super().__init__(path_entry) |
| 786 | |
| 787 | |
Brett Cannon | ce43ddf | 2009-03-12 22:28:55 +0000 | [diff] [blame] | 788 | # Import itself ############################################################### |
| 789 | |
Brett Cannon | e9103d2 | 2009-03-12 22:37:06 +0000 | [diff] [blame] | 790 | def _chained_path_hook(*path_hooks): |
| 791 | """Create a closure which sequentially checks path hooks to see which ones |
| 792 | (if any) can work with a path.""" |
| 793 | def path_hook(entry): |
| 794 | """Check to see if 'entry' matches any of the enclosed path hooks.""" |
| 795 | finders = [] |
| 796 | for hook in path_hooks: |
| 797 | try: |
| 798 | finder = hook(entry) |
| 799 | except ImportError: |
| 800 | continue |
| 801 | else: |
| 802 | finders.append(finder) |
| 803 | if not finders: |
| 804 | raise ImportError("no finder found") |
| 805 | else: |
| 806 | return _ChainedFinder(*finders) |
Brett Cannon | ce43ddf | 2009-03-12 22:28:55 +0000 | [diff] [blame] | 807 | |
Brett Cannon | e9103d2 | 2009-03-12 22:37:06 +0000 | [diff] [blame] | 808 | return path_hook |
Brett Cannon | ce43ddf | 2009-03-12 22:28:55 +0000 | [diff] [blame] | 809 | |
| 810 | |
Brett Cannon | f87e04d | 2009-03-12 22:47:53 +0000 | [diff] [blame] | 811 | _DEFAULT_PATH_HOOK = _chained_path_hook(_ExtensionFileFinder, _PyPycFileFinder) |
Brett Cannon | 2dee597 | 2009-02-21 03:15:37 +0000 | [diff] [blame] | 812 | |
Brett Cannon | 32732e3 | 2009-02-15 05:48:13 +0000 | [diff] [blame] | 813 | class _DefaultPathFinder(PathFinder): |
| 814 | |
| 815 | """Subclass of PathFinder that implements implicit semantics for |
| 816 | __import__.""" |
| 817 | |
Brett Cannon | 32732e3 | 2009-02-15 05:48:13 +0000 | [diff] [blame] | 818 | @classmethod |
| 819 | def _path_hooks(cls, path): |
| 820 | """Search sys.path_hooks as well as implicit path hooks.""" |
| 821 | try: |
| 822 | return super()._path_hooks(path) |
| 823 | except ImportError: |
Brett Cannon | 2dee597 | 2009-02-21 03:15:37 +0000 | [diff] [blame] | 824 | implicit_hooks = [_DEFAULT_PATH_HOOK, imp.NullImporter] |
Brett Cannon | 32732e3 | 2009-02-15 05:48:13 +0000 | [diff] [blame] | 825 | return super()._path_hooks(path, implicit_hooks) |
| 826 | |
| 827 | @classmethod |
| 828 | def _path_importer_cache(cls, path): |
| 829 | """Use the default path hook when None is stored in |
| 830 | sys.path_importer_cache.""" |
Brett Cannon | 2dee597 | 2009-02-21 03:15:37 +0000 | [diff] [blame] | 831 | return super()._path_importer_cache(path, _DEFAULT_PATH_HOOK) |
Brett Cannon | 32732e3 | 2009-02-15 05:48:13 +0000 | [diff] [blame] | 832 | |
| 833 | |
Brett Cannon | e9103d2 | 2009-03-12 22:37:06 +0000 | [diff] [blame] | 834 | class _ImportLockContext: |
| 835 | |
| 836 | """Context manager for the import lock.""" |
| 837 | |
| 838 | def __enter__(self): |
| 839 | """Acquire the import lock.""" |
| 840 | imp.acquire_lock() |
| 841 | |
| 842 | def __exit__(self, exc_type, exc_value, exc_traceback): |
| 843 | """Release the import lock regardless of any raised exceptions.""" |
| 844 | imp.release_lock() |
| 845 | |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 846 | |
Brett Cannon | 32732e3 | 2009-02-15 05:48:13 +0000 | [diff] [blame] | 847 | _IMPLICIT_META_PATH = [BuiltinImporter, FrozenImporter, _DefaultPathFinder] |
| 848 | |
Brett Cannon | 7f9876c | 2009-02-06 02:47:33 +0000 | [diff] [blame] | 849 | def _gcd_import(name, package=None, level=0): |
| 850 | """Import and return the module based on its name, the package the call is |
| 851 | being made from, and the level adjustment. |
| 852 | |
| 853 | This function represents the greatest common denominator of functionality |
Brett Cannon | 2c318a1 | 2009-02-07 01:15:27 +0000 | [diff] [blame] | 854 | between import_module and __import__. This includes settting __package__ if |
| 855 | the loader did not. |
| 856 | |
Brett Cannon | 7f9876c | 2009-02-06 02:47:33 +0000 | [diff] [blame] | 857 | """ |
Brett Cannon | 2c318a1 | 2009-02-07 01:15:27 +0000 | [diff] [blame] | 858 | if package: |
| 859 | if not hasattr(package, 'rindex'): |
| 860 | raise ValueError("__package__ not set to a string") |
| 861 | elif package not in sys.modules: |
| 862 | msg = ("Parent module {0!r} not loaded, cannot perform relative " |
| 863 | "import") |
| 864 | raise SystemError(msg.format(package)) |
| 865 | if not name and level == 0: |
| 866 | raise ValueError("Empty module name") |
Brett Cannon | 7f9876c | 2009-02-06 02:47:33 +0000 | [diff] [blame] | 867 | if level > 0: |
Brett Cannon | 2c318a1 | 2009-02-07 01:15:27 +0000 | [diff] [blame] | 868 | dot = len(package) |
Brett Cannon | 7f9876c | 2009-02-06 02:47:33 +0000 | [diff] [blame] | 869 | for x in range(level, 1, -1): |
| 870 | try: |
| 871 | dot = package.rindex('.', 0, dot) |
Brett Cannon | 7f9876c | 2009-02-06 02:47:33 +0000 | [diff] [blame] | 872 | except ValueError: |
Brett Cannon | 2c318a1 | 2009-02-07 01:15:27 +0000 | [diff] [blame] | 873 | raise ValueError("attempted relative import beyond " |
| 874 | "top-level package") |
| 875 | if name: |
| 876 | name = "{0}.{1}".format(package[:dot], name) |
| 877 | else: |
| 878 | name = package[:dot] |
Brett Cannon | 3eeaa0a | 2009-03-12 22:07:17 +0000 | [diff] [blame] | 879 | with _ImportLockContext(): |
Brett Cannon | 7f9876c | 2009-02-06 02:47:33 +0000 | [diff] [blame] | 880 | try: |
| 881 | return sys.modules[name] |
| 882 | except KeyError: |
| 883 | pass |
| 884 | parent = name.rpartition('.')[0] |
| 885 | path = None |
| 886 | if parent: |
| 887 | if parent not in sys.modules: |
Brett Cannon | 2c318a1 | 2009-02-07 01:15:27 +0000 | [diff] [blame] | 888 | _gcd_import(parent) |
| 889 | # Backwards-compatibility; be nicer to skip the dict lookup. |
| 890 | parent_module = sys.modules[parent] |
Brett Cannon | 7f9876c | 2009-02-06 02:47:33 +0000 | [diff] [blame] | 891 | path = parent_module.__path__ |
Brett Cannon | 32732e3 | 2009-02-15 05:48:13 +0000 | [diff] [blame] | 892 | meta_path = sys.meta_path + _IMPLICIT_META_PATH |
Brett Cannon | 2c318a1 | 2009-02-07 01:15:27 +0000 | [diff] [blame] | 893 | for finder in meta_path: |
Brett Cannon | 7f9876c | 2009-02-06 02:47:33 +0000 | [diff] [blame] | 894 | loader = finder.find_module(name, path) |
Brett Cannon | 2c318a1 | 2009-02-07 01:15:27 +0000 | [diff] [blame] | 895 | if loader is not None: |
| 896 | loader.load_module(name) |
| 897 | break |
Brett Cannon | 7f9876c | 2009-02-06 02:47:33 +0000 | [diff] [blame] | 898 | else: |
| 899 | raise ImportError("No module named {0}".format(name)) |
Brett Cannon | 2c318a1 | 2009-02-07 01:15:27 +0000 | [diff] [blame] | 900 | # Backwards-compatibility; be nicer to skip the dict lookup. |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 901 | module = sys.modules[name] |
| 902 | if parent: |
Brett Cannon | 2c318a1 | 2009-02-07 01:15:27 +0000 | [diff] [blame] | 903 | # Set the module as an attribute on its parent. |
| 904 | setattr(parent_module, name.rpartition('.')[2], module) |
| 905 | # Set __package__ if the loader did not. |
| 906 | if not hasattr(module, '__package__') or module.__package__ is None: |
| 907 | # Watch out for what comes out of sys.modules to not be a module, |
| 908 | # e.g. an int. |
| 909 | try: |
| 910 | module.__package__ = module.__name__ |
| 911 | if not hasattr(module, '__path__'): |
| 912 | module.__package__ = module.__package__.rpartition('.')[0] |
| 913 | except AttributeError: |
| 914 | pass |
| 915 | return module |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 916 | |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 917 | |
Brett Cannon | 7aa21f7 | 2009-03-15 00:53:05 +0000 | [diff] [blame] | 918 | def __import__(name, globals={}, locals={}, fromlist=[], level=0): |
Brett Cannon | 2c318a1 | 2009-02-07 01:15:27 +0000 | [diff] [blame] | 919 | """Import a module. |
| 920 | |
| 921 | The 'globals' argument is used to infer where the import is occuring from |
| 922 | to handle relative imports. The 'locals' argument is ignored. The |
| 923 | 'fromlist' argument specifies what should exist as attributes on the module |
| 924 | being imported (e.g. ``from module import <fromlist>``). The 'level' |
| 925 | argument represents the package location to import from in a relative |
| 926 | import (e.g. ``from ..pkg import mod`` would have a 'level' of 2). |
| 927 | |
| 928 | """ |
| 929 | if level == 0: |
| 930 | module = _gcd_import(name) |
| 931 | else: |
| 932 | # __package__ is not guaranteed to be defined. |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 933 | try: |
Brett Cannon | 2c318a1 | 2009-02-07 01:15:27 +0000 | [diff] [blame] | 934 | package = globals['__package__'] |
| 935 | except KeyError: |
| 936 | package = globals['__name__'] |
| 937 | if '__path__' not in globals: |
| 938 | package = package.rpartition('.')[0] |
| 939 | module = _gcd_import(name, package, level) |
| 940 | # The hell that is fromlist ... |
| 941 | if not fromlist: |
| 942 | # Return up to the first dot in 'name'. This is complicated by the fact |
| 943 | # that 'name' may be relative. |
| 944 | if level == 0: |
| 945 | return sys.modules[name.partition('.')[0]] |
| 946 | elif not name: |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 947 | return module |
Brett Cannon | 2c318a1 | 2009-02-07 01:15:27 +0000 | [diff] [blame] | 948 | else: |
| 949 | cut_off = len(name) - len(name.partition('.')[0]) |
| 950 | return sys.modules[module.__name__[:-cut_off]] |
| 951 | else: |
| 952 | # If a package was imported, try to import stuff from fromlist. |
| 953 | if hasattr(module, '__path__'): |
| 954 | if '*' in fromlist and hasattr(module, '__all__'): |
| 955 | fromlist.remove('*') |
| 956 | fromlist.extend(module.__all__) |
| 957 | for x in (y for y in fromlist if not hasattr(module,y)): |
| 958 | try: |
| 959 | _gcd_import('{0}.{1}'.format(module.__name__, x)) |
| 960 | except ImportError: |
| 961 | pass |
| 962 | return module |