blob: e06fbe64c28ccab600ec955748cd4c81edf2dbba [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,
Brett Cannon2fef4d22012-04-15 19:06:23 -040011 init_builtin, 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:
14 from _imp import load_dynamic
Brett Cannon0a140662013-06-13 20:57:26 -040015except ModuleNotFoundError:
Brett Cannon3e2fe052013-03-17 15:48:16 -070016 # Platform doesn't support dynamic loading.
17 load_dynamic = None
Brett Cannon6f44d662012-04-15 16:08:47 -040018
Brett Cannon77b2abd2012-07-09 16:09:00 -040019# Directly exposed by this module
Brett Cannon17098a52012-05-04 13:52:49 -040020from importlib._bootstrap import new_module
Brett Cannona6473f92012-07-13 13:57:03 -040021from importlib._bootstrap import cache_from_source, source_from_cache
Brett Cannon01a76172012-04-15 20:25:23 -040022
Brett Cannon77b2abd2012-07-09 16:09:00 -040023
Brett Cannon2ee61422012-04-15 22:28:28 -040024from importlib import _bootstrap
Brett Cannoncb66eb02012-05-11 12:58:42 -040025from importlib import machinery
Brett Cannon05a647d2013-06-14 19:02:34 -040026from importlib import util
Brett Cannon3fe35e62013-06-14 15:04:26 -040027import importlib
Brett Cannon2ee61422012-04-15 22:28:28 -040028import os
Brett Cannone69f0df2012-04-21 21:09:46 -040029import sys
30import tokenize
Brett Cannoncb66eb02012-05-11 12:58:42 -040031import warnings
Brett Cannone69f0df2012-04-21 21:09:46 -040032
33
Brett Cannonc0499522012-05-11 14:48:41 -040034# DEPRECATED
Brett Cannone69f0df2012-04-21 21:09:46 -040035SEARCH_ERROR = 0
36PY_SOURCE = 1
37PY_COMPILED = 2
38C_EXTENSION = 3
39PY_RESOURCE = 4
40PKG_DIRECTORY = 5
41C_BUILTIN = 6
42PY_FROZEN = 7
43PY_CODERESOURCE = 8
44IMP_HOOK = 9
Brett Cannon2ee61422012-04-15 22:28:28 -040045
46
Brett Cannon77b2abd2012-07-09 16:09:00 -040047def get_magic():
Brett Cannon05a647d2013-06-14 19:02:34 -040048 """**DEPRECATED**
49
50 Return the magic number for .pyc or .pyo files.
51 """
52 return util.MAGIC_NUMBER
Brett Cannon77b2abd2012-07-09 16:09:00 -040053
54
Brett Cannon98979b82012-07-02 15:13:11 -040055def get_tag():
56 """Return the magic tag for .pyc or .pyo files."""
57 return sys.implementation.cache_tag
58
59
Brett Cannon2657df42012-05-04 15:20:40 -040060def get_suffixes():
Brett Cannoncb66eb02012-05-11 12:58:42 -040061 warnings.warn('imp.get_suffixes() is deprecated; use the constants '
62 'defined on importlib.machinery instead',
63 DeprecationWarning, 2)
Brett Cannonac9f2f32012-08-10 13:47:54 -040064 extensions = [(s, 'rb', C_EXTENSION) for s in machinery.EXTENSION_SUFFIXES]
Brett Cannoncb66eb02012-05-11 12:58:42 -040065 source = [(s, 'U', PY_SOURCE) for s in machinery.SOURCE_SUFFIXES]
66 bytecode = [(s, 'rb', PY_COMPILED) for s in machinery.BYTECODE_SUFFIXES]
Brett Cannon2657df42012-05-04 15:20:40 -040067
68 return extensions + source + bytecode
69
70
Brett Cannonacf85cd2012-04-29 12:50:03 -040071class 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 Cannon64befe92012-04-17 19:14:26 -040086class _HackedGetData:
Brett Cannon16475ad2012-04-16 22:11:25 -040087
Brett Cannon64befe92012-04-17 19:14:26 -040088 """Compatibiilty support for 'file' arguments of various load_*()
89 functions."""
Brett Cannon16475ad2012-04-16 22:11:25 -040090
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 Cannon64befe92012-04-17 19:14:26 -040096 """Gross hack to contort loader to deal w/ load_*()'s bad API."""
Brett Cannon938d44d2012-04-22 19:58:33 -040097 if self.file and path == self.path:
Brett Cannon16475ad2012-04-16 22:11:25 -040098 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 Cannon938d44d2012-04-22 19:58:33 -0400109class _LoadSourceCompatibility(_HackedGetData, _bootstrap.SourceFileLoader):
Brett Cannon64befe92012-04-17 19:14:26 -0400110
111 """Compatibility support for implementing load_source()."""
112
113
Brett Cannon16475ad2012-04-16 22:11:25 -0400114def load_source(name, pathname, file=None):
Brett Cannonc0499522012-05-11 14:48:41 -0400115 msg = ('imp.load_source() is deprecated; use '
116 'importlib.machinery.SourceFileLoader(name, pathname).load_module()'
117 ' instead')
118 warnings.warn(msg, DeprecationWarning, 2)
Brett Cannon5a4c2332013-04-28 11:53:26 -0400119 _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 Cannon16475ad2012-04-16 22:11:25 -0400125
126
Brett Cannon64befe92012-04-17 19:14:26 -0400127class _LoadCompiledCompatibility(_HackedGetData,
Marc-Andre Lemburg4fe29c92012-04-25 02:31:37 +0200128 _bootstrap.SourcelessFileLoader):
Brett Cannon64befe92012-04-17 19:14:26 -0400129
130 """Compatibility support for implementing load_compiled()."""
131
132
133def load_compiled(name, pathname, file=None):
Brett Cannonc0499522012-05-11 14:48:41 -0400134 msg = ('imp.load_compiled() is deprecated; use '
135 'importlib.machinery.SourcelessFileLoader(name, pathname).'
136 'load_module() instead ')
137 warnings.warn(msg, DeprecationWarning, 2)
Brett Cannon5a4c2332013-04-28 11:53:26 -0400138 _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 Cannon64befe92012-04-17 19:14:26 -0400144
145
Brett Cannon2ee61422012-04-15 22:28:28 -0400146def load_package(name, path):
Brett Cannonc0499522012-05-11 14:48:41 -0400147 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 Cannon2ee61422012-04-15 22:28:28 -0400151 if os.path.isdir(path):
Brett Cannonc0499522012-05-11 14:48:41 -0400152 extensions = (machinery.SOURCE_SUFFIXES[:] +
153 machinery.BYTECODE_SUFFIXES[:])
Brett Cannon2ee61422012-04-15 22:28:28 -0400154 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 Cannon938d44d2012-04-22 19:58:33 -0400160 return _bootstrap.SourceFileLoader(name, path).load_module(name)
Brett Cannon2ee61422012-04-15 22:28:28 -0400161
Brett Cannon01a76172012-04-15 20:25:23 -0400162
163def load_module(name, file, filename, details):
Brett Cannon0450c9e2012-06-15 19:39:06 -0400164 """**DEPRECATED**
165
166 Load a module, given information returned by find_module().
Brett Cannon01a76172012-04-15 20:25:23 -0400167
168 The module name must include the full package name, if any.
169
170 """
171 suffix, mode, type_ = details
Brett Cannonc0499522012-05-11 14:48:41 -0400172 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 Cannon9d0f7722013-05-03 10:37:08 -0400176 elif file is None and type_ in {PY_SOURCE, PY_COMPILED}:
Brett Cannonc0499522012-05-11 14:48:41 -0400177 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 Cannon3e2fe052013-03-17 15:48:16 -0700183 elif type_ == C_EXTENSION and load_dynamic is not None:
Brett Cannon9d0f7722013-05-03 10:37:08 -0400184 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 Cannonc0499522012-05-11 14:48:41 -0400189 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 Coghlan91b9f132012-09-01 00:13:45 +1000196 msg = "Don't know how to import {} (type code {})".format(name, type_)
Brett Cannonc0499522012-05-11 14:48:41 -0400197 raise ImportError(msg, name=name)
Brett Cannone69f0df2012-04-21 21:09:46 -0400198
199
200def find_module(name, path=None):
Brett Cannon0450c9e2012-06-15 19:39:06 -0400201 """**DEPRECATED**
202
203 Search for a module.
Brett Cannone69f0df2012-04-21 21:09:46 -0400204
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 Cannoncb66eb02012-05-11 12:58:42 -0400228 for suffix in ['.py', machinery.BYTECODE_SUFFIXES[0]]:
Brett Cannone69f0df2012-04-21 21:09:46 -0400229 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 Cannoncb66eb02012-05-11 12:58:42 -0400233 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 Cannone69f0df2012-04-21 21:09:46 -0400243 else:
Brett Cannonbf7eab02012-07-09 13:24:34 -0400244 raise ImportError(_bootstrap._ERR_MSG.format(name), name=name)
Brett Cannone69f0df2012-04-21 21:09:46 -0400245
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 Cannon62228db2012-04-29 14:38:11 -0400252
253
Brett Cannon62228db2012-04-29 14:38:11 -0400254def reload(module):
Brett Cannon3fe35e62013-06-14 15:04:26 -0400255 """**DEPRECATED**
256
257 Reload the module and return it.
Brett Cannon62228db2012-04-29 14:38:11 -0400258
259 The module must have been successfully imported before.
260
261 """
Brett Cannon3fe35e62013-06-14 15:04:26 -0400262 return importlib.reload(module)