blob: e02aaef344c6148b43ef3954689126319504dea9 [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 Cannon3e2fe052013-03-17 15:48:16 -070010 get_frozen_object, is_frozen_package,
Nick Coghland5cacbb2015-05-23 22:24:10 +100011 init_frozen, is_builtin, is_frozen,
Brett Cannonac9f2f32012-08-10 13:47:54 -040012 _fix_co_filename)
Brett Cannon3e2fe052013-03-17 15:48:16 -070013try:
Nick Coghland5cacbb2015-05-23 22:24:10 +100014 from _imp import create_dynamic
Brett Cannoncd171c82013-07-04 17:43:24 -040015except ImportError:
Brett Cannon3e2fe052013-03-17 15:48:16 -070016 # Platform doesn't support dynamic loading.
Nick Coghland5cacbb2015-05-23 22:24:10 +100017 create_dynamic = None
Brett Cannon6f44d662012-04-15 16:08:47 -040018
Nick Coghland5cacbb2015-05-23 22:24:10 +100019from importlib._bootstrap import _ERR_MSG, _exec, _load, _builtin_from_name
Eric Snow32439d62015-05-02 19:15:18 -060020from importlib._bootstrap_external import SourcelessFileLoader
Brett Cannon01a76172012-04-15 20:25:23 -040021
Brett Cannoncb66eb02012-05-11 12:58:42 -040022from importlib import machinery
Brett Cannon05a647d2013-06-14 19:02:34 -040023from importlib import util
Brett Cannon3fe35e62013-06-14 15:04:26 -040024import importlib
Brett Cannon2ee61422012-04-15 22:28:28 -040025import os
Brett Cannone69f0df2012-04-21 21:09:46 -040026import sys
27import tokenize
Brett Cannona3c96152013-06-14 22:26:30 -040028import types
Brett Cannoncb66eb02012-05-11 12:58:42 -040029import warnings
Brett Cannone69f0df2012-04-21 21:09:46 -040030
Brett Cannondc6d3e12021-04-03 15:31:15 -070031warnings.warn("the imp module is deprecated in favour of importlib and slated "
32 "for removal in Python 3.12; "
Brett Cannone4f41de2013-06-16 13:13:40 -040033 "see the module's documentation for alternative uses",
Brett Cannonc0d91af2015-10-16 12:21:37 -070034 DeprecationWarning, stacklevel=2)
Brett Cannone69f0df2012-04-21 21:09:46 -040035
Brett Cannonc0499522012-05-11 14:48:41 -040036# DEPRECATED
Brett Cannone69f0df2012-04-21 21:09:46 -040037SEARCH_ERROR = 0
38PY_SOURCE = 1
39PY_COMPILED = 2
40C_EXTENSION = 3
41PY_RESOURCE = 4
42PKG_DIRECTORY = 5
43C_BUILTIN = 6
44PY_FROZEN = 7
45PY_CODERESOURCE = 8
46IMP_HOOK = 9
Brett Cannon2ee61422012-04-15 22:28:28 -040047
48
Brett Cannona3c96152013-06-14 22:26:30 -040049def new_module(name):
50 """**DEPRECATED**
51
52 Create a new module.
53
54 The module is not entered into sys.modules.
55
56 """
57 return types.ModuleType(name)
58
59
Brett Cannon77b2abd2012-07-09 16:09:00 -040060def get_magic():
Brett Cannon05a647d2013-06-14 19:02:34 -040061 """**DEPRECATED**
62
Brett Cannonf299abd2015-04-13 14:21:02 -040063 Return the magic number for .pyc files.
Brett Cannon05a647d2013-06-14 19:02:34 -040064 """
65 return util.MAGIC_NUMBER
Brett Cannon77b2abd2012-07-09 16:09:00 -040066
67
Brett Cannon98979b82012-07-02 15:13:11 -040068def get_tag():
Brett Cannonf299abd2015-04-13 14:21:02 -040069 """Return the magic tag for .pyc files."""
Brett Cannon98979b82012-07-02 15:13:11 -040070 return sys.implementation.cache_tag
71
72
Brett Cannona38e8142013-06-14 22:35:40 -040073def cache_from_source(path, debug_override=None):
74 """**DEPRECATED**
75
Brett Cannonf299abd2015-04-13 14:21:02 -040076 Given the path to a .py file, return the path to its .pyc file.
Brett Cannona38e8142013-06-14 22:35:40 -040077
78 The .py file does not need to exist; this simply returns the path to the
Brett Cannonf299abd2015-04-13 14:21:02 -040079 .pyc file calculated as if the .py file were imported.
Brett Cannona38e8142013-06-14 22:35:40 -040080
81 If debug_override is not None, then it must be a boolean and is used in
82 place of sys.flags.optimize.
83
84 If sys.implementation.cache_tag is None then NotImplementedError is raised.
85
86 """
Brett Cannonf299abd2015-04-13 14:21:02 -040087 with warnings.catch_warnings():
88 warnings.simplefilter('ignore')
89 return util.cache_from_source(path, debug_override)
Brett Cannona38e8142013-06-14 22:35:40 -040090
91
92def source_from_cache(path):
93 """**DEPRECATED**
94
Brett Cannonf299abd2015-04-13 14:21:02 -040095 Given the path to a .pyc. file, return the path to its .py file.
Brett Cannona38e8142013-06-14 22:35:40 -040096
Brett Cannonf299abd2015-04-13 14:21:02 -040097 The .pyc file does not need to exist; this simply returns the path to
98 the .py file calculated to correspond to the .pyc file. If path does
Brett Cannona38e8142013-06-14 22:35:40 -040099 not conform to PEP 3147 format, ValueError will be raised. If
100 sys.implementation.cache_tag is None then NotImplementedError is raised.
101
102 """
103 return util.source_from_cache(path)
104
105
Brett Cannon2657df42012-05-04 15:20:40 -0400106def get_suffixes():
Brett Cannone4f41de2013-06-16 13:13:40 -0400107 """**DEPRECATED**"""
Brett Cannonac9f2f32012-08-10 13:47:54 -0400108 extensions = [(s, 'rb', C_EXTENSION) for s in machinery.EXTENSION_SUFFIXES]
Serhiy Storchaka6787a382013-11-23 22:12:06 +0200109 source = [(s, 'r', PY_SOURCE) for s in machinery.SOURCE_SUFFIXES]
Brett Cannoncb66eb02012-05-11 12:58:42 -0400110 bytecode = [(s, 'rb', PY_COMPILED) for s in machinery.BYTECODE_SUFFIXES]
Brett Cannon2657df42012-05-04 15:20:40 -0400111
112 return extensions + source + bytecode
113
114
Brett Cannonacf85cd2012-04-29 12:50:03 -0400115class NullImporter:
116
Brett Cannone4f41de2013-06-16 13:13:40 -0400117 """**DEPRECATED**
118
119 Null import object.
120
121 """
Brett Cannonacf85cd2012-04-29 12:50:03 -0400122
123 def __init__(self, path):
124 if path == '':
125 raise ImportError('empty pathname', path='')
126 elif os.path.isdir(path):
127 raise ImportError('existing directory', path=path)
128
129 def find_module(self, fullname):
130 """Always returns None."""
131 return None
132
133
Brett Cannon64befe92012-04-17 19:14:26 -0400134class _HackedGetData:
Brett Cannon16475ad2012-04-16 22:11:25 -0400135
Zachary Ware50db6ac2015-04-14 15:43:00 -0500136 """Compatibility support for 'file' arguments of various load_*()
Brett Cannon64befe92012-04-17 19:14:26 -0400137 functions."""
Brett Cannon16475ad2012-04-16 22:11:25 -0400138
139 def __init__(self, fullname, path, file=None):
140 super().__init__(fullname, path)
141 self.file = file
142
143 def get_data(self, path):
Brett Cannon64befe92012-04-17 19:14:26 -0400144 """Gross hack to contort loader to deal w/ load_*()'s bad API."""
Brett Cannon938d44d2012-04-22 19:58:33 -0400145 if self.file and path == self.path:
Benjamin Petersonb0274f22018-07-06 20:41:06 -0700146 # The contract of get_data() requires us to return bytes. Reopen the
147 # file in binary mode if needed.
Brett Cannona4975a92013-08-23 11:45:57 -0400148 if not self.file.closed:
149 file = self.file
Benjamin Petersonb0274f22018-07-06 20:41:06 -0700150 if 'b' not in file.mode:
151 file.close()
152 if self.file.closed:
153 self.file = file = open(self.path, 'rb')
Brett Cannona4975a92013-08-23 11:45:57 -0400154
155 with file:
Brett Cannona4975a92013-08-23 11:45:57 -0400156 return file.read()
Brett Cannon16475ad2012-04-16 22:11:25 -0400157 else:
158 return super().get_data(path)
159
160
Brett Cannon589c4ff2013-06-14 22:29:58 -0400161class _LoadSourceCompatibility(_HackedGetData, machinery.SourceFileLoader):
Brett Cannon64befe92012-04-17 19:14:26 -0400162
163 """Compatibility support for implementing load_source()."""
164
165
Brett Cannon16475ad2012-04-16 22:11:25 -0400166def load_source(name, pathname, file=None):
Eric Snowb523f842013-11-22 09:05:39 -0700167 loader = _LoadSourceCompatibility(name, pathname, file)
168 spec = util.spec_from_file_location(name, pathname, loader=loader)
Eric Snowb523f842013-11-22 09:05:39 -0700169 if name in sys.modules:
Brett Cannon2a17bde2014-05-30 14:55:29 -0400170 module = _exec(spec, sys.modules[name])
Eric Snowb523f842013-11-22 09:05:39 -0700171 else:
Brett Cannon2a17bde2014-05-30 14:55:29 -0400172 module = _load(spec)
Brett Cannon5a4c2332013-04-28 11:53:26 -0400173 # To allow reloading to potentially work, use a non-hacked loader which
174 # won't rely on a now-closed file object.
Brett Cannon589c4ff2013-06-14 22:29:58 -0400175 module.__loader__ = machinery.SourceFileLoader(name, pathname)
Eric Snowb523f842013-11-22 09:05:39 -0700176 module.__spec__.loader = module.__loader__
Brett Cannon5a4c2332013-04-28 11:53:26 -0400177 return module
Brett Cannon16475ad2012-04-16 22:11:25 -0400178
179
Brett Cannon589c4ff2013-06-14 22:29:58 -0400180class _LoadCompiledCompatibility(_HackedGetData, SourcelessFileLoader):
Brett Cannon64befe92012-04-17 19:14:26 -0400181
182 """Compatibility support for implementing load_compiled()."""
183
184
185def load_compiled(name, pathname, file=None):
Brett Cannone4f41de2013-06-16 13:13:40 -0400186 """**DEPRECATED**"""
Eric Snowb523f842013-11-22 09:05:39 -0700187 loader = _LoadCompiledCompatibility(name, pathname, file)
188 spec = util.spec_from_file_location(name, pathname, loader=loader)
Eric Snowb523f842013-11-22 09:05:39 -0700189 if name in sys.modules:
Brett Cannon2a17bde2014-05-30 14:55:29 -0400190 module = _exec(spec, sys.modules[name])
Eric Snowb523f842013-11-22 09:05:39 -0700191 else:
Brett Cannon2a17bde2014-05-30 14:55:29 -0400192 module = _load(spec)
Brett Cannon5a4c2332013-04-28 11:53:26 -0400193 # To allow reloading to potentially work, use a non-hacked loader which
194 # won't rely on a now-closed file object.
Brett Cannon589c4ff2013-06-14 22:29:58 -0400195 module.__loader__ = SourcelessFileLoader(name, pathname)
Eric Snowb523f842013-11-22 09:05:39 -0700196 module.__spec__.loader = module.__loader__
Brett Cannon5a4c2332013-04-28 11:53:26 -0400197 return module
Brett Cannon64befe92012-04-17 19:14:26 -0400198
199
Brett Cannon2ee61422012-04-15 22:28:28 -0400200def load_package(name, path):
Brett Cannone4f41de2013-06-16 13:13:40 -0400201 """**DEPRECATED**"""
Brett Cannon2ee61422012-04-15 22:28:28 -0400202 if os.path.isdir(path):
Brett Cannonc0499522012-05-11 14:48:41 -0400203 extensions = (machinery.SOURCE_SUFFIXES[:] +
204 machinery.BYTECODE_SUFFIXES[:])
Brett Cannon2ee61422012-04-15 22:28:28 -0400205 for extension in extensions:
Alexandru Ardeleanc38e32a2017-06-23 20:35:03 +0300206 init_path = os.path.join(path, '__init__' + extension)
207 if os.path.exists(init_path):
208 path = init_path
Brett Cannon2ee61422012-04-15 22:28:28 -0400209 break
210 else:
211 raise ValueError('{!r} is not a package'.format(path))
Eric Snowb523f842013-11-22 09:05:39 -0700212 spec = util.spec_from_file_location(name, path,
213 submodule_search_locations=[])
Eric Snowb523f842013-11-22 09:05:39 -0700214 if name in sys.modules:
Brett Cannon2a17bde2014-05-30 14:55:29 -0400215 return _exec(spec, sys.modules[name])
Eric Snowb523f842013-11-22 09:05:39 -0700216 else:
Brett Cannon2a17bde2014-05-30 14:55:29 -0400217 return _load(spec)
Brett Cannon2ee61422012-04-15 22:28:28 -0400218
Brett Cannon01a76172012-04-15 20:25:23 -0400219
220def load_module(name, file, filename, details):
Brett Cannon0450c9e2012-06-15 19:39:06 -0400221 """**DEPRECATED**
222
223 Load a module, given information returned by find_module().
Brett Cannon01a76172012-04-15 20:25:23 -0400224
225 The module name must include the full package name, if any.
226
227 """
228 suffix, mode, type_ = details
Victor Stinner942f7a22020-03-04 18:50:22 +0100229 if mode and (not mode.startswith(('r', 'U')) or '+' in mode):
Brett Cannone4f41de2013-06-16 13:13:40 -0400230 raise ValueError('invalid file open mode {!r}'.format(mode))
231 elif file is None and type_ in {PY_SOURCE, PY_COMPILED}:
232 msg = 'file object required for import (type code {})'.format(type_)
233 raise ValueError(msg)
234 elif type_ == PY_SOURCE:
235 return load_source(name, filename, file)
236 elif type_ == PY_COMPILED:
237 return load_compiled(name, filename, file)
238 elif type_ == C_EXTENSION and load_dynamic is not None:
239 if file is None:
240 with open(filename, 'rb') as opened_file:
241 return load_dynamic(name, filename, opened_file)
Brett Cannonc0499522012-05-11 14:48:41 -0400242 else:
Brett Cannone4f41de2013-06-16 13:13:40 -0400243 return load_dynamic(name, filename, file)
244 elif type_ == PKG_DIRECTORY:
245 return load_package(name, filename)
246 elif type_ == C_BUILTIN:
247 return init_builtin(name)
248 elif type_ == PY_FROZEN:
249 return init_frozen(name)
250 else:
251 msg = "Don't know how to import {} (type code {})".format(name, type_)
252 raise ImportError(msg, name=name)
Brett Cannone69f0df2012-04-21 21:09:46 -0400253
254
255def find_module(name, path=None):
Brett Cannon0450c9e2012-06-15 19:39:06 -0400256 """**DEPRECATED**
257
258 Search for a module.
Brett Cannone69f0df2012-04-21 21:09:46 -0400259
260 If path is omitted or None, search for a built-in, frozen or special
261 module and continue search in sys.path. The module name cannot
262 contain '.'; to search for a submodule of a package, pass the
263 submodule name and the package's __path__.
264
265 """
266 if not isinstance(name, str):
267 raise TypeError("'name' must be a str, not {}".format(type(name)))
268 elif not isinstance(path, (type(None), list)):
269 # Backwards-compatibility
Brett Cannonf76457e2016-07-15 10:58:54 -0700270 raise RuntimeError("'path' must be None or a list, "
271 "not {}".format(type(path)))
Brett Cannone69f0df2012-04-21 21:09:46 -0400272
273 if path is None:
274 if is_builtin(name):
275 return None, None, ('', '', C_BUILTIN)
276 elif is_frozen(name):
277 return None, None, ('', '', PY_FROZEN)
278 else:
279 path = sys.path
280
281 for entry in path:
282 package_directory = os.path.join(entry, name)
Brett Cannoncb66eb02012-05-11 12:58:42 -0400283 for suffix in ['.py', machinery.BYTECODE_SUFFIXES[0]]:
Brett Cannone69f0df2012-04-21 21:09:46 -0400284 package_file_name = '__init__' + suffix
285 file_path = os.path.join(package_directory, package_file_name)
286 if os.path.isfile(file_path):
287 return None, package_directory, ('', '', PKG_DIRECTORY)
Brett Cannone4f41de2013-06-16 13:13:40 -0400288 for suffix, mode, type_ in get_suffixes():
289 file_name = name + suffix
290 file_path = os.path.join(entry, file_name)
291 if os.path.isfile(file_path):
292 break
293 else:
294 continue
295 break # Break out of outer loop when breaking out of inner loop.
Brett Cannone69f0df2012-04-21 21:09:46 -0400296 else:
Brett Cannon589c4ff2013-06-14 22:29:58 -0400297 raise ImportError(_ERR_MSG.format(name), name=name)
Brett Cannone69f0df2012-04-21 21:09:46 -0400298
299 encoding = None
Serhiy Storchaka6787a382013-11-23 22:12:06 +0200300 if 'b' not in mode:
Brett Cannone69f0df2012-04-21 21:09:46 -0400301 with open(file_path, 'rb') as file:
302 encoding = tokenize.detect_encoding(file.readline)[0]
303 file = open(file_path, mode, encoding=encoding)
304 return file, file_path, (suffix, mode, type_)
Brett Cannon62228db2012-04-29 14:38:11 -0400305
306
Brett Cannon62228db2012-04-29 14:38:11 -0400307def reload(module):
Brett Cannon3fe35e62013-06-14 15:04:26 -0400308 """**DEPRECATED**
309
310 Reload the module and return it.
Brett Cannon62228db2012-04-29 14:38:11 -0400311
312 The module must have been successfully imported before.
313
314 """
Brett Cannon3fe35e62013-06-14 15:04:26 -0400315 return importlib.reload(module)
Nick Coghland5cacbb2015-05-23 22:24:10 +1000316
317
318def init_builtin(name):
319 """**DEPRECATED**
320
321 Load and return a built-in module by name, or None is such module doesn't
322 exist
323 """
324 try:
325 return _builtin_from_name(name)
326 except ImportError:
327 return None
328
329
330if create_dynamic:
331 def load_dynamic(name, path, file=None):
332 """**DEPRECATED**
333
334 Load an extension module.
335 """
336 import importlib.machinery
337 loader = importlib.machinery.ExtensionFileLoader(name, path)
Nick Coghlan9d3c61c2015-09-05 21:05:05 +1000338
339 # Issue #24748: Skip the sys.modules check in _load_module_shim;
340 # always load new extension
341 spec = importlib.machinery.ModuleSpec(
342 name=name, loader=loader, origin=path)
343 return _load(spec)
344
Nick Coghland5cacbb2015-05-23 22:24:10 +1000345else:
346 load_dynamic = None