Brett Cannon | 6f44d66 | 2012-04-15 16:08:47 -0400 | [diff] [blame] | 1 | """This module provides the components needed to build your own __import__ |
| 2 | function. Undocumented functions are obsolete. |
| 3 | |
| 4 | In most cases it is preferred you consider using the importlib module's |
| 5 | functionality over this module. |
| 6 | |
| 7 | """ |
| 8 | # (Probably) need to stay in _imp |
Brett Cannon | 62228db | 2012-04-29 14:38:11 -0400 | [diff] [blame] | 9 | from _imp import (lock_held, acquire_lock, release_lock, |
Brett Cannon | 3e2fe05 | 2013-03-17 15:48:16 -0700 | [diff] [blame] | 10 | get_frozen_object, is_frozen_package, |
Brett Cannon | 2fef4d2 | 2012-04-15 19:06:23 -0400 | [diff] [blame] | 11 | init_builtin, init_frozen, is_builtin, is_frozen, |
Brett Cannon | ac9f2f3 | 2012-08-10 13:47:54 -0400 | [diff] [blame] | 12 | _fix_co_filename) |
Brett Cannon | 3e2fe05 | 2013-03-17 15:48:16 -0700 | [diff] [blame] | 13 | try: |
| 14 | from _imp import load_dynamic |
Brett Cannon | cd171c8 | 2013-07-04 17:43:24 -0400 | [diff] [blame] | 15 | except ImportError: |
Brett Cannon | 3e2fe05 | 2013-03-17 15:48:16 -0700 | [diff] [blame] | 16 | # Platform doesn't support dynamic loading. |
| 17 | load_dynamic = None |
Brett Cannon | 6f44d66 | 2012-04-15 16:08:47 -0400 | [diff] [blame] | 18 | |
Brett Cannon | 2a17bde | 2014-05-30 14:55:29 -0400 | [diff] [blame] | 19 | from importlib._bootstrap import SourcelessFileLoader, _ERR_MSG, _exec, _load |
Brett Cannon | 01a7617 | 2012-04-15 20:25:23 -0400 | [diff] [blame] | 20 | |
Brett Cannon | cb66eb0 | 2012-05-11 12:58:42 -0400 | [diff] [blame] | 21 | from importlib import machinery |
Brett Cannon | 05a647d | 2013-06-14 19:02:34 -0400 | [diff] [blame] | 22 | from importlib import util |
Brett Cannon | 3fe35e6 | 2013-06-14 15:04:26 -0400 | [diff] [blame] | 23 | import importlib |
Brett Cannon | 2ee6142 | 2012-04-15 22:28:28 -0400 | [diff] [blame] | 24 | import os |
Brett Cannon | e69f0df | 2012-04-21 21:09:46 -0400 | [diff] [blame] | 25 | import sys |
| 26 | import tokenize |
Brett Cannon | a3c9615 | 2013-06-14 22:26:30 -0400 | [diff] [blame] | 27 | import types |
Brett Cannon | cb66eb0 | 2012-05-11 12:58:42 -0400 | [diff] [blame] | 28 | import warnings |
Brett Cannon | e69f0df | 2012-04-21 21:09:46 -0400 | [diff] [blame] | 29 | |
Brett Cannon | e4f41de | 2013-06-16 13:13:40 -0400 | [diff] [blame] | 30 | warnings.warn("the imp module is deprecated in favour of importlib; " |
| 31 | "see the module's documentation for alternative uses", |
| 32 | PendingDeprecationWarning) |
Brett Cannon | e69f0df | 2012-04-21 21:09:46 -0400 | [diff] [blame] | 33 | |
Brett Cannon | c049952 | 2012-05-11 14:48:41 -0400 | [diff] [blame] | 34 | # DEPRECATED |
Brett Cannon | e69f0df | 2012-04-21 21:09:46 -0400 | [diff] [blame] | 35 | SEARCH_ERROR = 0 |
| 36 | PY_SOURCE = 1 |
| 37 | PY_COMPILED = 2 |
| 38 | C_EXTENSION = 3 |
| 39 | PY_RESOURCE = 4 |
| 40 | PKG_DIRECTORY = 5 |
| 41 | C_BUILTIN = 6 |
| 42 | PY_FROZEN = 7 |
| 43 | PY_CODERESOURCE = 8 |
| 44 | IMP_HOOK = 9 |
Brett Cannon | 2ee6142 | 2012-04-15 22:28:28 -0400 | [diff] [blame] | 45 | |
| 46 | |
Brett Cannon | a3c9615 | 2013-06-14 22:26:30 -0400 | [diff] [blame] | 47 | def new_module(name): |
| 48 | """**DEPRECATED** |
| 49 | |
| 50 | Create a new module. |
| 51 | |
| 52 | The module is not entered into sys.modules. |
| 53 | |
| 54 | """ |
| 55 | return types.ModuleType(name) |
| 56 | |
| 57 | |
Brett Cannon | 77b2abd | 2012-07-09 16:09:00 -0400 | [diff] [blame] | 58 | def get_magic(): |
Brett Cannon | 05a647d | 2013-06-14 19:02:34 -0400 | [diff] [blame] | 59 | """**DEPRECATED** |
| 60 | |
| 61 | Return the magic number for .pyc or .pyo files. |
| 62 | """ |
| 63 | return util.MAGIC_NUMBER |
Brett Cannon | 77b2abd | 2012-07-09 16:09:00 -0400 | [diff] [blame] | 64 | |
| 65 | |
Brett Cannon | 98979b8 | 2012-07-02 15:13:11 -0400 | [diff] [blame] | 66 | def get_tag(): |
| 67 | """Return the magic tag for .pyc or .pyo files.""" |
| 68 | return sys.implementation.cache_tag |
| 69 | |
| 70 | |
Brett Cannon | a38e814 | 2013-06-14 22:35:40 -0400 | [diff] [blame] | 71 | def cache_from_source(path, debug_override=None): |
| 72 | """**DEPRECATED** |
| 73 | |
| 74 | Given the path to a .py file, return the path to its .pyc/.pyo file. |
| 75 | |
| 76 | The .py file does not need to exist; this simply returns the path to the |
| 77 | .pyc/.pyo file calculated as if the .py file were imported. The extension |
| 78 | will be .pyc unless sys.flags.optimize is non-zero, then it will be .pyo. |
| 79 | |
| 80 | If debug_override is not None, then it must be a boolean and is used in |
| 81 | place of sys.flags.optimize. |
| 82 | |
| 83 | If sys.implementation.cache_tag is None then NotImplementedError is raised. |
| 84 | |
| 85 | """ |
| 86 | return util.cache_from_source(path, debug_override) |
| 87 | |
| 88 | |
| 89 | def source_from_cache(path): |
| 90 | """**DEPRECATED** |
| 91 | |
| 92 | Given the path to a .pyc./.pyo file, return the path to its .py file. |
| 93 | |
| 94 | The .pyc/.pyo file does not need to exist; this simply returns the path to |
| 95 | the .py file calculated to correspond to the .pyc/.pyo file. If path does |
| 96 | not conform to PEP 3147 format, ValueError will be raised. If |
| 97 | sys.implementation.cache_tag is None then NotImplementedError is raised. |
| 98 | |
| 99 | """ |
| 100 | return util.source_from_cache(path) |
| 101 | |
| 102 | |
Brett Cannon | 2657df4 | 2012-05-04 15:20:40 -0400 | [diff] [blame] | 103 | def get_suffixes(): |
Brett Cannon | e4f41de | 2013-06-16 13:13:40 -0400 | [diff] [blame] | 104 | """**DEPRECATED**""" |
Brett Cannon | ac9f2f3 | 2012-08-10 13:47:54 -0400 | [diff] [blame] | 105 | extensions = [(s, 'rb', C_EXTENSION) for s in machinery.EXTENSION_SUFFIXES] |
Serhiy Storchaka | 6787a38 | 2013-11-23 22:12:06 +0200 | [diff] [blame] | 106 | source = [(s, 'r', PY_SOURCE) for s in machinery.SOURCE_SUFFIXES] |
Brett Cannon | cb66eb0 | 2012-05-11 12:58:42 -0400 | [diff] [blame] | 107 | bytecode = [(s, 'rb', PY_COMPILED) for s in machinery.BYTECODE_SUFFIXES] |
Brett Cannon | 2657df4 | 2012-05-04 15:20:40 -0400 | [diff] [blame] | 108 | |
| 109 | return extensions + source + bytecode |
| 110 | |
| 111 | |
Brett Cannon | acf85cd | 2012-04-29 12:50:03 -0400 | [diff] [blame] | 112 | class NullImporter: |
| 113 | |
Brett Cannon | e4f41de | 2013-06-16 13:13:40 -0400 | [diff] [blame] | 114 | """**DEPRECATED** |
| 115 | |
| 116 | Null import object. |
| 117 | |
| 118 | """ |
Brett Cannon | acf85cd | 2012-04-29 12:50:03 -0400 | [diff] [blame] | 119 | |
| 120 | def __init__(self, path): |
| 121 | if path == '': |
| 122 | raise ImportError('empty pathname', path='') |
| 123 | elif os.path.isdir(path): |
| 124 | raise ImportError('existing directory', path=path) |
| 125 | |
| 126 | def find_module(self, fullname): |
| 127 | """Always returns None.""" |
| 128 | return None |
| 129 | |
| 130 | |
Brett Cannon | 64befe9 | 2012-04-17 19:14:26 -0400 | [diff] [blame] | 131 | class _HackedGetData: |
Brett Cannon | 16475ad | 2012-04-16 22:11:25 -0400 | [diff] [blame] | 132 | |
Brett Cannon | 64befe9 | 2012-04-17 19:14:26 -0400 | [diff] [blame] | 133 | """Compatibiilty support for 'file' arguments of various load_*() |
| 134 | functions.""" |
Brett Cannon | 16475ad | 2012-04-16 22:11:25 -0400 | [diff] [blame] | 135 | |
| 136 | def __init__(self, fullname, path, file=None): |
| 137 | super().__init__(fullname, path) |
| 138 | self.file = file |
| 139 | |
| 140 | def get_data(self, path): |
Brett Cannon | 64befe9 | 2012-04-17 19:14:26 -0400 | [diff] [blame] | 141 | """Gross hack to contort loader to deal w/ load_*()'s bad API.""" |
Brett Cannon | 938d44d | 2012-04-22 19:58:33 -0400 | [diff] [blame] | 142 | if self.file and path == self.path: |
Brett Cannon | a4975a9 | 2013-08-23 11:45:57 -0400 | [diff] [blame] | 143 | if not self.file.closed: |
| 144 | file = self.file |
| 145 | else: |
| 146 | self.file = file = open(self.path, 'r') |
| 147 | |
| 148 | with file: |
Brett Cannon | 16475ad | 2012-04-16 22:11:25 -0400 | [diff] [blame] | 149 | # Technically should be returning bytes, but |
| 150 | # SourceLoader.get_code() just passed what is returned to |
| 151 | # compile() which can handle str. And converting to bytes would |
| 152 | # require figuring out the encoding to decode to and |
| 153 | # tokenize.detect_encoding() only accepts bytes. |
Brett Cannon | a4975a9 | 2013-08-23 11:45:57 -0400 | [diff] [blame] | 154 | return file.read() |
Brett Cannon | 16475ad | 2012-04-16 22:11:25 -0400 | [diff] [blame] | 155 | else: |
| 156 | return super().get_data(path) |
| 157 | |
| 158 | |
Brett Cannon | 589c4ff | 2013-06-14 22:29:58 -0400 | [diff] [blame] | 159 | class _LoadSourceCompatibility(_HackedGetData, machinery.SourceFileLoader): |
Brett Cannon | 64befe9 | 2012-04-17 19:14:26 -0400 | [diff] [blame] | 160 | |
| 161 | """Compatibility support for implementing load_source().""" |
| 162 | |
| 163 | |
Brett Cannon | 16475ad | 2012-04-16 22:11:25 -0400 | [diff] [blame] | 164 | def load_source(name, pathname, file=None): |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 165 | loader = _LoadSourceCompatibility(name, pathname, file) |
| 166 | spec = util.spec_from_file_location(name, pathname, loader=loader) |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 167 | if name in sys.modules: |
Brett Cannon | 2a17bde | 2014-05-30 14:55:29 -0400 | [diff] [blame] | 168 | module = _exec(spec, sys.modules[name]) |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 169 | else: |
Brett Cannon | 2a17bde | 2014-05-30 14:55:29 -0400 | [diff] [blame] | 170 | module = _load(spec) |
Brett Cannon | 5a4c233 | 2013-04-28 11:53:26 -0400 | [diff] [blame] | 171 | # To allow reloading to potentially work, use a non-hacked loader which |
| 172 | # won't rely on a now-closed file object. |
Brett Cannon | 589c4ff | 2013-06-14 22:29:58 -0400 | [diff] [blame] | 173 | module.__loader__ = machinery.SourceFileLoader(name, pathname) |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 174 | module.__spec__.loader = module.__loader__ |
Brett Cannon | 5a4c233 | 2013-04-28 11:53:26 -0400 | [diff] [blame] | 175 | return module |
Brett Cannon | 16475ad | 2012-04-16 22:11:25 -0400 | [diff] [blame] | 176 | |
| 177 | |
Brett Cannon | 589c4ff | 2013-06-14 22:29:58 -0400 | [diff] [blame] | 178 | class _LoadCompiledCompatibility(_HackedGetData, SourcelessFileLoader): |
Brett Cannon | 64befe9 | 2012-04-17 19:14:26 -0400 | [diff] [blame] | 179 | |
| 180 | """Compatibility support for implementing load_compiled().""" |
| 181 | |
| 182 | |
| 183 | def load_compiled(name, pathname, file=None): |
Brett Cannon | e4f41de | 2013-06-16 13:13:40 -0400 | [diff] [blame] | 184 | """**DEPRECATED**""" |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 185 | loader = _LoadCompiledCompatibility(name, pathname, file) |
| 186 | spec = util.spec_from_file_location(name, pathname, loader=loader) |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 187 | if name in sys.modules: |
Brett Cannon | 2a17bde | 2014-05-30 14:55:29 -0400 | [diff] [blame] | 188 | module = _exec(spec, sys.modules[name]) |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 189 | else: |
Brett Cannon | 2a17bde | 2014-05-30 14:55:29 -0400 | [diff] [blame] | 190 | module = _load(spec) |
Brett Cannon | 5a4c233 | 2013-04-28 11:53:26 -0400 | [diff] [blame] | 191 | # To allow reloading to potentially work, use a non-hacked loader which |
| 192 | # won't rely on a now-closed file object. |
Brett Cannon | 589c4ff | 2013-06-14 22:29:58 -0400 | [diff] [blame] | 193 | module.__loader__ = SourcelessFileLoader(name, pathname) |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 194 | module.__spec__.loader = module.__loader__ |
Brett Cannon | 5a4c233 | 2013-04-28 11:53:26 -0400 | [diff] [blame] | 195 | return module |
Brett Cannon | 64befe9 | 2012-04-17 19:14:26 -0400 | [diff] [blame] | 196 | |
| 197 | |
Brett Cannon | 2ee6142 | 2012-04-15 22:28:28 -0400 | [diff] [blame] | 198 | def load_package(name, path): |
Brett Cannon | e4f41de | 2013-06-16 13:13:40 -0400 | [diff] [blame] | 199 | """**DEPRECATED**""" |
Brett Cannon | 2ee6142 | 2012-04-15 22:28:28 -0400 | [diff] [blame] | 200 | if os.path.isdir(path): |
Brett Cannon | c049952 | 2012-05-11 14:48:41 -0400 | [diff] [blame] | 201 | extensions = (machinery.SOURCE_SUFFIXES[:] + |
| 202 | machinery.BYTECODE_SUFFIXES[:]) |
Brett Cannon | 2ee6142 | 2012-04-15 22:28:28 -0400 | [diff] [blame] | 203 | for extension in extensions: |
| 204 | path = os.path.join(path, '__init__'+extension) |
| 205 | if os.path.exists(path): |
| 206 | break |
| 207 | else: |
| 208 | raise ValueError('{!r} is not a package'.format(path)) |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 209 | spec = util.spec_from_file_location(name, path, |
| 210 | submodule_search_locations=[]) |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 211 | if name in sys.modules: |
Brett Cannon | 2a17bde | 2014-05-30 14:55:29 -0400 | [diff] [blame] | 212 | return _exec(spec, sys.modules[name]) |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 213 | else: |
Brett Cannon | 2a17bde | 2014-05-30 14:55:29 -0400 | [diff] [blame] | 214 | return _load(spec) |
Brett Cannon | 2ee6142 | 2012-04-15 22:28:28 -0400 | [diff] [blame] | 215 | |
Brett Cannon | 01a7617 | 2012-04-15 20:25:23 -0400 | [diff] [blame] | 216 | |
| 217 | def load_module(name, file, filename, details): |
Brett Cannon | 0450c9e | 2012-06-15 19:39:06 -0400 | [diff] [blame] | 218 | """**DEPRECATED** |
| 219 | |
| 220 | Load a module, given information returned by find_module(). |
Brett Cannon | 01a7617 | 2012-04-15 20:25:23 -0400 | [diff] [blame] | 221 | |
| 222 | The module name must include the full package name, if any. |
| 223 | |
| 224 | """ |
| 225 | suffix, mode, type_ = details |
Brett Cannon | e4f41de | 2013-06-16 13:13:40 -0400 | [diff] [blame] | 226 | if mode and (not mode.startswith(('r', 'U')) or '+' in mode): |
| 227 | raise ValueError('invalid file open mode {!r}'.format(mode)) |
| 228 | elif file is None and type_ in {PY_SOURCE, PY_COMPILED}: |
| 229 | msg = 'file object required for import (type code {})'.format(type_) |
| 230 | raise ValueError(msg) |
| 231 | elif type_ == PY_SOURCE: |
| 232 | return load_source(name, filename, file) |
| 233 | elif type_ == PY_COMPILED: |
| 234 | return load_compiled(name, filename, file) |
| 235 | elif type_ == C_EXTENSION and load_dynamic is not None: |
| 236 | if file is None: |
| 237 | with open(filename, 'rb') as opened_file: |
| 238 | return load_dynamic(name, filename, opened_file) |
Brett Cannon | c049952 | 2012-05-11 14:48:41 -0400 | [diff] [blame] | 239 | else: |
Brett Cannon | e4f41de | 2013-06-16 13:13:40 -0400 | [diff] [blame] | 240 | return load_dynamic(name, filename, file) |
| 241 | elif type_ == PKG_DIRECTORY: |
| 242 | return load_package(name, filename) |
| 243 | elif type_ == C_BUILTIN: |
| 244 | return init_builtin(name) |
| 245 | elif type_ == PY_FROZEN: |
| 246 | return init_frozen(name) |
| 247 | else: |
| 248 | msg = "Don't know how to import {} (type code {})".format(name, type_) |
| 249 | raise ImportError(msg, name=name) |
Brett Cannon | e69f0df | 2012-04-21 21:09:46 -0400 | [diff] [blame] | 250 | |
| 251 | |
| 252 | def find_module(name, path=None): |
Brett Cannon | 0450c9e | 2012-06-15 19:39:06 -0400 | [diff] [blame] | 253 | """**DEPRECATED** |
| 254 | |
| 255 | Search for a module. |
Brett Cannon | e69f0df | 2012-04-21 21:09:46 -0400 | [diff] [blame] | 256 | |
| 257 | If path is omitted or None, search for a built-in, frozen or special |
| 258 | module and continue search in sys.path. The module name cannot |
| 259 | contain '.'; to search for a submodule of a package, pass the |
| 260 | submodule name and the package's __path__. |
| 261 | |
| 262 | """ |
| 263 | if not isinstance(name, str): |
| 264 | raise TypeError("'name' must be a str, not {}".format(type(name))) |
| 265 | elif not isinstance(path, (type(None), list)): |
| 266 | # Backwards-compatibility |
| 267 | raise RuntimeError("'list' must be None or a list, " |
| 268 | "not {}".format(type(name))) |
| 269 | |
| 270 | if path is None: |
| 271 | if is_builtin(name): |
| 272 | return None, None, ('', '', C_BUILTIN) |
| 273 | elif is_frozen(name): |
| 274 | return None, None, ('', '', PY_FROZEN) |
| 275 | else: |
| 276 | path = sys.path |
| 277 | |
| 278 | for entry in path: |
| 279 | package_directory = os.path.join(entry, name) |
Brett Cannon | cb66eb0 | 2012-05-11 12:58:42 -0400 | [diff] [blame] | 280 | for suffix in ['.py', machinery.BYTECODE_SUFFIXES[0]]: |
Brett Cannon | e69f0df | 2012-04-21 21:09:46 -0400 | [diff] [blame] | 281 | package_file_name = '__init__' + suffix |
| 282 | file_path = os.path.join(package_directory, package_file_name) |
| 283 | if os.path.isfile(file_path): |
| 284 | return None, package_directory, ('', '', PKG_DIRECTORY) |
Brett Cannon | e4f41de | 2013-06-16 13:13:40 -0400 | [diff] [blame] | 285 | for suffix, mode, type_ in get_suffixes(): |
| 286 | file_name = name + suffix |
| 287 | file_path = os.path.join(entry, file_name) |
| 288 | if os.path.isfile(file_path): |
| 289 | break |
| 290 | else: |
| 291 | continue |
| 292 | break # Break out of outer loop when breaking out of inner loop. |
Brett Cannon | e69f0df | 2012-04-21 21:09:46 -0400 | [diff] [blame] | 293 | else: |
Brett Cannon | 589c4ff | 2013-06-14 22:29:58 -0400 | [diff] [blame] | 294 | raise ImportError(_ERR_MSG.format(name), name=name) |
Brett Cannon | e69f0df | 2012-04-21 21:09:46 -0400 | [diff] [blame] | 295 | |
| 296 | encoding = None |
Serhiy Storchaka | 6787a38 | 2013-11-23 22:12:06 +0200 | [diff] [blame] | 297 | if 'b' not in mode: |
Brett Cannon | e69f0df | 2012-04-21 21:09:46 -0400 | [diff] [blame] | 298 | with open(file_path, 'rb') as file: |
| 299 | encoding = tokenize.detect_encoding(file.readline)[0] |
| 300 | file = open(file_path, mode, encoding=encoding) |
| 301 | return file, file_path, (suffix, mode, type_) |
Brett Cannon | 62228db | 2012-04-29 14:38:11 -0400 | [diff] [blame] | 302 | |
| 303 | |
Brett Cannon | 62228db | 2012-04-29 14:38:11 -0400 | [diff] [blame] | 304 | def reload(module): |
Brett Cannon | 3fe35e6 | 2013-06-14 15:04:26 -0400 | [diff] [blame] | 305 | """**DEPRECATED** |
| 306 | |
| 307 | Reload the module and return it. |
Brett Cannon | 62228db | 2012-04-29 14:38:11 -0400 | [diff] [blame] | 308 | |
| 309 | The module must have been successfully imported before. |
| 310 | |
| 311 | """ |
Brett Cannon | 3fe35e6 | 2013-06-14 15:04:26 -0400 | [diff] [blame] | 312 | return importlib.reload(module) |