blob: 3116347775381e3d786f77187e7667f33b2fa335 [file] [log] [blame]
Brett Cannon6f44d662012-04-15 16:08:47 -04001"""This module provides the components needed to build your own __import__
2function. Undocumented functions are obsolete.
3
4In most cases it is preferred you consider using the importlib module's
5functionality over this module.
6
7"""
8# (Probably) need to stay in _imp
Brett Cannon62228db2012-04-29 14:38:11 -04009from _imp import (lock_held, acquire_lock, release_lock,
Brett Cannon2fef4d22012-04-15 19:06:23 -040010 load_dynamic, get_frozen_object, is_frozen_package,
11 init_builtin, init_frozen, is_builtin, is_frozen,
Brett Cannon2657df42012-05-04 15:20:40 -040012 _fix_co_filename, extension_suffixes)
Brett Cannon24117a72012-04-20 18:04:03 -040013# Could move out of _imp, but not worth the code
Brett Cannon2f923892012-04-21 18:55:51 -040014from _imp import get_magic, get_tag
Brett Cannon6f44d662012-04-15 16:08:47 -040015
Brett Cannon17098a52012-05-04 13:52:49 -040016from importlib._bootstrap import new_module
17from importlib._bootstrap import cache_from_source
Brett Cannon01a76172012-04-15 20:25:23 -040018
Brett Cannon2ee61422012-04-15 22:28:28 -040019from importlib import _bootstrap
20import os
Brett Cannone69f0df2012-04-21 21:09:46 -040021import sys
22import tokenize
23
24
25# XXX "deprecate" once find_module(), load_module(), and get_suffixes() are
26# deprecated.
27SEARCH_ERROR = 0
28PY_SOURCE = 1
29PY_COMPILED = 2
30C_EXTENSION = 3
31PY_RESOURCE = 4
32PKG_DIRECTORY = 5
33C_BUILTIN = 6
34PY_FROZEN = 7
35PY_CODERESOURCE = 8
36IMP_HOOK = 9
Brett Cannon2ee61422012-04-15 22:28:28 -040037
38
Brett Cannon2657df42012-05-04 15:20:40 -040039def 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 Cannona64faf02012-04-21 18:52:52 -040047def 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 Cannon17098a52012-05-04 13:52:49 -040057 if pycache != _bootstrap._PYCACHE:
Brett Cannona64faf02012-04-21 18:52:52 -040058 raise ValueError('{} not bottom-level directory in '
Brett Cannon17098a52012-05-04 13:52:49 -040059 '{!r}'.format(_bootstrap._PYCACHE, path))
Brett Cannona64faf02012-04-21 18:52:52 -040060 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 Cannon17098a52012-05-04 13:52:49 -040064 return os.path.join(head, base_filename + _bootstrap._SOURCE_SUFFIXES[0])
Brett Cannona64faf02012-04-21 18:52:52 -040065
66
Brett Cannonacf85cd2012-04-29 12:50:03 -040067class 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 Cannon64befe92012-04-17 19:14:26 -040082class _HackedGetData:
Brett Cannon16475ad2012-04-16 22:11:25 -040083
Brett Cannon64befe92012-04-17 19:14:26 -040084 """Compatibiilty support for 'file' arguments of various load_*()
85 functions."""
Brett Cannon16475ad2012-04-16 22:11:25 -040086
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 Cannon64befe92012-04-17 19:14:26 -040092 """Gross hack to contort loader to deal w/ load_*()'s bad API."""
Brett Cannon938d44d2012-04-22 19:58:33 -040093 if self.file and path == self.path:
Brett Cannon16475ad2012-04-16 22:11:25 -040094 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 Cannon938d44d2012-04-22 19:58:33 -0400105class _LoadSourceCompatibility(_HackedGetData, _bootstrap.SourceFileLoader):
Brett Cannon64befe92012-04-17 19:14:26 -0400106
107 """Compatibility support for implementing load_source()."""
108
109
Brett Cannona64faf02012-04-21 18:52:52 -0400110# XXX deprecate after better API exposed in importlib
Brett Cannon16475ad2012-04-16 22:11:25 -0400111def load_source(name, pathname, file=None):
112 return _LoadSourceCompatibility(name, pathname, file).load_module(name)
113
114
Brett Cannon64befe92012-04-17 19:14:26 -0400115class _LoadCompiledCompatibility(_HackedGetData,
Marc-Andre Lemburg4fe29c92012-04-25 02:31:37 +0200116 _bootstrap.SourcelessFileLoader):
Brett Cannon64befe92012-04-17 19:14:26 -0400117
118 """Compatibility support for implementing load_compiled()."""
119
120
Brett Cannona64faf02012-04-21 18:52:52 -0400121# XXX deprecate
Brett Cannon64befe92012-04-17 19:14:26 -0400122def load_compiled(name, pathname, file=None):
123 return _LoadCompiledCompatibility(name, pathname, file).load_module(name)
124
125
Brett Cannona64faf02012-04-21 18:52:52 -0400126# XXX deprecate
Brett Cannon2ee61422012-04-15 22:28:28 -0400127def load_package(name, path):
128 if os.path.isdir(path):
Brett Cannon2657df42012-05-04 15:20:40 -0400129 extensions = _bootstrap._SOURCE_SUFFIXES
130 extensions += [_bootstrap._BYTECODE_SUFFIX]
Brett Cannon2ee61422012-04-15 22:28:28 -0400131 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 Cannon938d44d2012-04-22 19:58:33 -0400137 return _bootstrap.SourceFileLoader(name, path).load_module(name)
Brett Cannon2ee61422012-04-15 22:28:28 -0400138
Brett Cannon01a76172012-04-15 20:25:23 -0400139
Brett Cannona64faf02012-04-21 18:52:52 -0400140# XXX deprecate
Brett Cannon01a76172012-04-15 20:25:23 -0400141def 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 Cannonde10bf42012-04-16 20:44:21 -0400148 if mode and (not mode.startswith(('r', 'U')) or '+' in mode):
Brett Cannon01a76172012-04-15 20:25:23 -0400149 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 Cannone69f0df2012-04-21 21:09:46 -0400166
167
168def 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 Cannon17098a52012-05-04 13:52:49 -0400194 for suffix in ['.py', _bootstrap._BYTECODE_SUFFIX]:
Brett Cannone69f0df2012-04-21 21:09:46 -0400195 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 Cannon62228db2012-04-29 14:38:11 -0400216
217
218_RELOADING = {}
219
220def 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