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 | 0a14066 | 2013-06-13 20:57:26 -0400 | [diff] [blame] | 15 | except ModuleNotFoundError: |
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 | 77b2abd | 2012-07-09 16:09:00 -0400 | [diff] [blame] | 19 | # Directly exposed by this module |
Brett Cannon | 17098a5 | 2012-05-04 13:52:49 -0400 | [diff] [blame] | 20 | from importlib._bootstrap import new_module |
Brett Cannon | a6473f9 | 2012-07-13 13:57:03 -0400 | [diff] [blame] | 21 | from importlib._bootstrap import cache_from_source, source_from_cache |
Brett Cannon | 01a7617 | 2012-04-15 20:25:23 -0400 | [diff] [blame] | 22 | |
Brett Cannon | 77b2abd | 2012-07-09 16:09:00 -0400 | [diff] [blame] | 23 | |
Brett Cannon | 2ee6142 | 2012-04-15 22:28:28 -0400 | [diff] [blame] | 24 | from importlib import _bootstrap |
Brett Cannon | cb66eb0 | 2012-05-11 12:58:42 -0400 | [diff] [blame] | 25 | from importlib import machinery |
Brett Cannon | 05a647d | 2013-06-14 19:02:34 -0400 | [diff] [blame^] | 26 | from importlib import util |
Brett Cannon | 3fe35e6 | 2013-06-14 15:04:26 -0400 | [diff] [blame] | 27 | import importlib |
Brett Cannon | 2ee6142 | 2012-04-15 22:28:28 -0400 | [diff] [blame] | 28 | import os |
Brett Cannon | e69f0df | 2012-04-21 21:09:46 -0400 | [diff] [blame] | 29 | import sys |
| 30 | import tokenize |
Brett Cannon | cb66eb0 | 2012-05-11 12:58:42 -0400 | [diff] [blame] | 31 | import warnings |
Brett Cannon | e69f0df | 2012-04-21 21:09:46 -0400 | [diff] [blame] | 32 | |
| 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 | 77b2abd | 2012-07-09 16:09:00 -0400 | [diff] [blame] | 47 | def get_magic(): |
Brett Cannon | 05a647d | 2013-06-14 19:02:34 -0400 | [diff] [blame^] | 48 | """**DEPRECATED** |
| 49 | |
| 50 | Return the magic number for .pyc or .pyo files. |
| 51 | """ |
| 52 | return util.MAGIC_NUMBER |
Brett Cannon | 77b2abd | 2012-07-09 16:09:00 -0400 | [diff] [blame] | 53 | |
| 54 | |
Brett Cannon | 98979b8 | 2012-07-02 15:13:11 -0400 | [diff] [blame] | 55 | def get_tag(): |
| 56 | """Return the magic tag for .pyc or .pyo files.""" |
| 57 | return sys.implementation.cache_tag |
| 58 | |
| 59 | |
Brett Cannon | 2657df4 | 2012-05-04 15:20:40 -0400 | [diff] [blame] | 60 | def get_suffixes(): |
Brett Cannon | cb66eb0 | 2012-05-11 12:58:42 -0400 | [diff] [blame] | 61 | warnings.warn('imp.get_suffixes() is deprecated; use the constants ' |
| 62 | 'defined on importlib.machinery instead', |
| 63 | DeprecationWarning, 2) |
Brett Cannon | ac9f2f3 | 2012-08-10 13:47:54 -0400 | [diff] [blame] | 64 | extensions = [(s, 'rb', C_EXTENSION) for s in machinery.EXTENSION_SUFFIXES] |
Brett Cannon | cb66eb0 | 2012-05-11 12:58:42 -0400 | [diff] [blame] | 65 | source = [(s, 'U', PY_SOURCE) for s in machinery.SOURCE_SUFFIXES] |
| 66 | bytecode = [(s, 'rb', PY_COMPILED) for s in machinery.BYTECODE_SUFFIXES] |
Brett Cannon | 2657df4 | 2012-05-04 15:20:40 -0400 | [diff] [blame] | 67 | |
| 68 | return extensions + source + bytecode |
| 69 | |
| 70 | |
Brett Cannon | acf85cd | 2012-04-29 12:50:03 -0400 | [diff] [blame] | 71 | class NullImporter: |
| 72 | |
| 73 | """Null import object.""" |
| 74 | |
| 75 | def __init__(self, path): |
| 76 | if path == '': |
| 77 | raise ImportError('empty pathname', path='') |
| 78 | elif os.path.isdir(path): |
| 79 | raise ImportError('existing directory', path=path) |
| 80 | |
| 81 | def find_module(self, fullname): |
| 82 | """Always returns None.""" |
| 83 | return None |
| 84 | |
| 85 | |
Brett Cannon | 64befe9 | 2012-04-17 19:14:26 -0400 | [diff] [blame] | 86 | class _HackedGetData: |
Brett Cannon | 16475ad | 2012-04-16 22:11:25 -0400 | [diff] [blame] | 87 | |
Brett Cannon | 64befe9 | 2012-04-17 19:14:26 -0400 | [diff] [blame] | 88 | """Compatibiilty support for 'file' arguments of various load_*() |
| 89 | functions.""" |
Brett Cannon | 16475ad | 2012-04-16 22:11:25 -0400 | [diff] [blame] | 90 | |
| 91 | def __init__(self, fullname, path, file=None): |
| 92 | super().__init__(fullname, path) |
| 93 | self.file = file |
| 94 | |
| 95 | def get_data(self, path): |
Brett Cannon | 64befe9 | 2012-04-17 19:14:26 -0400 | [diff] [blame] | 96 | """Gross hack to contort loader to deal w/ load_*()'s bad API.""" |
Brett Cannon | 938d44d | 2012-04-22 19:58:33 -0400 | [diff] [blame] | 97 | if self.file and path == self.path: |
Brett Cannon | 16475ad | 2012-04-16 22:11:25 -0400 | [diff] [blame] | 98 | with self.file: |
| 99 | # Technically should be returning bytes, but |
| 100 | # SourceLoader.get_code() just passed what is returned to |
| 101 | # compile() which can handle str. And converting to bytes would |
| 102 | # require figuring out the encoding to decode to and |
| 103 | # tokenize.detect_encoding() only accepts bytes. |
| 104 | return self.file.read() |
| 105 | else: |
| 106 | return super().get_data(path) |
| 107 | |
| 108 | |
Brett Cannon | 938d44d | 2012-04-22 19:58:33 -0400 | [diff] [blame] | 109 | class _LoadSourceCompatibility(_HackedGetData, _bootstrap.SourceFileLoader): |
Brett Cannon | 64befe9 | 2012-04-17 19:14:26 -0400 | [diff] [blame] | 110 | |
| 111 | """Compatibility support for implementing load_source().""" |
| 112 | |
| 113 | |
Brett Cannon | 16475ad | 2012-04-16 22:11:25 -0400 | [diff] [blame] | 114 | def load_source(name, pathname, file=None): |
Brett Cannon | c049952 | 2012-05-11 14:48:41 -0400 | [diff] [blame] | 115 | msg = ('imp.load_source() is deprecated; use ' |
| 116 | 'importlib.machinery.SourceFileLoader(name, pathname).load_module()' |
| 117 | ' instead') |
| 118 | warnings.warn(msg, DeprecationWarning, 2) |
Brett Cannon | 5a4c233 | 2013-04-28 11:53:26 -0400 | [diff] [blame] | 119 | _LoadSourceCompatibility(name, pathname, file).load_module(name) |
| 120 | module = sys.modules[name] |
| 121 | # To allow reloading to potentially work, use a non-hacked loader which |
| 122 | # won't rely on a now-closed file object. |
| 123 | module.__loader__ = _bootstrap.SourceFileLoader(name, pathname) |
| 124 | return module |
Brett Cannon | 16475ad | 2012-04-16 22:11:25 -0400 | [diff] [blame] | 125 | |
| 126 | |
Brett Cannon | 64befe9 | 2012-04-17 19:14:26 -0400 | [diff] [blame] | 127 | class _LoadCompiledCompatibility(_HackedGetData, |
Marc-Andre Lemburg | 4fe29c9 | 2012-04-25 02:31:37 +0200 | [diff] [blame] | 128 | _bootstrap.SourcelessFileLoader): |
Brett Cannon | 64befe9 | 2012-04-17 19:14:26 -0400 | [diff] [blame] | 129 | |
| 130 | """Compatibility support for implementing load_compiled().""" |
| 131 | |
| 132 | |
| 133 | def load_compiled(name, pathname, file=None): |
Brett Cannon | c049952 | 2012-05-11 14:48:41 -0400 | [diff] [blame] | 134 | msg = ('imp.load_compiled() is deprecated; use ' |
| 135 | 'importlib.machinery.SourcelessFileLoader(name, pathname).' |
| 136 | 'load_module() instead ') |
| 137 | warnings.warn(msg, DeprecationWarning, 2) |
Brett Cannon | 5a4c233 | 2013-04-28 11:53:26 -0400 | [diff] [blame] | 138 | _LoadCompiledCompatibility(name, pathname, file).load_module(name) |
| 139 | module = sys.modules[name] |
| 140 | # To allow reloading to potentially work, use a non-hacked loader which |
| 141 | # won't rely on a now-closed file object. |
| 142 | module.__loader__ = _bootstrap.SourcelessFileLoader(name, pathname) |
| 143 | return module |
Brett Cannon | 64befe9 | 2012-04-17 19:14:26 -0400 | [diff] [blame] | 144 | |
| 145 | |
Brett Cannon | 2ee6142 | 2012-04-15 22:28:28 -0400 | [diff] [blame] | 146 | def load_package(name, path): |
Brett Cannon | c049952 | 2012-05-11 14:48:41 -0400 | [diff] [blame] | 147 | msg = ('imp.load_package() is deprecated; use either ' |
| 148 | 'importlib.machinery.SourceFileLoader() or ' |
| 149 | 'importlib.machinery.SourcelessFileLoader() instead') |
| 150 | warnings.warn(msg, DeprecationWarning, 2) |
Brett Cannon | 2ee6142 | 2012-04-15 22:28:28 -0400 | [diff] [blame] | 151 | if os.path.isdir(path): |
Brett Cannon | c049952 | 2012-05-11 14:48:41 -0400 | [diff] [blame] | 152 | extensions = (machinery.SOURCE_SUFFIXES[:] + |
| 153 | machinery.BYTECODE_SUFFIXES[:]) |
Brett Cannon | 2ee6142 | 2012-04-15 22:28:28 -0400 | [diff] [blame] | 154 | for extension in extensions: |
| 155 | path = os.path.join(path, '__init__'+extension) |
| 156 | if os.path.exists(path): |
| 157 | break |
| 158 | else: |
| 159 | raise ValueError('{!r} is not a package'.format(path)) |
Brett Cannon | 938d44d | 2012-04-22 19:58:33 -0400 | [diff] [blame] | 160 | return _bootstrap.SourceFileLoader(name, path).load_module(name) |
Brett Cannon | 2ee6142 | 2012-04-15 22:28:28 -0400 | [diff] [blame] | 161 | |
Brett Cannon | 01a7617 | 2012-04-15 20:25:23 -0400 | [diff] [blame] | 162 | |
| 163 | def load_module(name, file, filename, details): |
Brett Cannon | 0450c9e | 2012-06-15 19:39:06 -0400 | [diff] [blame] | 164 | """**DEPRECATED** |
| 165 | |
| 166 | Load a module, given information returned by find_module(). |
Brett Cannon | 01a7617 | 2012-04-15 20:25:23 -0400 | [diff] [blame] | 167 | |
| 168 | The module name must include the full package name, if any. |
| 169 | |
| 170 | """ |
| 171 | suffix, mode, type_ = details |
Brett Cannon | c049952 | 2012-05-11 14:48:41 -0400 | [diff] [blame] | 172 | with warnings.catch_warnings(): |
| 173 | warnings.simplefilter('ignore') |
| 174 | if mode and (not mode.startswith(('r', 'U')) or '+' in mode): |
| 175 | raise ValueError('invalid file open mode {!r}'.format(mode)) |
Brett Cannon | 9d0f772 | 2013-05-03 10:37:08 -0400 | [diff] [blame] | 176 | elif file is None and type_ in {PY_SOURCE, PY_COMPILED}: |
Brett Cannon | c049952 | 2012-05-11 14:48:41 -0400 | [diff] [blame] | 177 | msg = 'file object required for import (type code {})'.format(type_) |
| 178 | raise ValueError(msg) |
| 179 | elif type_ == PY_SOURCE: |
| 180 | return load_source(name, filename, file) |
| 181 | elif type_ == PY_COMPILED: |
| 182 | return load_compiled(name, filename, file) |
Brett Cannon | 3e2fe05 | 2013-03-17 15:48:16 -0700 | [diff] [blame] | 183 | elif type_ == C_EXTENSION and load_dynamic is not None: |
Brett Cannon | 9d0f772 | 2013-05-03 10:37:08 -0400 | [diff] [blame] | 184 | if file is None: |
| 185 | with open(filename, 'rb') as opened_file: |
| 186 | return load_dynamic(name, filename, opened_file) |
| 187 | else: |
| 188 | return load_dynamic(name, filename, file) |
Brett Cannon | c049952 | 2012-05-11 14:48:41 -0400 | [diff] [blame] | 189 | elif type_ == PKG_DIRECTORY: |
| 190 | return load_package(name, filename) |
| 191 | elif type_ == C_BUILTIN: |
| 192 | return init_builtin(name) |
| 193 | elif type_ == PY_FROZEN: |
| 194 | return init_frozen(name) |
| 195 | else: |
Nick Coghlan | 91b9f13 | 2012-09-01 00:13:45 +1000 | [diff] [blame] | 196 | msg = "Don't know how to import {} (type code {})".format(name, type_) |
Brett Cannon | c049952 | 2012-05-11 14:48:41 -0400 | [diff] [blame] | 197 | raise ImportError(msg, name=name) |
Brett Cannon | e69f0df | 2012-04-21 21:09:46 -0400 | [diff] [blame] | 198 | |
| 199 | |
| 200 | def find_module(name, path=None): |
Brett Cannon | 0450c9e | 2012-06-15 19:39:06 -0400 | [diff] [blame] | 201 | """**DEPRECATED** |
| 202 | |
| 203 | Search for a module. |
Brett Cannon | e69f0df | 2012-04-21 21:09:46 -0400 | [diff] [blame] | 204 | |
| 205 | If path is omitted or None, search for a built-in, frozen or special |
| 206 | module and continue search in sys.path. The module name cannot |
| 207 | contain '.'; to search for a submodule of a package, pass the |
| 208 | submodule name and the package's __path__. |
| 209 | |
| 210 | """ |
| 211 | if not isinstance(name, str): |
| 212 | raise TypeError("'name' must be a str, not {}".format(type(name))) |
| 213 | elif not isinstance(path, (type(None), list)): |
| 214 | # Backwards-compatibility |
| 215 | raise RuntimeError("'list' must be None or a list, " |
| 216 | "not {}".format(type(name))) |
| 217 | |
| 218 | if path is None: |
| 219 | if is_builtin(name): |
| 220 | return None, None, ('', '', C_BUILTIN) |
| 221 | elif is_frozen(name): |
| 222 | return None, None, ('', '', PY_FROZEN) |
| 223 | else: |
| 224 | path = sys.path |
| 225 | |
| 226 | for entry in path: |
| 227 | package_directory = os.path.join(entry, name) |
Brett Cannon | cb66eb0 | 2012-05-11 12:58:42 -0400 | [diff] [blame] | 228 | for suffix in ['.py', machinery.BYTECODE_SUFFIXES[0]]: |
Brett Cannon | e69f0df | 2012-04-21 21:09:46 -0400 | [diff] [blame] | 229 | package_file_name = '__init__' + suffix |
| 230 | file_path = os.path.join(package_directory, package_file_name) |
| 231 | if os.path.isfile(file_path): |
| 232 | return None, package_directory, ('', '', PKG_DIRECTORY) |
Brett Cannon | cb66eb0 | 2012-05-11 12:58:42 -0400 | [diff] [blame] | 233 | with warnings.catch_warnings(): |
| 234 | warnings.simplefilter('ignore') |
| 235 | for suffix, mode, type_ in get_suffixes(): |
| 236 | file_name = name + suffix |
| 237 | file_path = os.path.join(entry, file_name) |
| 238 | if os.path.isfile(file_path): |
| 239 | break |
| 240 | else: |
| 241 | continue |
| 242 | break # Break out of outer loop when breaking out of inner loop. |
Brett Cannon | e69f0df | 2012-04-21 21:09:46 -0400 | [diff] [blame] | 243 | else: |
Brett Cannon | bf7eab0 | 2012-07-09 13:24:34 -0400 | [diff] [blame] | 244 | raise ImportError(_bootstrap._ERR_MSG.format(name), name=name) |
Brett Cannon | e69f0df | 2012-04-21 21:09:46 -0400 | [diff] [blame] | 245 | |
| 246 | encoding = None |
| 247 | if mode == 'U': |
| 248 | with open(file_path, 'rb') as file: |
| 249 | encoding = tokenize.detect_encoding(file.readline)[0] |
| 250 | file = open(file_path, mode, encoding=encoding) |
| 251 | return file, file_path, (suffix, mode, type_) |
Brett Cannon | 62228db | 2012-04-29 14:38:11 -0400 | [diff] [blame] | 252 | |
| 253 | |
Brett Cannon | 62228db | 2012-04-29 14:38:11 -0400 | [diff] [blame] | 254 | def reload(module): |
Brett Cannon | 3fe35e6 | 2013-06-14 15:04:26 -0400 | [diff] [blame] | 255 | """**DEPRECATED** |
| 256 | |
| 257 | Reload the module and return it. |
Brett Cannon | 62228db | 2012-04-29 14:38:11 -0400 | [diff] [blame] | 258 | |
| 259 | The module must have been successfully imported before. |
| 260 | |
| 261 | """ |
Brett Cannon | 3fe35e6 | 2013-06-14 15:04:26 -0400 | [diff] [blame] | 262 | return importlib.reload(module) |