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 | 2fef4d2 | 2012-04-15 19:06:23 -0400 | [diff] [blame] | 10 | load_dynamic, get_frozen_object, is_frozen_package, |
| 11 | init_builtin, init_frozen, is_builtin, is_frozen, |
Brett Cannon | 2657df4 | 2012-05-04 15:20:40 -0400 | [diff] [blame^] | 12 | _fix_co_filename, extension_suffixes) |
Brett Cannon | 24117a7 | 2012-04-20 18:04:03 -0400 | [diff] [blame] | 13 | # Could move out of _imp, but not worth the code |
Brett Cannon | 2f92389 | 2012-04-21 18:55:51 -0400 | [diff] [blame] | 14 | from _imp import get_magic, get_tag |
Brett Cannon | 6f44d66 | 2012-04-15 16:08:47 -0400 | [diff] [blame] | 15 | |
Brett Cannon | 17098a5 | 2012-05-04 13:52:49 -0400 | [diff] [blame] | 16 | from importlib._bootstrap import new_module |
| 17 | from importlib._bootstrap import cache_from_source |
Brett Cannon | 01a7617 | 2012-04-15 20:25:23 -0400 | [diff] [blame] | 18 | |
Brett Cannon | 2ee6142 | 2012-04-15 22:28:28 -0400 | [diff] [blame] | 19 | from importlib import _bootstrap |
| 20 | import os |
Brett Cannon | e69f0df | 2012-04-21 21:09:46 -0400 | [diff] [blame] | 21 | import sys |
| 22 | import tokenize |
| 23 | |
| 24 | |
| 25 | # XXX "deprecate" once find_module(), load_module(), and get_suffixes() are |
| 26 | # deprecated. |
| 27 | SEARCH_ERROR = 0 |
| 28 | PY_SOURCE = 1 |
| 29 | PY_COMPILED = 2 |
| 30 | C_EXTENSION = 3 |
| 31 | PY_RESOURCE = 4 |
| 32 | PKG_DIRECTORY = 5 |
| 33 | C_BUILTIN = 6 |
| 34 | PY_FROZEN = 7 |
| 35 | PY_CODERESOURCE = 8 |
| 36 | IMP_HOOK = 9 |
Brett Cannon | 2ee6142 | 2012-04-15 22:28:28 -0400 | [diff] [blame] | 37 | |
| 38 | |
Brett Cannon | 2657df4 | 2012-05-04 15:20:40 -0400 | [diff] [blame^] | 39 | def get_suffixes(): |
| 40 | extensions = [(s, 'rb', C_EXTENSION) for s in extension_suffixes()] |
| 41 | source = [(s, 'U', PY_SOURCE) for s in _bootstrap._SOURCE_SUFFIXES] |
| 42 | bytecode = [(_bootstrap._BYTECODE_SUFFIX, 'rb', PY_COMPILED)] |
| 43 | |
| 44 | return extensions + source + bytecode |
| 45 | |
| 46 | |
Brett Cannon | a64faf0 | 2012-04-21 18:52:52 -0400 | [diff] [blame] | 47 | def source_from_cache(path): |
| 48 | """Given the path to a .pyc./.pyo file, return the path to its .py file. |
| 49 | |
| 50 | The .pyc/.pyo file does not need to exist; this simply returns the path to |
| 51 | the .py file calculated to correspond to the .pyc/.pyo file. If path does |
| 52 | not conform to PEP 3147 format, ValueError will be raised. |
| 53 | |
| 54 | """ |
| 55 | head, pycache_filename = os.path.split(path) |
| 56 | head, pycache = os.path.split(head) |
Brett Cannon | 17098a5 | 2012-05-04 13:52:49 -0400 | [diff] [blame] | 57 | if pycache != _bootstrap._PYCACHE: |
Brett Cannon | a64faf0 | 2012-04-21 18:52:52 -0400 | [diff] [blame] | 58 | raise ValueError('{} not bottom-level directory in ' |
Brett Cannon | 17098a5 | 2012-05-04 13:52:49 -0400 | [diff] [blame] | 59 | '{!r}'.format(_bootstrap._PYCACHE, path)) |
Brett Cannon | a64faf0 | 2012-04-21 18:52:52 -0400 | [diff] [blame] | 60 | if pycache_filename.count('.') != 2: |
| 61 | raise ValueError('expected only 2 dots in ' |
| 62 | '{!r}'.format(pycache_filename)) |
| 63 | base_filename = pycache_filename.partition('.')[0] |
Brett Cannon | 17098a5 | 2012-05-04 13:52:49 -0400 | [diff] [blame] | 64 | return os.path.join(head, base_filename + _bootstrap._SOURCE_SUFFIXES[0]) |
Brett Cannon | a64faf0 | 2012-04-21 18:52:52 -0400 | [diff] [blame] | 65 | |
| 66 | |
Brett Cannon | acf85cd | 2012-04-29 12:50:03 -0400 | [diff] [blame] | 67 | class NullImporter: |
| 68 | |
| 69 | """Null import object.""" |
| 70 | |
| 71 | def __init__(self, path): |
| 72 | if path == '': |
| 73 | raise ImportError('empty pathname', path='') |
| 74 | elif os.path.isdir(path): |
| 75 | raise ImportError('existing directory', path=path) |
| 76 | |
| 77 | def find_module(self, fullname): |
| 78 | """Always returns None.""" |
| 79 | return None |
| 80 | |
| 81 | |
Brett Cannon | 64befe9 | 2012-04-17 19:14:26 -0400 | [diff] [blame] | 82 | class _HackedGetData: |
Brett Cannon | 16475ad | 2012-04-16 22:11:25 -0400 | [diff] [blame] | 83 | |
Brett Cannon | 64befe9 | 2012-04-17 19:14:26 -0400 | [diff] [blame] | 84 | """Compatibiilty support for 'file' arguments of various load_*() |
| 85 | functions.""" |
Brett Cannon | 16475ad | 2012-04-16 22:11:25 -0400 | [diff] [blame] | 86 | |
| 87 | def __init__(self, fullname, path, file=None): |
| 88 | super().__init__(fullname, path) |
| 89 | self.file = file |
| 90 | |
| 91 | def get_data(self, path): |
Brett Cannon | 64befe9 | 2012-04-17 19:14:26 -0400 | [diff] [blame] | 92 | """Gross hack to contort loader to deal w/ load_*()'s bad API.""" |
Brett Cannon | 938d44d | 2012-04-22 19:58:33 -0400 | [diff] [blame] | 93 | if self.file and path == self.path: |
Brett Cannon | 16475ad | 2012-04-16 22:11:25 -0400 | [diff] [blame] | 94 | with self.file: |
| 95 | # Technically should be returning bytes, but |
| 96 | # SourceLoader.get_code() just passed what is returned to |
| 97 | # compile() which can handle str. And converting to bytes would |
| 98 | # require figuring out the encoding to decode to and |
| 99 | # tokenize.detect_encoding() only accepts bytes. |
| 100 | return self.file.read() |
| 101 | else: |
| 102 | return super().get_data(path) |
| 103 | |
| 104 | |
Brett Cannon | 938d44d | 2012-04-22 19:58:33 -0400 | [diff] [blame] | 105 | class _LoadSourceCompatibility(_HackedGetData, _bootstrap.SourceFileLoader): |
Brett Cannon | 64befe9 | 2012-04-17 19:14:26 -0400 | [diff] [blame] | 106 | |
| 107 | """Compatibility support for implementing load_source().""" |
| 108 | |
| 109 | |
Brett Cannon | a64faf0 | 2012-04-21 18:52:52 -0400 | [diff] [blame] | 110 | # XXX deprecate after better API exposed in importlib |
Brett Cannon | 16475ad | 2012-04-16 22:11:25 -0400 | [diff] [blame] | 111 | def load_source(name, pathname, file=None): |
| 112 | return _LoadSourceCompatibility(name, pathname, file).load_module(name) |
| 113 | |
| 114 | |
Brett Cannon | 64befe9 | 2012-04-17 19:14:26 -0400 | [diff] [blame] | 115 | class _LoadCompiledCompatibility(_HackedGetData, |
Marc-Andre Lemburg | 4fe29c9 | 2012-04-25 02:31:37 +0200 | [diff] [blame] | 116 | _bootstrap.SourcelessFileLoader): |
Brett Cannon | 64befe9 | 2012-04-17 19:14:26 -0400 | [diff] [blame] | 117 | |
| 118 | """Compatibility support for implementing load_compiled().""" |
| 119 | |
| 120 | |
Brett Cannon | a64faf0 | 2012-04-21 18:52:52 -0400 | [diff] [blame] | 121 | # XXX deprecate |
Brett Cannon | 64befe9 | 2012-04-17 19:14:26 -0400 | [diff] [blame] | 122 | def load_compiled(name, pathname, file=None): |
| 123 | return _LoadCompiledCompatibility(name, pathname, file).load_module(name) |
| 124 | |
| 125 | |
Brett Cannon | a64faf0 | 2012-04-21 18:52:52 -0400 | [diff] [blame] | 126 | # XXX deprecate |
Brett Cannon | 2ee6142 | 2012-04-15 22:28:28 -0400 | [diff] [blame] | 127 | def load_package(name, path): |
| 128 | if os.path.isdir(path): |
Brett Cannon | 2657df4 | 2012-05-04 15:20:40 -0400 | [diff] [blame^] | 129 | extensions = _bootstrap._SOURCE_SUFFIXES |
| 130 | extensions += [_bootstrap._BYTECODE_SUFFIX] |
Brett Cannon | 2ee6142 | 2012-04-15 22:28:28 -0400 | [diff] [blame] | 131 | for extension in extensions: |
| 132 | path = os.path.join(path, '__init__'+extension) |
| 133 | if os.path.exists(path): |
| 134 | break |
| 135 | else: |
| 136 | raise ValueError('{!r} is not a package'.format(path)) |
Brett Cannon | 938d44d | 2012-04-22 19:58:33 -0400 | [diff] [blame] | 137 | return _bootstrap.SourceFileLoader(name, path).load_module(name) |
Brett Cannon | 2ee6142 | 2012-04-15 22:28:28 -0400 | [diff] [blame] | 138 | |
Brett Cannon | 01a7617 | 2012-04-15 20:25:23 -0400 | [diff] [blame] | 139 | |
Brett Cannon | a64faf0 | 2012-04-21 18:52:52 -0400 | [diff] [blame] | 140 | # XXX deprecate |
Brett Cannon | 01a7617 | 2012-04-15 20:25:23 -0400 | [diff] [blame] | 141 | def load_module(name, file, filename, details): |
| 142 | """Load a module, given information returned by find_module(). |
| 143 | |
| 144 | The module name must include the full package name, if any. |
| 145 | |
| 146 | """ |
| 147 | suffix, mode, type_ = details |
Brett Cannon | de10bf4 | 2012-04-16 20:44:21 -0400 | [diff] [blame] | 148 | if mode and (not mode.startswith(('r', 'U')) or '+' in mode): |
Brett Cannon | 01a7617 | 2012-04-15 20:25:23 -0400 | [diff] [blame] | 149 | raise ValueError('invalid file open mode {!r}'.format(mode)) |
| 150 | elif file is None and type_ in {PY_SOURCE, PY_COMPILED}: |
| 151 | msg = 'file object required for import (type code {})'.format(type_) |
| 152 | raise ValueError(msg) |
| 153 | elif type_ == PY_SOURCE: |
| 154 | return load_source(name, filename, file) |
| 155 | elif type_ == PY_COMPILED: |
| 156 | return load_compiled(name, filename, file) |
| 157 | elif type_ == PKG_DIRECTORY: |
| 158 | return load_package(name, filename) |
| 159 | elif type_ == C_BUILTIN: |
| 160 | return init_builtin(name) |
| 161 | elif type_ == PY_FROZEN: |
| 162 | return init_frozen(name) |
| 163 | else: |
| 164 | msg = "Don't know how to import {} (type code {}".format(name, type_) |
| 165 | raise ImportError(msg, name=name) |
Brett Cannon | e69f0df | 2012-04-21 21:09:46 -0400 | [diff] [blame] | 166 | |
| 167 | |
| 168 | def find_module(name, path=None): |
| 169 | """Search for a module. |
| 170 | |
| 171 | If path is omitted or None, search for a built-in, frozen or special |
| 172 | module and continue search in sys.path. The module name cannot |
| 173 | contain '.'; to search for a submodule of a package, pass the |
| 174 | submodule name and the package's __path__. |
| 175 | |
| 176 | """ |
| 177 | if not isinstance(name, str): |
| 178 | raise TypeError("'name' must be a str, not {}".format(type(name))) |
| 179 | elif not isinstance(path, (type(None), list)): |
| 180 | # Backwards-compatibility |
| 181 | raise RuntimeError("'list' must be None or a list, " |
| 182 | "not {}".format(type(name))) |
| 183 | |
| 184 | if path is None: |
| 185 | if is_builtin(name): |
| 186 | return None, None, ('', '', C_BUILTIN) |
| 187 | elif is_frozen(name): |
| 188 | return None, None, ('', '', PY_FROZEN) |
| 189 | else: |
| 190 | path = sys.path |
| 191 | |
| 192 | for entry in path: |
| 193 | package_directory = os.path.join(entry, name) |
Brett Cannon | 17098a5 | 2012-05-04 13:52:49 -0400 | [diff] [blame] | 194 | for suffix in ['.py', _bootstrap._BYTECODE_SUFFIX]: |
Brett Cannon | e69f0df | 2012-04-21 21:09:46 -0400 | [diff] [blame] | 195 | package_file_name = '__init__' + suffix |
| 196 | file_path = os.path.join(package_directory, package_file_name) |
| 197 | if os.path.isfile(file_path): |
| 198 | return None, package_directory, ('', '', PKG_DIRECTORY) |
| 199 | for suffix, mode, type_ in get_suffixes(): |
| 200 | file_name = name + suffix |
| 201 | file_path = os.path.join(entry, file_name) |
| 202 | if os.path.isfile(file_path): |
| 203 | break |
| 204 | else: |
| 205 | continue |
| 206 | break # Break out of outer loop when breaking out of inner loop. |
| 207 | else: |
| 208 | raise ImportError('No module name {!r}'.format(name), name=name) |
| 209 | |
| 210 | encoding = None |
| 211 | if mode == 'U': |
| 212 | with open(file_path, 'rb') as file: |
| 213 | encoding = tokenize.detect_encoding(file.readline)[0] |
| 214 | file = open(file_path, mode, encoding=encoding) |
| 215 | return file, file_path, (suffix, mode, type_) |
Brett Cannon | 62228db | 2012-04-29 14:38:11 -0400 | [diff] [blame] | 216 | |
| 217 | |
| 218 | _RELOADING = {} |
| 219 | |
| 220 | def reload(module): |
| 221 | """Reload the module and return it. |
| 222 | |
| 223 | The module must have been successfully imported before. |
| 224 | |
| 225 | """ |
| 226 | if not module or type(module) != type(sys): |
| 227 | raise TypeError("reload() argument must be module") |
| 228 | name = module.__name__ |
| 229 | if name not in sys.modules: |
| 230 | msg = "module {} not in sys.modules" |
| 231 | raise ImportError(msg.format(name), name=name) |
| 232 | if name in _RELOADING: |
| 233 | return _RELOADING[name] |
| 234 | _RELOADING[name] = module |
| 235 | try: |
| 236 | parent_name = name.rpartition('.')[0] |
| 237 | if parent_name and parent_name not in sys.modules: |
| 238 | msg = "parent {!r} not in sys.modules" |
| 239 | raise ImportError(msg.format(parentname), name=parent_name) |
| 240 | return module.__loader__.load_module(name) |
| 241 | finally: |
| 242 | try: |
| 243 | del _RELOADING[name] |
| 244 | except KeyError: |
| 245 | pass |