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 |
| 9 | from _imp import (lock_held, acquire_lock, release_lock, reload, |
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, |
| 12 | _fix_co_filename) |
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 | ea59dbf | 2012-04-20 21:44:46 -0400 | [diff] [blame] | 15 | # Can (probably) move to importlib |
Brett Cannon | 2f92389 | 2012-04-21 18:55:51 -0400 | [diff] [blame] | 16 | from _imp import get_suffixes |
Brett Cannon | 6f44d66 | 2012-04-15 16:08:47 -0400 | [diff] [blame] | 17 | # Should be re-implemented here (and mostly deprecated) |
Brett Cannon | e69f0df | 2012-04-21 21:09:46 -0400 | [diff] [blame^] | 18 | from _imp import NullImporter |
Brett Cannon | 6f44d66 | 2012-04-15 16:08:47 -0400 | [diff] [blame] | 19 | |
| 20 | from importlib._bootstrap import _new_module as new_module |
Brett Cannon | ea59dbf | 2012-04-20 21:44:46 -0400 | [diff] [blame] | 21 | from importlib._bootstrap import _cache_from_source as cache_from_source |
Brett Cannon | 01a7617 | 2012-04-15 20:25:23 -0400 | [diff] [blame] | 22 | |
Brett Cannon | 2ee6142 | 2012-04-15 22:28:28 -0400 | [diff] [blame] | 23 | from importlib import _bootstrap |
| 24 | import os |
Brett Cannon | e69f0df | 2012-04-21 21:09:46 -0400 | [diff] [blame^] | 25 | import sys |
| 26 | import tokenize |
| 27 | |
| 28 | |
| 29 | # XXX "deprecate" once find_module(), load_module(), and get_suffixes() are |
| 30 | # deprecated. |
| 31 | SEARCH_ERROR = 0 |
| 32 | PY_SOURCE = 1 |
| 33 | PY_COMPILED = 2 |
| 34 | C_EXTENSION = 3 |
| 35 | PY_RESOURCE = 4 |
| 36 | PKG_DIRECTORY = 5 |
| 37 | C_BUILTIN = 6 |
| 38 | PY_FROZEN = 7 |
| 39 | PY_CODERESOURCE = 8 |
| 40 | IMP_HOOK = 9 |
Brett Cannon | 2ee6142 | 2012-04-15 22:28:28 -0400 | [diff] [blame] | 41 | |
| 42 | |
Brett Cannon | a64faf0 | 2012-04-21 18:52:52 -0400 | [diff] [blame] | 43 | def source_from_cache(path): |
| 44 | """Given the path to a .pyc./.pyo file, return the path to its .py file. |
| 45 | |
| 46 | The .pyc/.pyo file does not need to exist; this simply returns the path to |
| 47 | the .py file calculated to correspond to the .pyc/.pyo file. If path does |
| 48 | not conform to PEP 3147 format, ValueError will be raised. |
| 49 | |
| 50 | """ |
| 51 | head, pycache_filename = os.path.split(path) |
| 52 | head, pycache = os.path.split(head) |
| 53 | if pycache != _bootstrap.PYCACHE: |
| 54 | raise ValueError('{} not bottom-level directory in ' |
| 55 | '{!r}'.format(_bootstrap.PYCACHE, path)) |
| 56 | if pycache_filename.count('.') != 2: |
| 57 | raise ValueError('expected only 2 dots in ' |
| 58 | '{!r}'.format(pycache_filename)) |
| 59 | base_filename = pycache_filename.partition('.')[0] |
| 60 | return os.path.join(head, base_filename + _bootstrap.SOURCE_SUFFIXES[0]) |
| 61 | |
| 62 | |
Brett Cannon | 64befe9 | 2012-04-17 19:14:26 -0400 | [diff] [blame] | 63 | class _HackedGetData: |
Brett Cannon | 16475ad | 2012-04-16 22:11:25 -0400 | [diff] [blame] | 64 | |
Brett Cannon | 64befe9 | 2012-04-17 19:14:26 -0400 | [diff] [blame] | 65 | """Compatibiilty support for 'file' arguments of various load_*() |
| 66 | functions.""" |
Brett Cannon | 16475ad | 2012-04-16 22:11:25 -0400 | [diff] [blame] | 67 | |
| 68 | def __init__(self, fullname, path, file=None): |
| 69 | super().__init__(fullname, path) |
| 70 | self.file = file |
| 71 | |
| 72 | def get_data(self, path): |
Brett Cannon | 64befe9 | 2012-04-17 19:14:26 -0400 | [diff] [blame] | 73 | """Gross hack to contort loader to deal w/ load_*()'s bad API.""" |
Brett Cannon | 578393b | 2012-04-16 23:11:28 -0400 | [diff] [blame] | 74 | if self.file and path == self._path: |
Brett Cannon | 16475ad | 2012-04-16 22:11:25 -0400 | [diff] [blame] | 75 | with self.file: |
| 76 | # Technically should be returning bytes, but |
| 77 | # SourceLoader.get_code() just passed what is returned to |
| 78 | # compile() which can handle str. And converting to bytes would |
| 79 | # require figuring out the encoding to decode to and |
| 80 | # tokenize.detect_encoding() only accepts bytes. |
| 81 | return self.file.read() |
| 82 | else: |
| 83 | return super().get_data(path) |
| 84 | |
| 85 | |
Brett Cannon | 64befe9 | 2012-04-17 19:14:26 -0400 | [diff] [blame] | 86 | class _LoadSourceCompatibility(_HackedGetData, _bootstrap._SourceFileLoader): |
| 87 | |
| 88 | """Compatibility support for implementing load_source().""" |
| 89 | |
| 90 | |
Brett Cannon | a64faf0 | 2012-04-21 18:52:52 -0400 | [diff] [blame] | 91 | # XXX deprecate after better API exposed in importlib |
Brett Cannon | 16475ad | 2012-04-16 22:11:25 -0400 | [diff] [blame] | 92 | def load_source(name, pathname, file=None): |
| 93 | return _LoadSourceCompatibility(name, pathname, file).load_module(name) |
| 94 | |
| 95 | |
Brett Cannon | 64befe9 | 2012-04-17 19:14:26 -0400 | [diff] [blame] | 96 | class _LoadCompiledCompatibility(_HackedGetData, |
| 97 | _bootstrap._SourcelessFileLoader): |
| 98 | |
| 99 | """Compatibility support for implementing load_compiled().""" |
| 100 | |
| 101 | |
Brett Cannon | a64faf0 | 2012-04-21 18:52:52 -0400 | [diff] [blame] | 102 | # XXX deprecate |
Brett Cannon | 64befe9 | 2012-04-17 19:14:26 -0400 | [diff] [blame] | 103 | def load_compiled(name, pathname, file=None): |
| 104 | return _LoadCompiledCompatibility(name, pathname, file).load_module(name) |
| 105 | |
| 106 | |
Brett Cannon | a64faf0 | 2012-04-21 18:52:52 -0400 | [diff] [blame] | 107 | # XXX deprecate |
Brett Cannon | 2ee6142 | 2012-04-15 22:28:28 -0400 | [diff] [blame] | 108 | def load_package(name, path): |
| 109 | if os.path.isdir(path): |
| 110 | extensions = _bootstrap._suffix_list(PY_SOURCE) |
| 111 | extensions += _bootstrap._suffix_list(PY_COMPILED) |
| 112 | for extension in extensions: |
| 113 | path = os.path.join(path, '__init__'+extension) |
| 114 | if os.path.exists(path): |
| 115 | break |
| 116 | else: |
| 117 | raise ValueError('{!r} is not a package'.format(path)) |
| 118 | return _bootstrap._SourceFileLoader(name, path).load_module(name) |
| 119 | |
Brett Cannon | 01a7617 | 2012-04-15 20:25:23 -0400 | [diff] [blame] | 120 | |
Brett Cannon | a64faf0 | 2012-04-21 18:52:52 -0400 | [diff] [blame] | 121 | # XXX deprecate |
Brett Cannon | 01a7617 | 2012-04-15 20:25:23 -0400 | [diff] [blame] | 122 | def load_module(name, file, filename, details): |
| 123 | """Load a module, given information returned by find_module(). |
| 124 | |
| 125 | The module name must include the full package name, if any. |
| 126 | |
| 127 | """ |
| 128 | suffix, mode, type_ = details |
Brett Cannon | de10bf4 | 2012-04-16 20:44:21 -0400 | [diff] [blame] | 129 | if mode and (not mode.startswith(('r', 'U')) or '+' in mode): |
Brett Cannon | 01a7617 | 2012-04-15 20:25:23 -0400 | [diff] [blame] | 130 | raise ValueError('invalid file open mode {!r}'.format(mode)) |
| 131 | elif file is None and type_ in {PY_SOURCE, PY_COMPILED}: |
| 132 | msg = 'file object required for import (type code {})'.format(type_) |
| 133 | raise ValueError(msg) |
| 134 | elif type_ == PY_SOURCE: |
| 135 | return load_source(name, filename, file) |
| 136 | elif type_ == PY_COMPILED: |
| 137 | return load_compiled(name, filename, file) |
| 138 | elif type_ == PKG_DIRECTORY: |
| 139 | return load_package(name, filename) |
| 140 | elif type_ == C_BUILTIN: |
| 141 | return init_builtin(name) |
| 142 | elif type_ == PY_FROZEN: |
| 143 | return init_frozen(name) |
| 144 | else: |
| 145 | msg = "Don't know how to import {} (type code {}".format(name, type_) |
| 146 | raise ImportError(msg, name=name) |
Brett Cannon | e69f0df | 2012-04-21 21:09:46 -0400 | [diff] [blame^] | 147 | |
| 148 | |
| 149 | def find_module(name, path=None): |
| 150 | """Search for a module. |
| 151 | |
| 152 | If path is omitted or None, search for a built-in, frozen or special |
| 153 | module and continue search in sys.path. The module name cannot |
| 154 | contain '.'; to search for a submodule of a package, pass the |
| 155 | submodule name and the package's __path__. |
| 156 | |
| 157 | """ |
| 158 | if not isinstance(name, str): |
| 159 | raise TypeError("'name' must be a str, not {}".format(type(name))) |
| 160 | elif not isinstance(path, (type(None), list)): |
| 161 | # Backwards-compatibility |
| 162 | raise RuntimeError("'list' must be None or a list, " |
| 163 | "not {}".format(type(name))) |
| 164 | |
| 165 | if path is None: |
| 166 | if is_builtin(name): |
| 167 | return None, None, ('', '', C_BUILTIN) |
| 168 | elif is_frozen(name): |
| 169 | return None, None, ('', '', PY_FROZEN) |
| 170 | else: |
| 171 | path = sys.path |
| 172 | |
| 173 | for entry in path: |
| 174 | package_directory = os.path.join(entry, name) |
| 175 | for suffix in ['.py', _bootstrap.BYTECODE_SUFFIX]: |
| 176 | package_file_name = '__init__' + suffix |
| 177 | file_path = os.path.join(package_directory, package_file_name) |
| 178 | if os.path.isfile(file_path): |
| 179 | return None, package_directory, ('', '', PKG_DIRECTORY) |
| 180 | for suffix, mode, type_ in get_suffixes(): |
| 181 | file_name = name + suffix |
| 182 | file_path = os.path.join(entry, file_name) |
| 183 | if os.path.isfile(file_path): |
| 184 | break |
| 185 | else: |
| 186 | continue |
| 187 | break # Break out of outer loop when breaking out of inner loop. |
| 188 | else: |
| 189 | raise ImportError('No module name {!r}'.format(name), name=name) |
| 190 | |
| 191 | encoding = None |
| 192 | if mode == 'U': |
| 193 | with open(file_path, 'rb') as file: |
| 194 | encoding = tokenize.detect_encoding(file.readline)[0] |
| 195 | file = open(file_path, mode, encoding=encoding) |
| 196 | return file, file_path, (suffix, mode, type_) |
| 197 | |