1 | n/a | import builtins |
---|
2 | n/a | import contextlib |
---|
3 | n/a | import errno |
---|
4 | n/a | import functools |
---|
5 | n/a | import importlib |
---|
6 | n/a | from importlib import machinery, util, invalidate_caches |
---|
7 | n/a | import os |
---|
8 | n/a | import os.path |
---|
9 | n/a | from test import support |
---|
10 | n/a | import unittest |
---|
11 | n/a | import sys |
---|
12 | n/a | import tempfile |
---|
13 | n/a | import types |
---|
14 | n/a | |
---|
15 | n/a | |
---|
16 | n/a | BUILTINS = types.SimpleNamespace() |
---|
17 | n/a | BUILTINS.good_name = None |
---|
18 | n/a | BUILTINS.bad_name = None |
---|
19 | n/a | if 'errno' in sys.builtin_module_names: |
---|
20 | n/a | BUILTINS.good_name = 'errno' |
---|
21 | n/a | if 'importlib' not in sys.builtin_module_names: |
---|
22 | n/a | BUILTINS.bad_name = 'importlib' |
---|
23 | n/a | |
---|
24 | n/a | EXTENSIONS = types.SimpleNamespace() |
---|
25 | n/a | EXTENSIONS.path = None |
---|
26 | n/a | EXTENSIONS.ext = None |
---|
27 | n/a | EXTENSIONS.filename = None |
---|
28 | n/a | EXTENSIONS.file_path = None |
---|
29 | n/a | EXTENSIONS.name = '_testcapi' |
---|
30 | n/a | |
---|
31 | n/a | def _extension_details(): |
---|
32 | n/a | global EXTENSIONS |
---|
33 | n/a | for path in sys.path: |
---|
34 | n/a | for ext in machinery.EXTENSION_SUFFIXES: |
---|
35 | n/a | filename = EXTENSIONS.name + ext |
---|
36 | n/a | file_path = os.path.join(path, filename) |
---|
37 | n/a | if os.path.exists(file_path): |
---|
38 | n/a | EXTENSIONS.path = path |
---|
39 | n/a | EXTENSIONS.ext = ext |
---|
40 | n/a | EXTENSIONS.filename = filename |
---|
41 | n/a | EXTENSIONS.file_path = file_path |
---|
42 | n/a | return |
---|
43 | n/a | |
---|
44 | n/a | _extension_details() |
---|
45 | n/a | |
---|
46 | n/a | |
---|
47 | n/a | def import_importlib(module_name): |
---|
48 | n/a | """Import a module from importlib both w/ and w/o _frozen_importlib.""" |
---|
49 | n/a | fresh = ('importlib',) if '.' in module_name else () |
---|
50 | n/a | frozen = support.import_fresh_module(module_name) |
---|
51 | n/a | source = support.import_fresh_module(module_name, fresh=fresh, |
---|
52 | n/a | blocked=('_frozen_importlib', '_frozen_importlib_external')) |
---|
53 | n/a | return {'Frozen': frozen, 'Source': source} |
---|
54 | n/a | |
---|
55 | n/a | |
---|
56 | n/a | def specialize_class(cls, kind, base=None, **kwargs): |
---|
57 | n/a | # XXX Support passing in submodule names--load (and cache) them? |
---|
58 | n/a | # That would clean up the test modules a bit more. |
---|
59 | n/a | if base is None: |
---|
60 | n/a | base = unittest.TestCase |
---|
61 | n/a | elif not isinstance(base, type): |
---|
62 | n/a | base = base[kind] |
---|
63 | n/a | name = '{}_{}'.format(kind, cls.__name__) |
---|
64 | n/a | bases = (cls, base) |
---|
65 | n/a | specialized = types.new_class(name, bases) |
---|
66 | n/a | specialized.__module__ = cls.__module__ |
---|
67 | n/a | specialized._NAME = cls.__name__ |
---|
68 | n/a | specialized._KIND = kind |
---|
69 | n/a | for attr, values in kwargs.items(): |
---|
70 | n/a | value = values[kind] |
---|
71 | n/a | setattr(specialized, attr, value) |
---|
72 | n/a | return specialized |
---|
73 | n/a | |
---|
74 | n/a | |
---|
75 | n/a | def split_frozen(cls, base=None, **kwargs): |
---|
76 | n/a | frozen = specialize_class(cls, 'Frozen', base, **kwargs) |
---|
77 | n/a | source = specialize_class(cls, 'Source', base, **kwargs) |
---|
78 | n/a | return frozen, source |
---|
79 | n/a | |
---|
80 | n/a | |
---|
81 | n/a | def test_both(test_class, base=None, **kwargs): |
---|
82 | n/a | return split_frozen(test_class, base, **kwargs) |
---|
83 | n/a | |
---|
84 | n/a | |
---|
85 | n/a | CASE_INSENSITIVE_FS = True |
---|
86 | n/a | # Windows is the only OS that is *always* case-insensitive |
---|
87 | n/a | # (OS X *can* be case-sensitive). |
---|
88 | n/a | if sys.platform not in ('win32', 'cygwin'): |
---|
89 | n/a | changed_name = __file__.upper() |
---|
90 | n/a | if changed_name == __file__: |
---|
91 | n/a | changed_name = __file__.lower() |
---|
92 | n/a | if not os.path.exists(changed_name): |
---|
93 | n/a | CASE_INSENSITIVE_FS = False |
---|
94 | n/a | |
---|
95 | n/a | source_importlib = import_importlib('importlib')['Source'] |
---|
96 | n/a | __import__ = {'Frozen': staticmethod(builtins.__import__), |
---|
97 | n/a | 'Source': staticmethod(source_importlib.__import__)} |
---|
98 | n/a | |
---|
99 | n/a | |
---|
100 | n/a | def case_insensitive_tests(test): |
---|
101 | n/a | """Class decorator that nullifies tests requiring a case-insensitive |
---|
102 | n/a | file system.""" |
---|
103 | n/a | return unittest.skipIf(not CASE_INSENSITIVE_FS, |
---|
104 | n/a | "requires a case-insensitive filesystem")(test) |
---|
105 | n/a | |
---|
106 | n/a | |
---|
107 | n/a | def submodule(parent, name, pkg_dir, content=''): |
---|
108 | n/a | path = os.path.join(pkg_dir, name + '.py') |
---|
109 | n/a | with open(path, 'w') as subfile: |
---|
110 | n/a | subfile.write(content) |
---|
111 | n/a | return '{}.{}'.format(parent, name), path |
---|
112 | n/a | |
---|
113 | n/a | |
---|
114 | n/a | @contextlib.contextmanager |
---|
115 | n/a | def uncache(*names): |
---|
116 | n/a | """Uncache a module from sys.modules. |
---|
117 | n/a | |
---|
118 | n/a | A basic sanity check is performed to prevent uncaching modules that either |
---|
119 | n/a | cannot/shouldn't be uncached. |
---|
120 | n/a | |
---|
121 | n/a | """ |
---|
122 | n/a | for name in names: |
---|
123 | n/a | if name in ('sys', 'marshal', 'imp'): |
---|
124 | n/a | raise ValueError( |
---|
125 | n/a | "cannot uncache {0}".format(name)) |
---|
126 | n/a | try: |
---|
127 | n/a | del sys.modules[name] |
---|
128 | n/a | except KeyError: |
---|
129 | n/a | pass |
---|
130 | n/a | try: |
---|
131 | n/a | yield |
---|
132 | n/a | finally: |
---|
133 | n/a | for name in names: |
---|
134 | n/a | try: |
---|
135 | n/a | del sys.modules[name] |
---|
136 | n/a | except KeyError: |
---|
137 | n/a | pass |
---|
138 | n/a | |
---|
139 | n/a | |
---|
140 | n/a | @contextlib.contextmanager |
---|
141 | n/a | def temp_module(name, content='', *, pkg=False): |
---|
142 | n/a | conflicts = [n for n in sys.modules if n.partition('.')[0] == name] |
---|
143 | n/a | with support.temp_cwd(None) as cwd: |
---|
144 | n/a | with uncache(name, *conflicts): |
---|
145 | n/a | with support.DirsOnSysPath(cwd): |
---|
146 | n/a | invalidate_caches() |
---|
147 | n/a | |
---|
148 | n/a | location = os.path.join(cwd, name) |
---|
149 | n/a | if pkg: |
---|
150 | n/a | modpath = os.path.join(location, '__init__.py') |
---|
151 | n/a | os.mkdir(name) |
---|
152 | n/a | else: |
---|
153 | n/a | modpath = location + '.py' |
---|
154 | n/a | if content is None: |
---|
155 | n/a | # Make sure the module file gets created. |
---|
156 | n/a | content = '' |
---|
157 | n/a | if content is not None: |
---|
158 | n/a | # not a namespace package |
---|
159 | n/a | with open(modpath, 'w') as modfile: |
---|
160 | n/a | modfile.write(content) |
---|
161 | n/a | yield location |
---|
162 | n/a | |
---|
163 | n/a | |
---|
164 | n/a | @contextlib.contextmanager |
---|
165 | n/a | def import_state(**kwargs): |
---|
166 | n/a | """Context manager to manage the various importers and stored state in the |
---|
167 | n/a | sys module. |
---|
168 | n/a | |
---|
169 | n/a | The 'modules' attribute is not supported as the interpreter state stores a |
---|
170 | n/a | pointer to the dict that the interpreter uses internally; |
---|
171 | n/a | reassigning to sys.modules does not have the desired effect. |
---|
172 | n/a | |
---|
173 | n/a | """ |
---|
174 | n/a | originals = {} |
---|
175 | n/a | try: |
---|
176 | n/a | for attr, default in (('meta_path', []), ('path', []), |
---|
177 | n/a | ('path_hooks', []), |
---|
178 | n/a | ('path_importer_cache', {})): |
---|
179 | n/a | originals[attr] = getattr(sys, attr) |
---|
180 | n/a | if attr in kwargs: |
---|
181 | n/a | new_value = kwargs[attr] |
---|
182 | n/a | del kwargs[attr] |
---|
183 | n/a | else: |
---|
184 | n/a | new_value = default |
---|
185 | n/a | setattr(sys, attr, new_value) |
---|
186 | n/a | if len(kwargs): |
---|
187 | n/a | raise ValueError( |
---|
188 | n/a | 'unrecognized arguments: {0}'.format(kwargs.keys())) |
---|
189 | n/a | yield |
---|
190 | n/a | finally: |
---|
191 | n/a | for attr, value in originals.items(): |
---|
192 | n/a | setattr(sys, attr, value) |
---|
193 | n/a | |
---|
194 | n/a | |
---|
195 | n/a | class _ImporterMock: |
---|
196 | n/a | |
---|
197 | n/a | """Base class to help with creating importer mocks.""" |
---|
198 | n/a | |
---|
199 | n/a | def __init__(self, *names, module_code={}): |
---|
200 | n/a | self.modules = {} |
---|
201 | n/a | self.module_code = {} |
---|
202 | n/a | for name in names: |
---|
203 | n/a | if not name.endswith('.__init__'): |
---|
204 | n/a | import_name = name |
---|
205 | n/a | else: |
---|
206 | n/a | import_name = name[:-len('.__init__')] |
---|
207 | n/a | if '.' not in name: |
---|
208 | n/a | package = None |
---|
209 | n/a | elif import_name == name: |
---|
210 | n/a | package = name.rsplit('.', 1)[0] |
---|
211 | n/a | else: |
---|
212 | n/a | package = import_name |
---|
213 | n/a | module = types.ModuleType(import_name) |
---|
214 | n/a | module.__loader__ = self |
---|
215 | n/a | module.__file__ = '<mock __file__>' |
---|
216 | n/a | module.__package__ = package |
---|
217 | n/a | module.attr = name |
---|
218 | n/a | if import_name != name: |
---|
219 | n/a | module.__path__ = ['<mock __path__>'] |
---|
220 | n/a | self.modules[import_name] = module |
---|
221 | n/a | if import_name in module_code: |
---|
222 | n/a | self.module_code[import_name] = module_code[import_name] |
---|
223 | n/a | |
---|
224 | n/a | def __getitem__(self, name): |
---|
225 | n/a | return self.modules[name] |
---|
226 | n/a | |
---|
227 | n/a | def __enter__(self): |
---|
228 | n/a | self._uncache = uncache(*self.modules.keys()) |
---|
229 | n/a | self._uncache.__enter__() |
---|
230 | n/a | return self |
---|
231 | n/a | |
---|
232 | n/a | def __exit__(self, *exc_info): |
---|
233 | n/a | self._uncache.__exit__(None, None, None) |
---|
234 | n/a | |
---|
235 | n/a | |
---|
236 | n/a | class mock_modules(_ImporterMock): |
---|
237 | n/a | |
---|
238 | n/a | """Importer mock using PEP 302 APIs.""" |
---|
239 | n/a | |
---|
240 | n/a | def find_module(self, fullname, path=None): |
---|
241 | n/a | if fullname not in self.modules: |
---|
242 | n/a | return None |
---|
243 | n/a | else: |
---|
244 | n/a | return self |
---|
245 | n/a | |
---|
246 | n/a | def load_module(self, fullname): |
---|
247 | n/a | if fullname not in self.modules: |
---|
248 | n/a | raise ImportError |
---|
249 | n/a | else: |
---|
250 | n/a | sys.modules[fullname] = self.modules[fullname] |
---|
251 | n/a | if fullname in self.module_code: |
---|
252 | n/a | try: |
---|
253 | n/a | self.module_code[fullname]() |
---|
254 | n/a | except Exception: |
---|
255 | n/a | del sys.modules[fullname] |
---|
256 | n/a | raise |
---|
257 | n/a | return self.modules[fullname] |
---|
258 | n/a | |
---|
259 | n/a | |
---|
260 | n/a | class mock_spec(_ImporterMock): |
---|
261 | n/a | |
---|
262 | n/a | """Importer mock using PEP 451 APIs.""" |
---|
263 | n/a | |
---|
264 | n/a | def find_spec(self, fullname, path=None, parent=None): |
---|
265 | n/a | try: |
---|
266 | n/a | module = self.modules[fullname] |
---|
267 | n/a | except KeyError: |
---|
268 | n/a | return None |
---|
269 | n/a | spec = util.spec_from_file_location( |
---|
270 | n/a | fullname, module.__file__, loader=self, |
---|
271 | n/a | submodule_search_locations=getattr(module, '__path__', None)) |
---|
272 | n/a | return spec |
---|
273 | n/a | |
---|
274 | n/a | def create_module(self, spec): |
---|
275 | n/a | if spec.name not in self.modules: |
---|
276 | n/a | raise ImportError |
---|
277 | n/a | return self.modules[spec.name] |
---|
278 | n/a | |
---|
279 | n/a | def exec_module(self, module): |
---|
280 | n/a | try: |
---|
281 | n/a | self.module_code[module.__spec__.name]() |
---|
282 | n/a | except KeyError: |
---|
283 | n/a | pass |
---|
284 | n/a | |
---|
285 | n/a | |
---|
286 | n/a | def writes_bytecode_files(fxn): |
---|
287 | n/a | """Decorator to protect sys.dont_write_bytecode from mutation and to skip |
---|
288 | n/a | tests that require it to be set to False.""" |
---|
289 | n/a | if sys.dont_write_bytecode: |
---|
290 | n/a | return lambda *args, **kwargs: None |
---|
291 | n/a | @functools.wraps(fxn) |
---|
292 | n/a | def wrapper(*args, **kwargs): |
---|
293 | n/a | original = sys.dont_write_bytecode |
---|
294 | n/a | sys.dont_write_bytecode = False |
---|
295 | n/a | try: |
---|
296 | n/a | to_return = fxn(*args, **kwargs) |
---|
297 | n/a | finally: |
---|
298 | n/a | sys.dont_write_bytecode = original |
---|
299 | n/a | return to_return |
---|
300 | n/a | return wrapper |
---|
301 | n/a | |
---|
302 | n/a | |
---|
303 | n/a | def ensure_bytecode_path(bytecode_path): |
---|
304 | n/a | """Ensure that the __pycache__ directory for PEP 3147 pyc file exists. |
---|
305 | n/a | |
---|
306 | n/a | :param bytecode_path: File system path to PEP 3147 pyc file. |
---|
307 | n/a | """ |
---|
308 | n/a | try: |
---|
309 | n/a | os.mkdir(os.path.dirname(bytecode_path)) |
---|
310 | n/a | except OSError as error: |
---|
311 | n/a | if error.errno != errno.EEXIST: |
---|
312 | n/a | raise |
---|
313 | n/a | |
---|
314 | n/a | |
---|
315 | n/a | @contextlib.contextmanager |
---|
316 | n/a | def create_modules(*names): |
---|
317 | n/a | """Temporarily create each named module with an attribute (named 'attr') |
---|
318 | n/a | that contains the name passed into the context manager that caused the |
---|
319 | n/a | creation of the module. |
---|
320 | n/a | |
---|
321 | n/a | All files are created in a temporary directory returned by |
---|
322 | n/a | tempfile.mkdtemp(). This directory is inserted at the beginning of |
---|
323 | n/a | sys.path. When the context manager exits all created files (source and |
---|
324 | n/a | bytecode) are explicitly deleted. |
---|
325 | n/a | |
---|
326 | n/a | No magic is performed when creating packages! This means that if you create |
---|
327 | n/a | a module within a package you must also create the package's __init__ as |
---|
328 | n/a | well. |
---|
329 | n/a | |
---|
330 | n/a | """ |
---|
331 | n/a | source = 'attr = {0!r}' |
---|
332 | n/a | created_paths = [] |
---|
333 | n/a | mapping = {} |
---|
334 | n/a | state_manager = None |
---|
335 | n/a | uncache_manager = None |
---|
336 | n/a | try: |
---|
337 | n/a | temp_dir = tempfile.mkdtemp() |
---|
338 | n/a | mapping['.root'] = temp_dir |
---|
339 | n/a | import_names = set() |
---|
340 | n/a | for name in names: |
---|
341 | n/a | if not name.endswith('__init__'): |
---|
342 | n/a | import_name = name |
---|
343 | n/a | else: |
---|
344 | n/a | import_name = name[:-len('.__init__')] |
---|
345 | n/a | import_names.add(import_name) |
---|
346 | n/a | if import_name in sys.modules: |
---|
347 | n/a | del sys.modules[import_name] |
---|
348 | n/a | name_parts = name.split('.') |
---|
349 | n/a | file_path = temp_dir |
---|
350 | n/a | for directory in name_parts[:-1]: |
---|
351 | n/a | file_path = os.path.join(file_path, directory) |
---|
352 | n/a | if not os.path.exists(file_path): |
---|
353 | n/a | os.mkdir(file_path) |
---|
354 | n/a | created_paths.append(file_path) |
---|
355 | n/a | file_path = os.path.join(file_path, name_parts[-1] + '.py') |
---|
356 | n/a | with open(file_path, 'w') as file: |
---|
357 | n/a | file.write(source.format(name)) |
---|
358 | n/a | created_paths.append(file_path) |
---|
359 | n/a | mapping[name] = file_path |
---|
360 | n/a | uncache_manager = uncache(*import_names) |
---|
361 | n/a | uncache_manager.__enter__() |
---|
362 | n/a | state_manager = import_state(path=[temp_dir]) |
---|
363 | n/a | state_manager.__enter__() |
---|
364 | n/a | yield mapping |
---|
365 | n/a | finally: |
---|
366 | n/a | if state_manager is not None: |
---|
367 | n/a | state_manager.__exit__(None, None, None) |
---|
368 | n/a | if uncache_manager is not None: |
---|
369 | n/a | uncache_manager.__exit__(None, None, None) |
---|
370 | n/a | support.rmtree(temp_dir) |
---|
371 | n/a | |
---|
372 | n/a | |
---|
373 | n/a | def mock_path_hook(*entries, importer): |
---|
374 | n/a | """A mock sys.path_hooks entry.""" |
---|
375 | n/a | def hook(entry): |
---|
376 | n/a | if entry not in entries: |
---|
377 | n/a | raise ImportError |
---|
378 | n/a | return importer |
---|
379 | n/a | return hook |
---|
380 | n/a | |
---|
381 | n/a | |
---|
382 | n/a | class CASEOKTestBase: |
---|
383 | n/a | |
---|
384 | n/a | def caseok_env_changed(self, *, should_exist): |
---|
385 | n/a | possibilities = b'PYTHONCASEOK', 'PYTHONCASEOK' |
---|
386 | n/a | if any(x in self.importlib._bootstrap_external._os.environ |
---|
387 | n/a | for x in possibilities) != should_exist: |
---|
388 | n/a | self.skipTest('os.environ changes not reflected in _os.environ') |
---|