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