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, |
Nick Coghlan | d5cacbb | 2015-05-23 22:24:10 +1000 | [diff] [blame] | 11 | 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: |
Nick Coghlan | d5cacbb | 2015-05-23 22:24:10 +1000 | [diff] [blame] | 14 | from _imp import create_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. |
Nick Coghlan | d5cacbb | 2015-05-23 22:24:10 +1000 | [diff] [blame] | 17 | create_dynamic = None |
Brett Cannon | 6f44d66 | 2012-04-15 16:08:47 -0400 | [diff] [blame] | 18 | |
Nick Coghlan | d5cacbb | 2015-05-23 22:24:10 +1000 | [diff] [blame] | 19 | from importlib._bootstrap import _ERR_MSG, _exec, _load, _builtin_from_name |
Eric Snow | 32439d6 | 2015-05-02 19:15:18 -0600 | [diff] [blame] | 20 | from importlib._bootstrap_external import SourcelessFileLoader |
Brett Cannon | 01a7617 | 2012-04-15 20:25:23 -0400 | [diff] [blame] | 21 | |
Brett Cannon | cb66eb0 | 2012-05-11 12:58:42 -0400 | [diff] [blame] | 22 | from importlib import machinery |
Brett Cannon | 05a647d | 2013-06-14 19:02:34 -0400 | [diff] [blame] | 23 | from importlib import util |
Brett Cannon | 3fe35e6 | 2013-06-14 15:04:26 -0400 | [diff] [blame] | 24 | import importlib |
Brett Cannon | 2ee6142 | 2012-04-15 22:28:28 -0400 | [diff] [blame] | 25 | import os |
Brett Cannon | e69f0df | 2012-04-21 21:09:46 -0400 | [diff] [blame] | 26 | import sys |
| 27 | import tokenize |
Brett Cannon | a3c9615 | 2013-06-14 22:26:30 -0400 | [diff] [blame] | 28 | import types |
Brett Cannon | cb66eb0 | 2012-05-11 12:58:42 -0400 | [diff] [blame] | 29 | import warnings |
Brett Cannon | e69f0df | 2012-04-21 21:09:46 -0400 | [diff] [blame] | 30 | |
Brett Cannon | e4f41de | 2013-06-16 13:13:40 -0400 | [diff] [blame] | 31 | warnings.warn("the imp module is deprecated in favour of importlib; " |
| 32 | "see the module's documentation for alternative uses", |
Brett Cannon | c0d91af | 2015-10-16 12:21:37 -0700 | [diff] [blame] | 33 | DeprecationWarning, stacklevel=2) |
Brett Cannon | e69f0df | 2012-04-21 21:09:46 -0400 | [diff] [blame] | 34 | |
Brett Cannon | c049952 | 2012-05-11 14:48:41 -0400 | [diff] [blame] | 35 | # DEPRECATED |
Brett Cannon | e69f0df | 2012-04-21 21:09:46 -0400 | [diff] [blame] | 36 | SEARCH_ERROR = 0 |
| 37 | PY_SOURCE = 1 |
| 38 | PY_COMPILED = 2 |
| 39 | C_EXTENSION = 3 |
| 40 | PY_RESOURCE = 4 |
| 41 | PKG_DIRECTORY = 5 |
| 42 | C_BUILTIN = 6 |
| 43 | PY_FROZEN = 7 |
| 44 | PY_CODERESOURCE = 8 |
| 45 | IMP_HOOK = 9 |
Brett Cannon | 2ee6142 | 2012-04-15 22:28:28 -0400 | [diff] [blame] | 46 | |
| 47 | |
Brett Cannon | a3c9615 | 2013-06-14 22:26:30 -0400 | [diff] [blame] | 48 | def new_module(name): |
| 49 | """**DEPRECATED** |
| 50 | |
| 51 | Create a new module. |
| 52 | |
| 53 | The module is not entered into sys.modules. |
| 54 | |
| 55 | """ |
| 56 | return types.ModuleType(name) |
| 57 | |
| 58 | |
Brett Cannon | 77b2abd | 2012-07-09 16:09:00 -0400 | [diff] [blame] | 59 | def get_magic(): |
Brett Cannon | 05a647d | 2013-06-14 19:02:34 -0400 | [diff] [blame] | 60 | """**DEPRECATED** |
| 61 | |
Brett Cannon | f299abd | 2015-04-13 14:21:02 -0400 | [diff] [blame] | 62 | Return the magic number for .pyc files. |
Brett Cannon | 05a647d | 2013-06-14 19:02:34 -0400 | [diff] [blame] | 63 | """ |
| 64 | return util.MAGIC_NUMBER |
Brett Cannon | 77b2abd | 2012-07-09 16:09:00 -0400 | [diff] [blame] | 65 | |
| 66 | |
Brett Cannon | 98979b8 | 2012-07-02 15:13:11 -0400 | [diff] [blame] | 67 | def get_tag(): |
Brett Cannon | f299abd | 2015-04-13 14:21:02 -0400 | [diff] [blame] | 68 | """Return the magic tag for .pyc files.""" |
Brett Cannon | 98979b8 | 2012-07-02 15:13:11 -0400 | [diff] [blame] | 69 | return sys.implementation.cache_tag |
| 70 | |
| 71 | |
Brett Cannon | a38e814 | 2013-06-14 22:35:40 -0400 | [diff] [blame] | 72 | def cache_from_source(path, debug_override=None): |
| 73 | """**DEPRECATED** |
| 74 | |
Brett Cannon | f299abd | 2015-04-13 14:21:02 -0400 | [diff] [blame] | 75 | Given the path to a .py file, return the path to its .pyc file. |
Brett Cannon | a38e814 | 2013-06-14 22:35:40 -0400 | [diff] [blame] | 76 | |
| 77 | The .py file does not need to exist; this simply returns the path to the |
Brett Cannon | f299abd | 2015-04-13 14:21:02 -0400 | [diff] [blame] | 78 | .pyc file calculated as if the .py file were imported. |
Brett Cannon | a38e814 | 2013-06-14 22:35:40 -0400 | [diff] [blame] | 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 | """ |
Brett Cannon | f299abd | 2015-04-13 14:21:02 -0400 | [diff] [blame] | 86 | with warnings.catch_warnings(): |
| 87 | warnings.simplefilter('ignore') |
| 88 | return util.cache_from_source(path, debug_override) |
Brett Cannon | a38e814 | 2013-06-14 22:35:40 -0400 | [diff] [blame] | 89 | |
| 90 | |
| 91 | def source_from_cache(path): |
| 92 | """**DEPRECATED** |
| 93 | |
Brett Cannon | f299abd | 2015-04-13 14:21:02 -0400 | [diff] [blame] | 94 | Given the path to a .pyc. file, return the path to its .py file. |
Brett Cannon | a38e814 | 2013-06-14 22:35:40 -0400 | [diff] [blame] | 95 | |
Brett Cannon | f299abd | 2015-04-13 14:21:02 -0400 | [diff] [blame] | 96 | The .pyc file does not need to exist; this simply returns the path to |
| 97 | the .py file calculated to correspond to the .pyc file. If path does |
Brett Cannon | a38e814 | 2013-06-14 22:35:40 -0400 | [diff] [blame] | 98 | not conform to PEP 3147 format, ValueError will be raised. If |
| 99 | sys.implementation.cache_tag is None then NotImplementedError is raised. |
| 100 | |
| 101 | """ |
| 102 | return util.source_from_cache(path) |
| 103 | |
| 104 | |
Brett Cannon | 2657df4 | 2012-05-04 15:20:40 -0400 | [diff] [blame] | 105 | def get_suffixes(): |
Brett Cannon | e4f41de | 2013-06-16 13:13:40 -0400 | [diff] [blame] | 106 | """**DEPRECATED**""" |
Brett Cannon | ac9f2f3 | 2012-08-10 13:47:54 -0400 | [diff] [blame] | 107 | extensions = [(s, 'rb', C_EXTENSION) for s in machinery.EXTENSION_SUFFIXES] |
Serhiy Storchaka | 6787a38 | 2013-11-23 22:12:06 +0200 | [diff] [blame] | 108 | source = [(s, 'r', PY_SOURCE) for s in machinery.SOURCE_SUFFIXES] |
Brett Cannon | cb66eb0 | 2012-05-11 12:58:42 -0400 | [diff] [blame] | 109 | bytecode = [(s, 'rb', PY_COMPILED) for s in machinery.BYTECODE_SUFFIXES] |
Brett Cannon | 2657df4 | 2012-05-04 15:20:40 -0400 | [diff] [blame] | 110 | |
| 111 | return extensions + source + bytecode |
| 112 | |
| 113 | |
Brett Cannon | acf85cd | 2012-04-29 12:50:03 -0400 | [diff] [blame] | 114 | class NullImporter: |
| 115 | |
Brett Cannon | e4f41de | 2013-06-16 13:13:40 -0400 | [diff] [blame] | 116 | """**DEPRECATED** |
| 117 | |
| 118 | Null import object. |
| 119 | |
| 120 | """ |
Brett Cannon | acf85cd | 2012-04-29 12:50:03 -0400 | [diff] [blame] | 121 | |
| 122 | def __init__(self, path): |
| 123 | if path == '': |
| 124 | raise ImportError('empty pathname', path='') |
| 125 | elif os.path.isdir(path): |
| 126 | raise ImportError('existing directory', path=path) |
| 127 | |
| 128 | def find_module(self, fullname): |
| 129 | """Always returns None.""" |
| 130 | return None |
| 131 | |
| 132 | |
Brett Cannon | 64befe9 | 2012-04-17 19:14:26 -0400 | [diff] [blame] | 133 | class _HackedGetData: |
Brett Cannon | 16475ad | 2012-04-16 22:11:25 -0400 | [diff] [blame] | 134 | |
Zachary Ware | 50db6ac | 2015-04-14 15:43:00 -0500 | [diff] [blame] | 135 | """Compatibility support for 'file' arguments of various load_*() |
Brett Cannon | 64befe9 | 2012-04-17 19:14:26 -0400 | [diff] [blame] | 136 | functions.""" |
Brett Cannon | 16475ad | 2012-04-16 22:11:25 -0400 | [diff] [blame] | 137 | |
| 138 | def __init__(self, fullname, path, file=None): |
| 139 | super().__init__(fullname, path) |
| 140 | self.file = file |
| 141 | |
| 142 | def get_data(self, path): |
Brett Cannon | 64befe9 | 2012-04-17 19:14:26 -0400 | [diff] [blame] | 143 | """Gross hack to contort loader to deal w/ load_*()'s bad API.""" |
Brett Cannon | 938d44d | 2012-04-22 19:58:33 -0400 | [diff] [blame] | 144 | if self.file and path == self.path: |
Benjamin Peterson | b0274f2 | 2018-07-06 20:41:06 -0700 | [diff] [blame] | 145 | # The contract of get_data() requires us to return bytes. Reopen the |
| 146 | # file in binary mode if needed. |
Brett Cannon | a4975a9 | 2013-08-23 11:45:57 -0400 | [diff] [blame] | 147 | if not self.file.closed: |
| 148 | file = self.file |
Benjamin Peterson | b0274f2 | 2018-07-06 20:41:06 -0700 | [diff] [blame] | 149 | if 'b' not in file.mode: |
| 150 | file.close() |
| 151 | if self.file.closed: |
| 152 | self.file = file = open(self.path, 'rb') |
Brett Cannon | a4975a9 | 2013-08-23 11:45:57 -0400 | [diff] [blame] | 153 | |
| 154 | with file: |
Brett Cannon | a4975a9 | 2013-08-23 11:45:57 -0400 | [diff] [blame] | 155 | return file.read() |
Brett Cannon | 16475ad | 2012-04-16 22:11:25 -0400 | [diff] [blame] | 156 | else: |
| 157 | return super().get_data(path) |
| 158 | |
| 159 | |
Brett Cannon | 589c4ff | 2013-06-14 22:29:58 -0400 | [diff] [blame] | 160 | class _LoadSourceCompatibility(_HackedGetData, machinery.SourceFileLoader): |
Brett Cannon | 64befe9 | 2012-04-17 19:14:26 -0400 | [diff] [blame] | 161 | |
| 162 | """Compatibility support for implementing load_source().""" |
| 163 | |
| 164 | |
Brett Cannon | 16475ad | 2012-04-16 22:11:25 -0400 | [diff] [blame] | 165 | def load_source(name, pathname, file=None): |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 166 | loader = _LoadSourceCompatibility(name, pathname, file) |
| 167 | spec = util.spec_from_file_location(name, pathname, loader=loader) |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 168 | if name in sys.modules: |
Brett Cannon | 2a17bde | 2014-05-30 14:55:29 -0400 | [diff] [blame] | 169 | module = _exec(spec, sys.modules[name]) |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 170 | else: |
Brett Cannon | 2a17bde | 2014-05-30 14:55:29 -0400 | [diff] [blame] | 171 | module = _load(spec) |
Brett Cannon | 5a4c233 | 2013-04-28 11:53:26 -0400 | [diff] [blame] | 172 | # To allow reloading to potentially work, use a non-hacked loader which |
| 173 | # won't rely on a now-closed file object. |
Brett Cannon | 589c4ff | 2013-06-14 22:29:58 -0400 | [diff] [blame] | 174 | module.__loader__ = machinery.SourceFileLoader(name, pathname) |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 175 | module.__spec__.loader = module.__loader__ |
Brett Cannon | 5a4c233 | 2013-04-28 11:53:26 -0400 | [diff] [blame] | 176 | return module |
Brett Cannon | 16475ad | 2012-04-16 22:11:25 -0400 | [diff] [blame] | 177 | |
| 178 | |
Brett Cannon | 589c4ff | 2013-06-14 22:29:58 -0400 | [diff] [blame] | 179 | class _LoadCompiledCompatibility(_HackedGetData, SourcelessFileLoader): |
Brett Cannon | 64befe9 | 2012-04-17 19:14:26 -0400 | [diff] [blame] | 180 | |
| 181 | """Compatibility support for implementing load_compiled().""" |
| 182 | |
| 183 | |
| 184 | def load_compiled(name, pathname, file=None): |
Brett Cannon | e4f41de | 2013-06-16 13:13:40 -0400 | [diff] [blame] | 185 | """**DEPRECATED**""" |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 186 | loader = _LoadCompiledCompatibility(name, pathname, file) |
| 187 | spec = util.spec_from_file_location(name, pathname, loader=loader) |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 188 | if name in sys.modules: |
Brett Cannon | 2a17bde | 2014-05-30 14:55:29 -0400 | [diff] [blame] | 189 | module = _exec(spec, sys.modules[name]) |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 190 | else: |
Brett Cannon | 2a17bde | 2014-05-30 14:55:29 -0400 | [diff] [blame] | 191 | module = _load(spec) |
Brett Cannon | 5a4c233 | 2013-04-28 11:53:26 -0400 | [diff] [blame] | 192 | # To allow reloading to potentially work, use a non-hacked loader which |
| 193 | # won't rely on a now-closed file object. |
Brett Cannon | 589c4ff | 2013-06-14 22:29:58 -0400 | [diff] [blame] | 194 | module.__loader__ = SourcelessFileLoader(name, pathname) |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 195 | module.__spec__.loader = module.__loader__ |
Brett Cannon | 5a4c233 | 2013-04-28 11:53:26 -0400 | [diff] [blame] | 196 | return module |
Brett Cannon | 64befe9 | 2012-04-17 19:14:26 -0400 | [diff] [blame] | 197 | |
| 198 | |
Brett Cannon | 2ee6142 | 2012-04-15 22:28:28 -0400 | [diff] [blame] | 199 | def load_package(name, path): |
Brett Cannon | e4f41de | 2013-06-16 13:13:40 -0400 | [diff] [blame] | 200 | """**DEPRECATED**""" |
Brett Cannon | 2ee6142 | 2012-04-15 22:28:28 -0400 | [diff] [blame] | 201 | if os.path.isdir(path): |
Brett Cannon | c049952 | 2012-05-11 14:48:41 -0400 | [diff] [blame] | 202 | extensions = (machinery.SOURCE_SUFFIXES[:] + |
| 203 | machinery.BYTECODE_SUFFIXES[:]) |
Brett Cannon | 2ee6142 | 2012-04-15 22:28:28 -0400 | [diff] [blame] | 204 | for extension in extensions: |
Alexandru Ardelean | c38e32a | 2017-06-23 20:35:03 +0300 | [diff] [blame] | 205 | init_path = os.path.join(path, '__init__' + extension) |
| 206 | if os.path.exists(init_path): |
| 207 | path = init_path |
Brett Cannon | 2ee6142 | 2012-04-15 22:28:28 -0400 | [diff] [blame] | 208 | break |
| 209 | else: |
| 210 | raise ValueError('{!r} is not a package'.format(path)) |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 211 | spec = util.spec_from_file_location(name, path, |
| 212 | submodule_search_locations=[]) |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 213 | if name in sys.modules: |
Brett Cannon | 2a17bde | 2014-05-30 14:55:29 -0400 | [diff] [blame] | 214 | return _exec(spec, sys.modules[name]) |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 215 | else: |
Brett Cannon | 2a17bde | 2014-05-30 14:55:29 -0400 | [diff] [blame] | 216 | return _load(spec) |
Brett Cannon | 2ee6142 | 2012-04-15 22:28:28 -0400 | [diff] [blame] | 217 | |
Brett Cannon | 01a7617 | 2012-04-15 20:25:23 -0400 | [diff] [blame] | 218 | |
| 219 | def load_module(name, file, filename, details): |
Brett Cannon | 0450c9e | 2012-06-15 19:39:06 -0400 | [diff] [blame] | 220 | """**DEPRECATED** |
| 221 | |
| 222 | Load a module, given information returned by find_module(). |
Brett Cannon | 01a7617 | 2012-04-15 20:25:23 -0400 | [diff] [blame] | 223 | |
| 224 | The module name must include the full package name, if any. |
| 225 | |
| 226 | """ |
| 227 | suffix, mode, type_ = details |
Victor Stinner | 942f7a2 | 2020-03-04 18:50:22 +0100 | [diff] [blame] | 228 | if mode and (not mode.startswith(('r', 'U')) or '+' in mode): |
Brett Cannon | e4f41de | 2013-06-16 13:13:40 -0400 | [diff] [blame] | 229 | raise ValueError('invalid file open mode {!r}'.format(mode)) |
| 230 | elif file is None and type_ in {PY_SOURCE, PY_COMPILED}: |
| 231 | msg = 'file object required for import (type code {})'.format(type_) |
| 232 | raise ValueError(msg) |
| 233 | elif type_ == PY_SOURCE: |
| 234 | return load_source(name, filename, file) |
| 235 | elif type_ == PY_COMPILED: |
| 236 | return load_compiled(name, filename, file) |
| 237 | elif type_ == C_EXTENSION and load_dynamic is not None: |
| 238 | if file is None: |
| 239 | with open(filename, 'rb') as opened_file: |
| 240 | return load_dynamic(name, filename, opened_file) |
Brett Cannon | c049952 | 2012-05-11 14:48:41 -0400 | [diff] [blame] | 241 | else: |
Brett Cannon | e4f41de | 2013-06-16 13:13:40 -0400 | [diff] [blame] | 242 | return load_dynamic(name, filename, file) |
| 243 | elif type_ == PKG_DIRECTORY: |
| 244 | return load_package(name, filename) |
| 245 | elif type_ == C_BUILTIN: |
| 246 | return init_builtin(name) |
| 247 | elif type_ == PY_FROZEN: |
| 248 | return init_frozen(name) |
| 249 | else: |
| 250 | msg = "Don't know how to import {} (type code {})".format(name, type_) |
| 251 | raise ImportError(msg, name=name) |
Brett Cannon | e69f0df | 2012-04-21 21:09:46 -0400 | [diff] [blame] | 252 | |
| 253 | |
| 254 | def find_module(name, path=None): |
Brett Cannon | 0450c9e | 2012-06-15 19:39:06 -0400 | [diff] [blame] | 255 | """**DEPRECATED** |
| 256 | |
| 257 | Search for a module. |
Brett Cannon | e69f0df | 2012-04-21 21:09:46 -0400 | [diff] [blame] | 258 | |
| 259 | If path is omitted or None, search for a built-in, frozen or special |
| 260 | module and continue search in sys.path. The module name cannot |
| 261 | contain '.'; to search for a submodule of a package, pass the |
| 262 | submodule name and the package's __path__. |
| 263 | |
| 264 | """ |
| 265 | if not isinstance(name, str): |
| 266 | raise TypeError("'name' must be a str, not {}".format(type(name))) |
| 267 | elif not isinstance(path, (type(None), list)): |
| 268 | # Backwards-compatibility |
Brett Cannon | f76457e | 2016-07-15 10:58:54 -0700 | [diff] [blame] | 269 | raise RuntimeError("'path' must be None or a list, " |
| 270 | "not {}".format(type(path))) |
Brett Cannon | e69f0df | 2012-04-21 21:09:46 -0400 | [diff] [blame] | 271 | |
| 272 | if path is None: |
| 273 | if is_builtin(name): |
| 274 | return None, None, ('', '', C_BUILTIN) |
| 275 | elif is_frozen(name): |
| 276 | return None, None, ('', '', PY_FROZEN) |
| 277 | else: |
| 278 | path = sys.path |
| 279 | |
| 280 | for entry in path: |
| 281 | package_directory = os.path.join(entry, name) |
Brett Cannon | cb66eb0 | 2012-05-11 12:58:42 -0400 | [diff] [blame] | 282 | for suffix in ['.py', machinery.BYTECODE_SUFFIXES[0]]: |
Brett Cannon | e69f0df | 2012-04-21 21:09:46 -0400 | [diff] [blame] | 283 | package_file_name = '__init__' + suffix |
| 284 | file_path = os.path.join(package_directory, package_file_name) |
| 285 | if os.path.isfile(file_path): |
| 286 | return None, package_directory, ('', '', PKG_DIRECTORY) |
Brett Cannon | e4f41de | 2013-06-16 13:13:40 -0400 | [diff] [blame] | 287 | for suffix, mode, type_ in get_suffixes(): |
| 288 | file_name = name + suffix |
| 289 | file_path = os.path.join(entry, file_name) |
| 290 | if os.path.isfile(file_path): |
| 291 | break |
| 292 | else: |
| 293 | continue |
| 294 | break # Break out of outer loop when breaking out of inner loop. |
Brett Cannon | e69f0df | 2012-04-21 21:09:46 -0400 | [diff] [blame] | 295 | else: |
Brett Cannon | 589c4ff | 2013-06-14 22:29:58 -0400 | [diff] [blame] | 296 | raise ImportError(_ERR_MSG.format(name), name=name) |
Brett Cannon | e69f0df | 2012-04-21 21:09:46 -0400 | [diff] [blame] | 297 | |
| 298 | encoding = None |
Serhiy Storchaka | 6787a38 | 2013-11-23 22:12:06 +0200 | [diff] [blame] | 299 | if 'b' not in mode: |
Brett Cannon | e69f0df | 2012-04-21 21:09:46 -0400 | [diff] [blame] | 300 | with open(file_path, 'rb') as file: |
| 301 | encoding = tokenize.detect_encoding(file.readline)[0] |
| 302 | file = open(file_path, mode, encoding=encoding) |
| 303 | return file, file_path, (suffix, mode, type_) |
Brett Cannon | 62228db | 2012-04-29 14:38:11 -0400 | [diff] [blame] | 304 | |
| 305 | |
Brett Cannon | 62228db | 2012-04-29 14:38:11 -0400 | [diff] [blame] | 306 | def reload(module): |
Brett Cannon | 3fe35e6 | 2013-06-14 15:04:26 -0400 | [diff] [blame] | 307 | """**DEPRECATED** |
| 308 | |
| 309 | Reload the module and return it. |
Brett Cannon | 62228db | 2012-04-29 14:38:11 -0400 | [diff] [blame] | 310 | |
| 311 | The module must have been successfully imported before. |
| 312 | |
| 313 | """ |
Brett Cannon | 3fe35e6 | 2013-06-14 15:04:26 -0400 | [diff] [blame] | 314 | return importlib.reload(module) |
Nick Coghlan | d5cacbb | 2015-05-23 22:24:10 +1000 | [diff] [blame] | 315 | |
| 316 | |
| 317 | def init_builtin(name): |
| 318 | """**DEPRECATED** |
| 319 | |
| 320 | Load and return a built-in module by name, or None is such module doesn't |
| 321 | exist |
| 322 | """ |
| 323 | try: |
| 324 | return _builtin_from_name(name) |
| 325 | except ImportError: |
| 326 | return None |
| 327 | |
| 328 | |
| 329 | if create_dynamic: |
| 330 | def load_dynamic(name, path, file=None): |
| 331 | """**DEPRECATED** |
| 332 | |
| 333 | Load an extension module. |
| 334 | """ |
| 335 | import importlib.machinery |
| 336 | loader = importlib.machinery.ExtensionFileLoader(name, path) |
Nick Coghlan | 9d3c61c | 2015-09-05 21:05:05 +1000 | [diff] [blame] | 337 | |
| 338 | # Issue #24748: Skip the sys.modules check in _load_module_shim; |
| 339 | # always load new extension |
| 340 | spec = importlib.machinery.ModuleSpec( |
| 341 | name=name, loader=loader, origin=path) |
| 342 | return _load(spec) |
| 343 | |
Nick Coghlan | d5cacbb | 2015-05-23 22:24:10 +1000 | [diff] [blame] | 344 | else: |
| 345 | load_dynamic = None |