1 | n/a | from . import util as test_util |
---|
2 | n/a | |
---|
3 | n/a | init = test_util.import_importlib('importlib') |
---|
4 | n/a | util = test_util.import_importlib('importlib.util') |
---|
5 | n/a | machinery = test_util.import_importlib('importlib.machinery') |
---|
6 | n/a | |
---|
7 | n/a | import os.path |
---|
8 | n/a | import sys |
---|
9 | n/a | from test import support |
---|
10 | n/a | import types |
---|
11 | n/a | import unittest |
---|
12 | n/a | import warnings |
---|
13 | n/a | |
---|
14 | n/a | |
---|
15 | n/a | class ImportModuleTests: |
---|
16 | n/a | |
---|
17 | n/a | """Test importlib.import_module.""" |
---|
18 | n/a | |
---|
19 | n/a | def test_module_import(self): |
---|
20 | n/a | # Test importing a top-level module. |
---|
21 | n/a | with test_util.mock_modules('top_level') as mock: |
---|
22 | n/a | with test_util.import_state(meta_path=[mock]): |
---|
23 | n/a | module = self.init.import_module('top_level') |
---|
24 | n/a | self.assertEqual(module.__name__, 'top_level') |
---|
25 | n/a | |
---|
26 | n/a | def test_absolute_package_import(self): |
---|
27 | n/a | # Test importing a module from a package with an absolute name. |
---|
28 | n/a | pkg_name = 'pkg' |
---|
29 | n/a | pkg_long_name = '{0}.__init__'.format(pkg_name) |
---|
30 | n/a | name = '{0}.mod'.format(pkg_name) |
---|
31 | n/a | with test_util.mock_modules(pkg_long_name, name) as mock: |
---|
32 | n/a | with test_util.import_state(meta_path=[mock]): |
---|
33 | n/a | module = self.init.import_module(name) |
---|
34 | n/a | self.assertEqual(module.__name__, name) |
---|
35 | n/a | |
---|
36 | n/a | def test_shallow_relative_package_import(self): |
---|
37 | n/a | # Test importing a module from a package through a relative import. |
---|
38 | n/a | pkg_name = 'pkg' |
---|
39 | n/a | pkg_long_name = '{0}.__init__'.format(pkg_name) |
---|
40 | n/a | module_name = 'mod' |
---|
41 | n/a | absolute_name = '{0}.{1}'.format(pkg_name, module_name) |
---|
42 | n/a | relative_name = '.{0}'.format(module_name) |
---|
43 | n/a | with test_util.mock_modules(pkg_long_name, absolute_name) as mock: |
---|
44 | n/a | with test_util.import_state(meta_path=[mock]): |
---|
45 | n/a | self.init.import_module(pkg_name) |
---|
46 | n/a | module = self.init.import_module(relative_name, pkg_name) |
---|
47 | n/a | self.assertEqual(module.__name__, absolute_name) |
---|
48 | n/a | |
---|
49 | n/a | def test_deep_relative_package_import(self): |
---|
50 | n/a | modules = ['a.__init__', 'a.b.__init__', 'a.c'] |
---|
51 | n/a | with test_util.mock_modules(*modules) as mock: |
---|
52 | n/a | with test_util.import_state(meta_path=[mock]): |
---|
53 | n/a | self.init.import_module('a') |
---|
54 | n/a | self.init.import_module('a.b') |
---|
55 | n/a | module = self.init.import_module('..c', 'a.b') |
---|
56 | n/a | self.assertEqual(module.__name__, 'a.c') |
---|
57 | n/a | |
---|
58 | n/a | def test_absolute_import_with_package(self): |
---|
59 | n/a | # Test importing a module from a package with an absolute name with |
---|
60 | n/a | # the 'package' argument given. |
---|
61 | n/a | pkg_name = 'pkg' |
---|
62 | n/a | pkg_long_name = '{0}.__init__'.format(pkg_name) |
---|
63 | n/a | name = '{0}.mod'.format(pkg_name) |
---|
64 | n/a | with test_util.mock_modules(pkg_long_name, name) as mock: |
---|
65 | n/a | with test_util.import_state(meta_path=[mock]): |
---|
66 | n/a | self.init.import_module(pkg_name) |
---|
67 | n/a | module = self.init.import_module(name, pkg_name) |
---|
68 | n/a | self.assertEqual(module.__name__, name) |
---|
69 | n/a | |
---|
70 | n/a | def test_relative_import_wo_package(self): |
---|
71 | n/a | # Relative imports cannot happen without the 'package' argument being |
---|
72 | n/a | # set. |
---|
73 | n/a | with self.assertRaises(TypeError): |
---|
74 | n/a | self.init.import_module('.support') |
---|
75 | n/a | |
---|
76 | n/a | |
---|
77 | n/a | def test_loaded_once(self): |
---|
78 | n/a | # Issue #13591: Modules should only be loaded once when |
---|
79 | n/a | # initializing the parent package attempts to import the |
---|
80 | n/a | # module currently being imported. |
---|
81 | n/a | b_load_count = 0 |
---|
82 | n/a | def load_a(): |
---|
83 | n/a | self.init.import_module('a.b') |
---|
84 | n/a | def load_b(): |
---|
85 | n/a | nonlocal b_load_count |
---|
86 | n/a | b_load_count += 1 |
---|
87 | n/a | code = {'a': load_a, 'a.b': load_b} |
---|
88 | n/a | modules = ['a.__init__', 'a.b'] |
---|
89 | n/a | with test_util.mock_modules(*modules, module_code=code) as mock: |
---|
90 | n/a | with test_util.import_state(meta_path=[mock]): |
---|
91 | n/a | self.init.import_module('a.b') |
---|
92 | n/a | self.assertEqual(b_load_count, 1) |
---|
93 | n/a | |
---|
94 | n/a | |
---|
95 | n/a | (Frozen_ImportModuleTests, |
---|
96 | n/a | Source_ImportModuleTests |
---|
97 | n/a | ) = test_util.test_both(ImportModuleTests, init=init) |
---|
98 | n/a | |
---|
99 | n/a | |
---|
100 | n/a | class FindLoaderTests: |
---|
101 | n/a | |
---|
102 | n/a | FakeMetaFinder = None |
---|
103 | n/a | |
---|
104 | n/a | def test_sys_modules(self): |
---|
105 | n/a | # If a module with __loader__ is in sys.modules, then return it. |
---|
106 | n/a | name = 'some_mod' |
---|
107 | n/a | with test_util.uncache(name): |
---|
108 | n/a | module = types.ModuleType(name) |
---|
109 | n/a | loader = 'a loader!' |
---|
110 | n/a | module.__loader__ = loader |
---|
111 | n/a | sys.modules[name] = module |
---|
112 | n/a | with warnings.catch_warnings(): |
---|
113 | n/a | warnings.simplefilter('ignore', DeprecationWarning) |
---|
114 | n/a | found = self.init.find_loader(name) |
---|
115 | n/a | self.assertEqual(loader, found) |
---|
116 | n/a | |
---|
117 | n/a | def test_sys_modules_loader_is_None(self): |
---|
118 | n/a | # If sys.modules[name].__loader__ is None, raise ValueError. |
---|
119 | n/a | name = 'some_mod' |
---|
120 | n/a | with test_util.uncache(name): |
---|
121 | n/a | module = types.ModuleType(name) |
---|
122 | n/a | module.__loader__ = None |
---|
123 | n/a | sys.modules[name] = module |
---|
124 | n/a | with self.assertRaises(ValueError): |
---|
125 | n/a | with warnings.catch_warnings(): |
---|
126 | n/a | warnings.simplefilter('ignore', DeprecationWarning) |
---|
127 | n/a | self.init.find_loader(name) |
---|
128 | n/a | |
---|
129 | n/a | def test_sys_modules_loader_is_not_set(self): |
---|
130 | n/a | # Should raise ValueError |
---|
131 | n/a | # Issue #17099 |
---|
132 | n/a | name = 'some_mod' |
---|
133 | n/a | with test_util.uncache(name): |
---|
134 | n/a | module = types.ModuleType(name) |
---|
135 | n/a | try: |
---|
136 | n/a | del module.__loader__ |
---|
137 | n/a | except AttributeError: |
---|
138 | n/a | pass |
---|
139 | n/a | sys.modules[name] = module |
---|
140 | n/a | with self.assertRaises(ValueError): |
---|
141 | n/a | with warnings.catch_warnings(): |
---|
142 | n/a | warnings.simplefilter('ignore', DeprecationWarning) |
---|
143 | n/a | self.init.find_loader(name) |
---|
144 | n/a | |
---|
145 | n/a | def test_success(self): |
---|
146 | n/a | # Return the loader found on sys.meta_path. |
---|
147 | n/a | name = 'some_mod' |
---|
148 | n/a | with test_util.uncache(name): |
---|
149 | n/a | with test_util.import_state(meta_path=[self.FakeMetaFinder]): |
---|
150 | n/a | with warnings.catch_warnings(): |
---|
151 | n/a | warnings.simplefilter('ignore', DeprecationWarning) |
---|
152 | n/a | self.assertEqual((name, None), self.init.find_loader(name)) |
---|
153 | n/a | |
---|
154 | n/a | def test_success_path(self): |
---|
155 | n/a | # Searching on a path should work. |
---|
156 | n/a | name = 'some_mod' |
---|
157 | n/a | path = 'path to some place' |
---|
158 | n/a | with test_util.uncache(name): |
---|
159 | n/a | with test_util.import_state(meta_path=[self.FakeMetaFinder]): |
---|
160 | n/a | with warnings.catch_warnings(): |
---|
161 | n/a | warnings.simplefilter('ignore', DeprecationWarning) |
---|
162 | n/a | self.assertEqual((name, path), |
---|
163 | n/a | self.init.find_loader(name, path)) |
---|
164 | n/a | |
---|
165 | n/a | def test_nothing(self): |
---|
166 | n/a | # None is returned upon failure to find a loader. |
---|
167 | n/a | with warnings.catch_warnings(): |
---|
168 | n/a | warnings.simplefilter('ignore', DeprecationWarning) |
---|
169 | n/a | self.assertIsNone(self.init.find_loader('nevergoingtofindthismodule')) |
---|
170 | n/a | |
---|
171 | n/a | |
---|
172 | n/a | class FindLoaderPEP451Tests(FindLoaderTests): |
---|
173 | n/a | |
---|
174 | n/a | class FakeMetaFinder: |
---|
175 | n/a | @staticmethod |
---|
176 | n/a | def find_spec(name, path=None, target=None): |
---|
177 | n/a | return machinery['Source'].ModuleSpec(name, (name, path)) |
---|
178 | n/a | |
---|
179 | n/a | |
---|
180 | n/a | (Frozen_FindLoaderPEP451Tests, |
---|
181 | n/a | Source_FindLoaderPEP451Tests |
---|
182 | n/a | ) = test_util.test_both(FindLoaderPEP451Tests, init=init) |
---|
183 | n/a | |
---|
184 | n/a | |
---|
185 | n/a | class FindLoaderPEP302Tests(FindLoaderTests): |
---|
186 | n/a | |
---|
187 | n/a | class FakeMetaFinder: |
---|
188 | n/a | @staticmethod |
---|
189 | n/a | def find_module(name, path=None): |
---|
190 | n/a | return name, path |
---|
191 | n/a | |
---|
192 | n/a | |
---|
193 | n/a | (Frozen_FindLoaderPEP302Tests, |
---|
194 | n/a | Source_FindLoaderPEP302Tests |
---|
195 | n/a | ) = test_util.test_both(FindLoaderPEP302Tests, init=init) |
---|
196 | n/a | |
---|
197 | n/a | |
---|
198 | n/a | class ReloadTests: |
---|
199 | n/a | |
---|
200 | n/a | """Test module reloading for builtin and extension modules.""" |
---|
201 | n/a | |
---|
202 | n/a | def test_reload_modules(self): |
---|
203 | n/a | for mod in ('tokenize', 'time', 'marshal'): |
---|
204 | n/a | with self.subTest(module=mod): |
---|
205 | n/a | with support.CleanImport(mod): |
---|
206 | n/a | module = self.init.import_module(mod) |
---|
207 | n/a | self.init.reload(module) |
---|
208 | n/a | |
---|
209 | n/a | def test_module_replaced(self): |
---|
210 | n/a | def code(): |
---|
211 | n/a | import sys |
---|
212 | n/a | module = type(sys)('top_level') |
---|
213 | n/a | module.spam = 3 |
---|
214 | n/a | sys.modules['top_level'] = module |
---|
215 | n/a | mock = test_util.mock_modules('top_level', |
---|
216 | n/a | module_code={'top_level': code}) |
---|
217 | n/a | with mock: |
---|
218 | n/a | with test_util.import_state(meta_path=[mock]): |
---|
219 | n/a | module = self.init.import_module('top_level') |
---|
220 | n/a | reloaded = self.init.reload(module) |
---|
221 | n/a | actual = sys.modules['top_level'] |
---|
222 | n/a | self.assertEqual(actual.spam, 3) |
---|
223 | n/a | self.assertEqual(reloaded.spam, 3) |
---|
224 | n/a | |
---|
225 | n/a | def test_reload_missing_loader(self): |
---|
226 | n/a | with support.CleanImport('types'): |
---|
227 | n/a | import types |
---|
228 | n/a | loader = types.__loader__ |
---|
229 | n/a | del types.__loader__ |
---|
230 | n/a | reloaded = self.init.reload(types) |
---|
231 | n/a | |
---|
232 | n/a | self.assertIs(reloaded, types) |
---|
233 | n/a | self.assertIs(sys.modules['types'], types) |
---|
234 | n/a | self.assertEqual(reloaded.__loader__.path, loader.path) |
---|
235 | n/a | |
---|
236 | n/a | def test_reload_loader_replaced(self): |
---|
237 | n/a | with support.CleanImport('types'): |
---|
238 | n/a | import types |
---|
239 | n/a | types.__loader__ = None |
---|
240 | n/a | self.init.invalidate_caches() |
---|
241 | n/a | reloaded = self.init.reload(types) |
---|
242 | n/a | |
---|
243 | n/a | self.assertIsNot(reloaded.__loader__, None) |
---|
244 | n/a | self.assertIs(reloaded, types) |
---|
245 | n/a | self.assertIs(sys.modules['types'], types) |
---|
246 | n/a | |
---|
247 | n/a | def test_reload_location_changed(self): |
---|
248 | n/a | name = 'spam' |
---|
249 | n/a | with support.temp_cwd(None) as cwd: |
---|
250 | n/a | with test_util.uncache('spam'): |
---|
251 | n/a | with support.DirsOnSysPath(cwd): |
---|
252 | n/a | # Start as a plain module. |
---|
253 | n/a | self.init.invalidate_caches() |
---|
254 | n/a | path = os.path.join(cwd, name + '.py') |
---|
255 | n/a | cached = self.util.cache_from_source(path) |
---|
256 | n/a | expected = {'__name__': name, |
---|
257 | n/a | '__package__': '', |
---|
258 | n/a | '__file__': path, |
---|
259 | n/a | '__cached__': cached, |
---|
260 | n/a | '__doc__': None, |
---|
261 | n/a | } |
---|
262 | n/a | support.create_empty_file(path) |
---|
263 | n/a | module = self.init.import_module(name) |
---|
264 | n/a | ns = vars(module).copy() |
---|
265 | n/a | loader = ns.pop('__loader__') |
---|
266 | n/a | spec = ns.pop('__spec__') |
---|
267 | n/a | ns.pop('__builtins__', None) # An implementation detail. |
---|
268 | n/a | self.assertEqual(spec.name, name) |
---|
269 | n/a | self.assertEqual(spec.loader, loader) |
---|
270 | n/a | self.assertEqual(loader.path, path) |
---|
271 | n/a | self.assertEqual(ns, expected) |
---|
272 | n/a | |
---|
273 | n/a | # Change to a package. |
---|
274 | n/a | self.init.invalidate_caches() |
---|
275 | n/a | init_path = os.path.join(cwd, name, '__init__.py') |
---|
276 | n/a | cached = self.util.cache_from_source(init_path) |
---|
277 | n/a | expected = {'__name__': name, |
---|
278 | n/a | '__package__': name, |
---|
279 | n/a | '__file__': init_path, |
---|
280 | n/a | '__cached__': cached, |
---|
281 | n/a | '__path__': [os.path.dirname(init_path)], |
---|
282 | n/a | '__doc__': None, |
---|
283 | n/a | } |
---|
284 | n/a | os.mkdir(name) |
---|
285 | n/a | os.rename(path, init_path) |
---|
286 | n/a | reloaded = self.init.reload(module) |
---|
287 | n/a | ns = vars(reloaded).copy() |
---|
288 | n/a | loader = ns.pop('__loader__') |
---|
289 | n/a | spec = ns.pop('__spec__') |
---|
290 | n/a | ns.pop('__builtins__', None) # An implementation detail. |
---|
291 | n/a | self.assertEqual(spec.name, name) |
---|
292 | n/a | self.assertEqual(spec.loader, loader) |
---|
293 | n/a | self.assertIs(reloaded, module) |
---|
294 | n/a | self.assertEqual(loader.path, init_path) |
---|
295 | n/a | self.maxDiff = None |
---|
296 | n/a | self.assertEqual(ns, expected) |
---|
297 | n/a | |
---|
298 | n/a | def test_reload_namespace_changed(self): |
---|
299 | n/a | name = 'spam' |
---|
300 | n/a | with support.temp_cwd(None) as cwd: |
---|
301 | n/a | with test_util.uncache('spam'): |
---|
302 | n/a | with support.DirsOnSysPath(cwd): |
---|
303 | n/a | # Start as a namespace package. |
---|
304 | n/a | self.init.invalidate_caches() |
---|
305 | n/a | bad_path = os.path.join(cwd, name, '__init.py') |
---|
306 | n/a | cached = self.util.cache_from_source(bad_path) |
---|
307 | n/a | expected = {'__name__': name, |
---|
308 | n/a | '__package__': name, |
---|
309 | n/a | '__doc__': None, |
---|
310 | n/a | } |
---|
311 | n/a | os.mkdir(name) |
---|
312 | n/a | with open(bad_path, 'w') as init_file: |
---|
313 | n/a | init_file.write('eggs = None') |
---|
314 | n/a | module = self.init.import_module(name) |
---|
315 | n/a | ns = vars(module).copy() |
---|
316 | n/a | loader = ns.pop('__loader__') |
---|
317 | n/a | path = ns.pop('__path__') |
---|
318 | n/a | spec = ns.pop('__spec__') |
---|
319 | n/a | ns.pop('__builtins__', None) # An implementation detail. |
---|
320 | n/a | self.assertEqual(spec.name, name) |
---|
321 | n/a | self.assertIs(spec.loader, None) |
---|
322 | n/a | self.assertIsNot(loader, None) |
---|
323 | n/a | self.assertEqual(set(path), |
---|
324 | n/a | set([os.path.dirname(bad_path)])) |
---|
325 | n/a | with self.assertRaises(AttributeError): |
---|
326 | n/a | # a NamespaceLoader |
---|
327 | n/a | loader.path |
---|
328 | n/a | self.assertEqual(ns, expected) |
---|
329 | n/a | |
---|
330 | n/a | # Change to a regular package. |
---|
331 | n/a | self.init.invalidate_caches() |
---|
332 | n/a | init_path = os.path.join(cwd, name, '__init__.py') |
---|
333 | n/a | cached = self.util.cache_from_source(init_path) |
---|
334 | n/a | expected = {'__name__': name, |
---|
335 | n/a | '__package__': name, |
---|
336 | n/a | '__file__': init_path, |
---|
337 | n/a | '__cached__': cached, |
---|
338 | n/a | '__path__': [os.path.dirname(init_path)], |
---|
339 | n/a | '__doc__': None, |
---|
340 | n/a | 'eggs': None, |
---|
341 | n/a | } |
---|
342 | n/a | os.rename(bad_path, init_path) |
---|
343 | n/a | reloaded = self.init.reload(module) |
---|
344 | n/a | ns = vars(reloaded).copy() |
---|
345 | n/a | loader = ns.pop('__loader__') |
---|
346 | n/a | spec = ns.pop('__spec__') |
---|
347 | n/a | ns.pop('__builtins__', None) # An implementation detail. |
---|
348 | n/a | self.assertEqual(spec.name, name) |
---|
349 | n/a | self.assertEqual(spec.loader, loader) |
---|
350 | n/a | self.assertIs(reloaded, module) |
---|
351 | n/a | self.assertEqual(loader.path, init_path) |
---|
352 | n/a | self.assertEqual(ns, expected) |
---|
353 | n/a | |
---|
354 | n/a | def test_reload_submodule(self): |
---|
355 | n/a | # See #19851. |
---|
356 | n/a | name = 'spam' |
---|
357 | n/a | subname = 'ham' |
---|
358 | n/a | with test_util.temp_module(name, pkg=True) as pkg_dir: |
---|
359 | n/a | fullname, _ = test_util.submodule(name, subname, pkg_dir) |
---|
360 | n/a | ham = self.init.import_module(fullname) |
---|
361 | n/a | reloaded = self.init.reload(ham) |
---|
362 | n/a | self.assertIs(reloaded, ham) |
---|
363 | n/a | |
---|
364 | n/a | |
---|
365 | n/a | (Frozen_ReloadTests, |
---|
366 | n/a | Source_ReloadTests |
---|
367 | n/a | ) = test_util.test_both(ReloadTests, init=init, util=util) |
---|
368 | n/a | |
---|
369 | n/a | |
---|
370 | n/a | class InvalidateCacheTests: |
---|
371 | n/a | |
---|
372 | n/a | def test_method_called(self): |
---|
373 | n/a | # If defined the method should be called. |
---|
374 | n/a | class InvalidatingNullFinder: |
---|
375 | n/a | def __init__(self, *ignored): |
---|
376 | n/a | self.called = False |
---|
377 | n/a | def find_module(self, *args): |
---|
378 | n/a | return None |
---|
379 | n/a | def invalidate_caches(self): |
---|
380 | n/a | self.called = True |
---|
381 | n/a | |
---|
382 | n/a | key = 'gobledeegook' |
---|
383 | n/a | meta_ins = InvalidatingNullFinder() |
---|
384 | n/a | path_ins = InvalidatingNullFinder() |
---|
385 | n/a | sys.meta_path.insert(0, meta_ins) |
---|
386 | n/a | self.addCleanup(lambda: sys.path_importer_cache.__delitem__(key)) |
---|
387 | n/a | sys.path_importer_cache[key] = path_ins |
---|
388 | n/a | self.addCleanup(lambda: sys.meta_path.remove(meta_ins)) |
---|
389 | n/a | self.init.invalidate_caches() |
---|
390 | n/a | self.assertTrue(meta_ins.called) |
---|
391 | n/a | self.assertTrue(path_ins.called) |
---|
392 | n/a | |
---|
393 | n/a | def test_method_lacking(self): |
---|
394 | n/a | # There should be no issues if the method is not defined. |
---|
395 | n/a | key = 'gobbledeegook' |
---|
396 | n/a | sys.path_importer_cache[key] = None |
---|
397 | n/a | self.addCleanup(lambda: sys.path_importer_cache.__delitem__(key)) |
---|
398 | n/a | self.init.invalidate_caches() # Shouldn't trigger an exception. |
---|
399 | n/a | |
---|
400 | n/a | |
---|
401 | n/a | (Frozen_InvalidateCacheTests, |
---|
402 | n/a | Source_InvalidateCacheTests |
---|
403 | n/a | ) = test_util.test_both(InvalidateCacheTests, init=init) |
---|
404 | n/a | |
---|
405 | n/a | |
---|
406 | n/a | class FrozenImportlibTests(unittest.TestCase): |
---|
407 | n/a | |
---|
408 | n/a | def test_no_frozen_importlib(self): |
---|
409 | n/a | # Should be able to import w/o _frozen_importlib being defined. |
---|
410 | n/a | # Can't do an isinstance() check since separate copies of importlib |
---|
411 | n/a | # may have been used for import, so just check the name is not for the |
---|
412 | n/a | # frozen loader. |
---|
413 | n/a | source_init = init['Source'] |
---|
414 | n/a | self.assertNotEqual(source_init.__loader__.__class__.__name__, |
---|
415 | n/a | 'FrozenImporter') |
---|
416 | n/a | |
---|
417 | n/a | |
---|
418 | n/a | class StartupTests: |
---|
419 | n/a | |
---|
420 | n/a | def test_everyone_has___loader__(self): |
---|
421 | n/a | # Issue #17098: all modules should have __loader__ defined. |
---|
422 | n/a | for name, module in sys.modules.items(): |
---|
423 | n/a | if isinstance(module, types.ModuleType): |
---|
424 | n/a | with self.subTest(name=name): |
---|
425 | n/a | self.assertTrue(hasattr(module, '__loader__'), |
---|
426 | n/a | '{!r} lacks a __loader__ attribute'.format(name)) |
---|
427 | n/a | if self.machinery.BuiltinImporter.find_module(name): |
---|
428 | n/a | self.assertIsNot(module.__loader__, None) |
---|
429 | n/a | elif self.machinery.FrozenImporter.find_module(name): |
---|
430 | n/a | self.assertIsNot(module.__loader__, None) |
---|
431 | n/a | |
---|
432 | n/a | def test_everyone_has___spec__(self): |
---|
433 | n/a | for name, module in sys.modules.items(): |
---|
434 | n/a | if isinstance(module, types.ModuleType): |
---|
435 | n/a | with self.subTest(name=name): |
---|
436 | n/a | self.assertTrue(hasattr(module, '__spec__')) |
---|
437 | n/a | if self.machinery.BuiltinImporter.find_module(name): |
---|
438 | n/a | self.assertIsNot(module.__spec__, None) |
---|
439 | n/a | elif self.machinery.FrozenImporter.find_module(name): |
---|
440 | n/a | self.assertIsNot(module.__spec__, None) |
---|
441 | n/a | |
---|
442 | n/a | |
---|
443 | n/a | (Frozen_StartupTests, |
---|
444 | n/a | Source_StartupTests |
---|
445 | n/a | ) = test_util.test_both(StartupTests, machinery=machinery) |
---|
446 | n/a | |
---|
447 | n/a | |
---|
448 | n/a | if __name__ == '__main__': |
---|
449 | n/a | unittest.main() |
---|