blob: 74253abcb410bc2b628bb2a48f03376b3b660a28 [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
9from _imp import (lock_held, acquire_lock, release_lock, reload,
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,
12 _fix_co_filename)
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 Cannonea59dbf2012-04-20 21:44:46 -040015# Can (probably) move to importlib
Brett Cannon2f923892012-04-21 18:55:51 -040016from _imp import get_suffixes
Brett Cannon6f44d662012-04-15 16:08:47 -040017# Should be re-implemented here (and mostly deprecated)
Brett Cannone69f0df2012-04-21 21:09:46 -040018from _imp import NullImporter
Brett Cannon6f44d662012-04-15 16:08:47 -040019
20from importlib._bootstrap import _new_module as new_module
Brett Cannonea59dbf2012-04-20 21:44:46 -040021from importlib._bootstrap import _cache_from_source as cache_from_source
Brett Cannon01a76172012-04-15 20:25:23 -040022
Brett Cannon2ee61422012-04-15 22:28:28 -040023from importlib import _bootstrap
24import os
Brett Cannone69f0df2012-04-21 21:09:46 -040025import sys
26import tokenize
27
28
29# XXX "deprecate" once find_module(), load_module(), and get_suffixes() are
30# deprecated.
31SEARCH_ERROR = 0
32PY_SOURCE = 1
33PY_COMPILED = 2
34C_EXTENSION = 3
35PY_RESOURCE = 4
36PKG_DIRECTORY = 5
37C_BUILTIN = 6
38PY_FROZEN = 7
39PY_CODERESOURCE = 8
40IMP_HOOK = 9
Brett Cannon2ee61422012-04-15 22:28:28 -040041
42
Brett Cannona64faf02012-04-21 18:52:52 -040043def 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 Cannon64befe92012-04-17 19:14:26 -040063class _HackedGetData:
Brett Cannon16475ad2012-04-16 22:11:25 -040064
Brett Cannon64befe92012-04-17 19:14:26 -040065 """Compatibiilty support for 'file' arguments of various load_*()
66 functions."""
Brett Cannon16475ad2012-04-16 22:11:25 -040067
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 Cannon64befe92012-04-17 19:14:26 -040073 """Gross hack to contort loader to deal w/ load_*()'s bad API."""
Brett Cannon578393b2012-04-16 23:11:28 -040074 if self.file and path == self._path:
Brett Cannon16475ad2012-04-16 22:11:25 -040075 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 Cannon64befe92012-04-17 19:14:26 -040086class _LoadSourceCompatibility(_HackedGetData, _bootstrap._SourceFileLoader):
87
88 """Compatibility support for implementing load_source()."""
89
90
Brett Cannona64faf02012-04-21 18:52:52 -040091# XXX deprecate after better API exposed in importlib
Brett Cannon16475ad2012-04-16 22:11:25 -040092def load_source(name, pathname, file=None):
93 return _LoadSourceCompatibility(name, pathname, file).load_module(name)
94
95
Brett Cannon64befe92012-04-17 19:14:26 -040096class _LoadCompiledCompatibility(_HackedGetData,
97 _bootstrap._SourcelessFileLoader):
98
99 """Compatibility support for implementing load_compiled()."""
100
101
Brett Cannona64faf02012-04-21 18:52:52 -0400102# XXX deprecate
Brett Cannon64befe92012-04-17 19:14:26 -0400103def load_compiled(name, pathname, file=None):
104 return _LoadCompiledCompatibility(name, pathname, file).load_module(name)
105
106
Brett Cannona64faf02012-04-21 18:52:52 -0400107# XXX deprecate
Brett Cannon2ee61422012-04-15 22:28:28 -0400108def 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 Cannon01a76172012-04-15 20:25:23 -0400120
Brett Cannona64faf02012-04-21 18:52:52 -0400121# XXX deprecate
Brett Cannon01a76172012-04-15 20:25:23 -0400122def 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 Cannonde10bf42012-04-16 20:44:21 -0400129 if mode and (not mode.startswith(('r', 'U')) or '+' in mode):
Brett Cannon01a76172012-04-15 20:25:23 -0400130 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 Cannone69f0df2012-04-21 21:09:46 -0400147
148
149def 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