1 | n/a | from . import util as test_util |
---|
2 | n/a | |
---|
3 | n/a | init = test_util.import_importlib('importlib') |
---|
4 | n/a | machinery = test_util.import_importlib('importlib.machinery') |
---|
5 | n/a | util = test_util.import_importlib('importlib.util') |
---|
6 | n/a | |
---|
7 | n/a | import os.path |
---|
8 | n/a | import pathlib |
---|
9 | n/a | from test.support import CleanImport |
---|
10 | n/a | import unittest |
---|
11 | n/a | import sys |
---|
12 | n/a | import warnings |
---|
13 | n/a | |
---|
14 | n/a | |
---|
15 | n/a | |
---|
16 | n/a | class TestLoader: |
---|
17 | n/a | |
---|
18 | n/a | def __init__(self, path=None, is_package=None): |
---|
19 | n/a | self.path = path |
---|
20 | n/a | self.package = is_package |
---|
21 | n/a | |
---|
22 | n/a | def __repr__(self): |
---|
23 | n/a | return '<TestLoader object>' |
---|
24 | n/a | |
---|
25 | n/a | def __getattr__(self, name): |
---|
26 | n/a | if name == 'get_filename' and self.path is not None: |
---|
27 | n/a | return self._get_filename |
---|
28 | n/a | if name == 'is_package': |
---|
29 | n/a | return self._is_package |
---|
30 | n/a | raise AttributeError(name) |
---|
31 | n/a | |
---|
32 | n/a | def _get_filename(self, name): |
---|
33 | n/a | return self.path |
---|
34 | n/a | |
---|
35 | n/a | def _is_package(self, name): |
---|
36 | n/a | return self.package |
---|
37 | n/a | |
---|
38 | n/a | def create_module(self, spec): |
---|
39 | n/a | return None |
---|
40 | n/a | |
---|
41 | n/a | |
---|
42 | n/a | class NewLoader(TestLoader): |
---|
43 | n/a | |
---|
44 | n/a | EGGS = 1 |
---|
45 | n/a | |
---|
46 | n/a | def exec_module(self, module): |
---|
47 | n/a | module.eggs = self.EGGS |
---|
48 | n/a | |
---|
49 | n/a | |
---|
50 | n/a | class LegacyLoader(TestLoader): |
---|
51 | n/a | |
---|
52 | n/a | HAM = -1 |
---|
53 | n/a | |
---|
54 | n/a | with warnings.catch_warnings(): |
---|
55 | n/a | warnings.simplefilter("ignore", DeprecationWarning) |
---|
56 | n/a | |
---|
57 | n/a | frozen_util = util['Frozen'] |
---|
58 | n/a | |
---|
59 | n/a | @frozen_util.module_for_loader |
---|
60 | n/a | def load_module(self, module): |
---|
61 | n/a | module.ham = self.HAM |
---|
62 | n/a | return module |
---|
63 | n/a | |
---|
64 | n/a | |
---|
65 | n/a | class ModuleSpecTests: |
---|
66 | n/a | |
---|
67 | n/a | def setUp(self): |
---|
68 | n/a | self.name = 'spam' |
---|
69 | n/a | self.path = 'spam.py' |
---|
70 | n/a | self.cached = self.util.cache_from_source(self.path) |
---|
71 | n/a | self.loader = TestLoader() |
---|
72 | n/a | self.spec = self.machinery.ModuleSpec(self.name, self.loader) |
---|
73 | n/a | self.loc_spec = self.machinery.ModuleSpec(self.name, self.loader, |
---|
74 | n/a | origin=self.path) |
---|
75 | n/a | self.loc_spec._set_fileattr = True |
---|
76 | n/a | |
---|
77 | n/a | def test_default(self): |
---|
78 | n/a | spec = self.machinery.ModuleSpec(self.name, self.loader) |
---|
79 | n/a | |
---|
80 | n/a | self.assertEqual(spec.name, self.name) |
---|
81 | n/a | self.assertEqual(spec.loader, self.loader) |
---|
82 | n/a | self.assertIs(spec.origin, None) |
---|
83 | n/a | self.assertIs(spec.loader_state, None) |
---|
84 | n/a | self.assertIs(spec.submodule_search_locations, None) |
---|
85 | n/a | self.assertIs(spec.cached, None) |
---|
86 | n/a | self.assertFalse(spec.has_location) |
---|
87 | n/a | |
---|
88 | n/a | def test_default_no_loader(self): |
---|
89 | n/a | spec = self.machinery.ModuleSpec(self.name, None) |
---|
90 | n/a | |
---|
91 | n/a | self.assertEqual(spec.name, self.name) |
---|
92 | n/a | self.assertIs(spec.loader, None) |
---|
93 | n/a | self.assertIs(spec.origin, None) |
---|
94 | n/a | self.assertIs(spec.loader_state, None) |
---|
95 | n/a | self.assertIs(spec.submodule_search_locations, None) |
---|
96 | n/a | self.assertIs(spec.cached, None) |
---|
97 | n/a | self.assertFalse(spec.has_location) |
---|
98 | n/a | |
---|
99 | n/a | def test_default_is_package_false(self): |
---|
100 | n/a | spec = self.machinery.ModuleSpec(self.name, self.loader, |
---|
101 | n/a | is_package=False) |
---|
102 | n/a | |
---|
103 | n/a | self.assertEqual(spec.name, self.name) |
---|
104 | n/a | self.assertEqual(spec.loader, self.loader) |
---|
105 | n/a | self.assertIs(spec.origin, None) |
---|
106 | n/a | self.assertIs(spec.loader_state, None) |
---|
107 | n/a | self.assertIs(spec.submodule_search_locations, None) |
---|
108 | n/a | self.assertIs(spec.cached, None) |
---|
109 | n/a | self.assertFalse(spec.has_location) |
---|
110 | n/a | |
---|
111 | n/a | def test_default_is_package_true(self): |
---|
112 | n/a | spec = self.machinery.ModuleSpec(self.name, self.loader, |
---|
113 | n/a | is_package=True) |
---|
114 | n/a | |
---|
115 | n/a | self.assertEqual(spec.name, self.name) |
---|
116 | n/a | self.assertEqual(spec.loader, self.loader) |
---|
117 | n/a | self.assertIs(spec.origin, None) |
---|
118 | n/a | self.assertIs(spec.loader_state, None) |
---|
119 | n/a | self.assertEqual(spec.submodule_search_locations, []) |
---|
120 | n/a | self.assertIs(spec.cached, None) |
---|
121 | n/a | self.assertFalse(spec.has_location) |
---|
122 | n/a | |
---|
123 | n/a | def test_has_location_setter(self): |
---|
124 | n/a | spec = self.machinery.ModuleSpec(self.name, self.loader, |
---|
125 | n/a | origin='somewhere') |
---|
126 | n/a | self.assertFalse(spec.has_location) |
---|
127 | n/a | spec.has_location = True |
---|
128 | n/a | self.assertTrue(spec.has_location) |
---|
129 | n/a | |
---|
130 | n/a | def test_equality(self): |
---|
131 | n/a | other = type(sys.implementation)(name=self.name, |
---|
132 | n/a | loader=self.loader, |
---|
133 | n/a | origin=None, |
---|
134 | n/a | submodule_search_locations=None, |
---|
135 | n/a | has_location=False, |
---|
136 | n/a | cached=None, |
---|
137 | n/a | ) |
---|
138 | n/a | |
---|
139 | n/a | self.assertTrue(self.spec == other) |
---|
140 | n/a | |
---|
141 | n/a | def test_equality_location(self): |
---|
142 | n/a | other = type(sys.implementation)(name=self.name, |
---|
143 | n/a | loader=self.loader, |
---|
144 | n/a | origin=self.path, |
---|
145 | n/a | submodule_search_locations=None, |
---|
146 | n/a | has_location=True, |
---|
147 | n/a | cached=self.cached, |
---|
148 | n/a | ) |
---|
149 | n/a | |
---|
150 | n/a | self.assertEqual(self.loc_spec, other) |
---|
151 | n/a | |
---|
152 | n/a | def test_inequality(self): |
---|
153 | n/a | other = type(sys.implementation)(name='ham', |
---|
154 | n/a | loader=self.loader, |
---|
155 | n/a | origin=None, |
---|
156 | n/a | submodule_search_locations=None, |
---|
157 | n/a | has_location=False, |
---|
158 | n/a | cached=None, |
---|
159 | n/a | ) |
---|
160 | n/a | |
---|
161 | n/a | self.assertNotEqual(self.spec, other) |
---|
162 | n/a | |
---|
163 | n/a | def test_inequality_incomplete(self): |
---|
164 | n/a | other = type(sys.implementation)(name=self.name, |
---|
165 | n/a | loader=self.loader, |
---|
166 | n/a | ) |
---|
167 | n/a | |
---|
168 | n/a | self.assertNotEqual(self.spec, other) |
---|
169 | n/a | |
---|
170 | n/a | def test_package(self): |
---|
171 | n/a | spec = self.machinery.ModuleSpec('spam.eggs', self.loader) |
---|
172 | n/a | |
---|
173 | n/a | self.assertEqual(spec.parent, 'spam') |
---|
174 | n/a | |
---|
175 | n/a | def test_package_is_package(self): |
---|
176 | n/a | spec = self.machinery.ModuleSpec('spam.eggs', self.loader, |
---|
177 | n/a | is_package=True) |
---|
178 | n/a | |
---|
179 | n/a | self.assertEqual(spec.parent, 'spam.eggs') |
---|
180 | n/a | |
---|
181 | n/a | # cached |
---|
182 | n/a | |
---|
183 | n/a | def test_cached_set(self): |
---|
184 | n/a | before = self.spec.cached |
---|
185 | n/a | self.spec.cached = 'there' |
---|
186 | n/a | after = self.spec.cached |
---|
187 | n/a | |
---|
188 | n/a | self.assertIs(before, None) |
---|
189 | n/a | self.assertEqual(after, 'there') |
---|
190 | n/a | |
---|
191 | n/a | def test_cached_no_origin(self): |
---|
192 | n/a | spec = self.machinery.ModuleSpec(self.name, self.loader) |
---|
193 | n/a | |
---|
194 | n/a | self.assertIs(spec.cached, None) |
---|
195 | n/a | |
---|
196 | n/a | def test_cached_with_origin_not_location(self): |
---|
197 | n/a | spec = self.machinery.ModuleSpec(self.name, self.loader, |
---|
198 | n/a | origin=self.path) |
---|
199 | n/a | |
---|
200 | n/a | self.assertIs(spec.cached, None) |
---|
201 | n/a | |
---|
202 | n/a | def test_cached_source(self): |
---|
203 | n/a | expected = self.util.cache_from_source(self.path) |
---|
204 | n/a | |
---|
205 | n/a | self.assertEqual(self.loc_spec.cached, expected) |
---|
206 | n/a | |
---|
207 | n/a | def test_cached_source_unknown_suffix(self): |
---|
208 | n/a | self.loc_spec.origin = 'spam.spamspamspam' |
---|
209 | n/a | |
---|
210 | n/a | self.assertIs(self.loc_spec.cached, None) |
---|
211 | n/a | |
---|
212 | n/a | def test_cached_source_missing_cache_tag(self): |
---|
213 | n/a | original = sys.implementation.cache_tag |
---|
214 | n/a | sys.implementation.cache_tag = None |
---|
215 | n/a | try: |
---|
216 | n/a | cached = self.loc_spec.cached |
---|
217 | n/a | finally: |
---|
218 | n/a | sys.implementation.cache_tag = original |
---|
219 | n/a | |
---|
220 | n/a | self.assertIs(cached, None) |
---|
221 | n/a | |
---|
222 | n/a | def test_cached_sourceless(self): |
---|
223 | n/a | self.loc_spec.origin = 'spam.pyc' |
---|
224 | n/a | |
---|
225 | n/a | self.assertEqual(self.loc_spec.cached, 'spam.pyc') |
---|
226 | n/a | |
---|
227 | n/a | |
---|
228 | n/a | (Frozen_ModuleSpecTests, |
---|
229 | n/a | Source_ModuleSpecTests |
---|
230 | n/a | ) = test_util.test_both(ModuleSpecTests, util=util, machinery=machinery) |
---|
231 | n/a | |
---|
232 | n/a | |
---|
233 | n/a | class ModuleSpecMethodsTests: |
---|
234 | n/a | |
---|
235 | n/a | @property |
---|
236 | n/a | def bootstrap(self): |
---|
237 | n/a | return self.init._bootstrap |
---|
238 | n/a | |
---|
239 | n/a | def setUp(self): |
---|
240 | n/a | self.name = 'spam' |
---|
241 | n/a | self.path = 'spam.py' |
---|
242 | n/a | self.cached = self.util.cache_from_source(self.path) |
---|
243 | n/a | self.loader = TestLoader() |
---|
244 | n/a | self.spec = self.machinery.ModuleSpec(self.name, self.loader) |
---|
245 | n/a | self.loc_spec = self.machinery.ModuleSpec(self.name, self.loader, |
---|
246 | n/a | origin=self.path) |
---|
247 | n/a | self.loc_spec._set_fileattr = True |
---|
248 | n/a | |
---|
249 | n/a | # exec() |
---|
250 | n/a | |
---|
251 | n/a | def test_exec(self): |
---|
252 | n/a | self.spec.loader = NewLoader() |
---|
253 | n/a | module = self.util.module_from_spec(self.spec) |
---|
254 | n/a | sys.modules[self.name] = module |
---|
255 | n/a | self.assertFalse(hasattr(module, 'eggs')) |
---|
256 | n/a | self.bootstrap._exec(self.spec, module) |
---|
257 | n/a | |
---|
258 | n/a | self.assertEqual(module.eggs, 1) |
---|
259 | n/a | |
---|
260 | n/a | # load() |
---|
261 | n/a | |
---|
262 | n/a | def test_load(self): |
---|
263 | n/a | self.spec.loader = NewLoader() |
---|
264 | n/a | with CleanImport(self.spec.name): |
---|
265 | n/a | loaded = self.bootstrap._load(self.spec) |
---|
266 | n/a | installed = sys.modules[self.spec.name] |
---|
267 | n/a | |
---|
268 | n/a | self.assertEqual(loaded.eggs, 1) |
---|
269 | n/a | self.assertIs(loaded, installed) |
---|
270 | n/a | |
---|
271 | n/a | def test_load_replaced(self): |
---|
272 | n/a | replacement = object() |
---|
273 | n/a | class ReplacingLoader(TestLoader): |
---|
274 | n/a | def exec_module(self, module): |
---|
275 | n/a | sys.modules[module.__name__] = replacement |
---|
276 | n/a | self.spec.loader = ReplacingLoader() |
---|
277 | n/a | with CleanImport(self.spec.name): |
---|
278 | n/a | loaded = self.bootstrap._load(self.spec) |
---|
279 | n/a | installed = sys.modules[self.spec.name] |
---|
280 | n/a | |
---|
281 | n/a | self.assertIs(loaded, replacement) |
---|
282 | n/a | self.assertIs(installed, replacement) |
---|
283 | n/a | |
---|
284 | n/a | def test_load_failed(self): |
---|
285 | n/a | class FailedLoader(TestLoader): |
---|
286 | n/a | def exec_module(self, module): |
---|
287 | n/a | raise RuntimeError |
---|
288 | n/a | self.spec.loader = FailedLoader() |
---|
289 | n/a | with CleanImport(self.spec.name): |
---|
290 | n/a | with self.assertRaises(RuntimeError): |
---|
291 | n/a | loaded = self.bootstrap._load(self.spec) |
---|
292 | n/a | self.assertNotIn(self.spec.name, sys.modules) |
---|
293 | n/a | |
---|
294 | n/a | def test_load_failed_removed(self): |
---|
295 | n/a | class FailedLoader(TestLoader): |
---|
296 | n/a | def exec_module(self, module): |
---|
297 | n/a | del sys.modules[module.__name__] |
---|
298 | n/a | raise RuntimeError |
---|
299 | n/a | self.spec.loader = FailedLoader() |
---|
300 | n/a | with CleanImport(self.spec.name): |
---|
301 | n/a | with self.assertRaises(RuntimeError): |
---|
302 | n/a | loaded = self.bootstrap._load(self.spec) |
---|
303 | n/a | self.assertNotIn(self.spec.name, sys.modules) |
---|
304 | n/a | |
---|
305 | n/a | def test_load_legacy(self): |
---|
306 | n/a | self.spec.loader = LegacyLoader() |
---|
307 | n/a | with CleanImport(self.spec.name): |
---|
308 | n/a | loaded = self.bootstrap._load(self.spec) |
---|
309 | n/a | |
---|
310 | n/a | self.assertEqual(loaded.ham, -1) |
---|
311 | n/a | |
---|
312 | n/a | def test_load_legacy_attributes(self): |
---|
313 | n/a | self.spec.loader = LegacyLoader() |
---|
314 | n/a | with CleanImport(self.spec.name): |
---|
315 | n/a | loaded = self.bootstrap._load(self.spec) |
---|
316 | n/a | |
---|
317 | n/a | self.assertIs(loaded.__loader__, self.spec.loader) |
---|
318 | n/a | self.assertEqual(loaded.__package__, self.spec.parent) |
---|
319 | n/a | self.assertIs(loaded.__spec__, self.spec) |
---|
320 | n/a | |
---|
321 | n/a | def test_load_legacy_attributes_immutable(self): |
---|
322 | n/a | module = object() |
---|
323 | n/a | class ImmutableLoader(TestLoader): |
---|
324 | n/a | def load_module(self, name): |
---|
325 | n/a | sys.modules[name] = module |
---|
326 | n/a | return module |
---|
327 | n/a | self.spec.loader = ImmutableLoader() |
---|
328 | n/a | with CleanImport(self.spec.name): |
---|
329 | n/a | loaded = self.bootstrap._load(self.spec) |
---|
330 | n/a | |
---|
331 | n/a | self.assertIs(sys.modules[self.spec.name], module) |
---|
332 | n/a | |
---|
333 | n/a | # reload() |
---|
334 | n/a | |
---|
335 | n/a | def test_reload(self): |
---|
336 | n/a | self.spec.loader = NewLoader() |
---|
337 | n/a | with CleanImport(self.spec.name): |
---|
338 | n/a | loaded = self.bootstrap._load(self.spec) |
---|
339 | n/a | reloaded = self.bootstrap._exec(self.spec, loaded) |
---|
340 | n/a | installed = sys.modules[self.spec.name] |
---|
341 | n/a | |
---|
342 | n/a | self.assertEqual(loaded.eggs, 1) |
---|
343 | n/a | self.assertIs(reloaded, loaded) |
---|
344 | n/a | self.assertIs(installed, loaded) |
---|
345 | n/a | |
---|
346 | n/a | def test_reload_modified(self): |
---|
347 | n/a | self.spec.loader = NewLoader() |
---|
348 | n/a | with CleanImport(self.spec.name): |
---|
349 | n/a | loaded = self.bootstrap._load(self.spec) |
---|
350 | n/a | loaded.eggs = 2 |
---|
351 | n/a | reloaded = self.bootstrap._exec(self.spec, loaded) |
---|
352 | n/a | |
---|
353 | n/a | self.assertEqual(loaded.eggs, 1) |
---|
354 | n/a | self.assertIs(reloaded, loaded) |
---|
355 | n/a | |
---|
356 | n/a | def test_reload_extra_attributes(self): |
---|
357 | n/a | self.spec.loader = NewLoader() |
---|
358 | n/a | with CleanImport(self.spec.name): |
---|
359 | n/a | loaded = self.bootstrap._load(self.spec) |
---|
360 | n/a | loaded.available = False |
---|
361 | n/a | reloaded = self.bootstrap._exec(self.spec, loaded) |
---|
362 | n/a | |
---|
363 | n/a | self.assertFalse(loaded.available) |
---|
364 | n/a | self.assertIs(reloaded, loaded) |
---|
365 | n/a | |
---|
366 | n/a | def test_reload_init_module_attrs(self): |
---|
367 | n/a | self.spec.loader = NewLoader() |
---|
368 | n/a | with CleanImport(self.spec.name): |
---|
369 | n/a | loaded = self.bootstrap._load(self.spec) |
---|
370 | n/a | loaded.__name__ = 'ham' |
---|
371 | n/a | del loaded.__loader__ |
---|
372 | n/a | del loaded.__package__ |
---|
373 | n/a | del loaded.__spec__ |
---|
374 | n/a | self.bootstrap._exec(self.spec, loaded) |
---|
375 | n/a | |
---|
376 | n/a | self.assertEqual(loaded.__name__, self.spec.name) |
---|
377 | n/a | self.assertIs(loaded.__loader__, self.spec.loader) |
---|
378 | n/a | self.assertEqual(loaded.__package__, self.spec.parent) |
---|
379 | n/a | self.assertIs(loaded.__spec__, self.spec) |
---|
380 | n/a | self.assertFalse(hasattr(loaded, '__path__')) |
---|
381 | n/a | self.assertFalse(hasattr(loaded, '__file__')) |
---|
382 | n/a | self.assertFalse(hasattr(loaded, '__cached__')) |
---|
383 | n/a | |
---|
384 | n/a | def test_reload_legacy(self): |
---|
385 | n/a | self.spec.loader = LegacyLoader() |
---|
386 | n/a | with CleanImport(self.spec.name): |
---|
387 | n/a | loaded = self.bootstrap._load(self.spec) |
---|
388 | n/a | reloaded = self.bootstrap._exec(self.spec, loaded) |
---|
389 | n/a | installed = sys.modules[self.spec.name] |
---|
390 | n/a | |
---|
391 | n/a | self.assertEqual(loaded.ham, -1) |
---|
392 | n/a | self.assertIs(reloaded, loaded) |
---|
393 | n/a | self.assertIs(installed, loaded) |
---|
394 | n/a | |
---|
395 | n/a | |
---|
396 | n/a | (Frozen_ModuleSpecMethodsTests, |
---|
397 | n/a | Source_ModuleSpecMethodsTests |
---|
398 | n/a | ) = test_util.test_both(ModuleSpecMethodsTests, init=init, util=util, |
---|
399 | n/a | machinery=machinery) |
---|
400 | n/a | |
---|
401 | n/a | |
---|
402 | n/a | class ModuleReprTests: |
---|
403 | n/a | |
---|
404 | n/a | @property |
---|
405 | n/a | def bootstrap(self): |
---|
406 | n/a | return self.init._bootstrap |
---|
407 | n/a | |
---|
408 | n/a | def setUp(self): |
---|
409 | n/a | self.module = type(os)('spam') |
---|
410 | n/a | self.spec = self.machinery.ModuleSpec('spam', TestLoader()) |
---|
411 | n/a | |
---|
412 | n/a | def test_module___loader___module_repr(self): |
---|
413 | n/a | class Loader: |
---|
414 | n/a | def module_repr(self, module): |
---|
415 | n/a | return '<delicious {}>'.format(module.__name__) |
---|
416 | n/a | self.module.__loader__ = Loader() |
---|
417 | n/a | modrepr = self.bootstrap._module_repr(self.module) |
---|
418 | n/a | |
---|
419 | n/a | self.assertEqual(modrepr, '<delicious spam>') |
---|
420 | n/a | |
---|
421 | n/a | def test_module___loader___module_repr_bad(self): |
---|
422 | n/a | class Loader(TestLoader): |
---|
423 | n/a | def module_repr(self, module): |
---|
424 | n/a | raise Exception |
---|
425 | n/a | self.module.__loader__ = Loader() |
---|
426 | n/a | modrepr = self.bootstrap._module_repr(self.module) |
---|
427 | n/a | |
---|
428 | n/a | self.assertEqual(modrepr, |
---|
429 | n/a | '<module {!r} (<TestLoader object>)>'.format('spam')) |
---|
430 | n/a | |
---|
431 | n/a | def test_module___spec__(self): |
---|
432 | n/a | origin = 'in a hole, in the ground' |
---|
433 | n/a | self.spec.origin = origin |
---|
434 | n/a | self.module.__spec__ = self.spec |
---|
435 | n/a | modrepr = self.bootstrap._module_repr(self.module) |
---|
436 | n/a | |
---|
437 | n/a | self.assertEqual(modrepr, '<module {!r} ({})>'.format('spam', origin)) |
---|
438 | n/a | |
---|
439 | n/a | def test_module___spec___location(self): |
---|
440 | n/a | location = 'in_a_galaxy_far_far_away.py' |
---|
441 | n/a | self.spec.origin = location |
---|
442 | n/a | self.spec._set_fileattr = True |
---|
443 | n/a | self.module.__spec__ = self.spec |
---|
444 | n/a | modrepr = self.bootstrap._module_repr(self.module) |
---|
445 | n/a | |
---|
446 | n/a | self.assertEqual(modrepr, |
---|
447 | n/a | '<module {!r} from {!r}>'.format('spam', location)) |
---|
448 | n/a | |
---|
449 | n/a | def test_module___spec___no_origin(self): |
---|
450 | n/a | self.spec.loader = TestLoader() |
---|
451 | n/a | self.module.__spec__ = self.spec |
---|
452 | n/a | modrepr = self.bootstrap._module_repr(self.module) |
---|
453 | n/a | |
---|
454 | n/a | self.assertEqual(modrepr, |
---|
455 | n/a | '<module {!r} (<TestLoader object>)>'.format('spam')) |
---|
456 | n/a | |
---|
457 | n/a | def test_module___spec___no_origin_no_loader(self): |
---|
458 | n/a | self.spec.loader = None |
---|
459 | n/a | self.module.__spec__ = self.spec |
---|
460 | n/a | modrepr = self.bootstrap._module_repr(self.module) |
---|
461 | n/a | |
---|
462 | n/a | self.assertEqual(modrepr, '<module {!r}>'.format('spam')) |
---|
463 | n/a | |
---|
464 | n/a | def test_module_no_name(self): |
---|
465 | n/a | del self.module.__name__ |
---|
466 | n/a | modrepr = self.bootstrap._module_repr(self.module) |
---|
467 | n/a | |
---|
468 | n/a | self.assertEqual(modrepr, '<module {!r}>'.format('?')) |
---|
469 | n/a | |
---|
470 | n/a | def test_module_with_file(self): |
---|
471 | n/a | filename = 'e/i/e/i/o/spam.py' |
---|
472 | n/a | self.module.__file__ = filename |
---|
473 | n/a | modrepr = self.bootstrap._module_repr(self.module) |
---|
474 | n/a | |
---|
475 | n/a | self.assertEqual(modrepr, |
---|
476 | n/a | '<module {!r} from {!r}>'.format('spam', filename)) |
---|
477 | n/a | |
---|
478 | n/a | def test_module_no_file(self): |
---|
479 | n/a | self.module.__loader__ = TestLoader() |
---|
480 | n/a | modrepr = self.bootstrap._module_repr(self.module) |
---|
481 | n/a | |
---|
482 | n/a | self.assertEqual(modrepr, |
---|
483 | n/a | '<module {!r} (<TestLoader object>)>'.format('spam')) |
---|
484 | n/a | |
---|
485 | n/a | def test_module_no_file_no_loader(self): |
---|
486 | n/a | modrepr = self.bootstrap._module_repr(self.module) |
---|
487 | n/a | |
---|
488 | n/a | self.assertEqual(modrepr, '<module {!r}>'.format('spam')) |
---|
489 | n/a | |
---|
490 | n/a | |
---|
491 | n/a | (Frozen_ModuleReprTests, |
---|
492 | n/a | Source_ModuleReprTests |
---|
493 | n/a | ) = test_util.test_both(ModuleReprTests, init=init, util=util, |
---|
494 | n/a | machinery=machinery) |
---|
495 | n/a | |
---|
496 | n/a | |
---|
497 | n/a | class FactoryTests: |
---|
498 | n/a | |
---|
499 | n/a | def setUp(self): |
---|
500 | n/a | self.name = 'spam' |
---|
501 | n/a | self.path = 'spam.py' |
---|
502 | n/a | self.cached = self.util.cache_from_source(self.path) |
---|
503 | n/a | self.loader = TestLoader() |
---|
504 | n/a | self.fileloader = TestLoader(self.path) |
---|
505 | n/a | self.pkgloader = TestLoader(self.path, True) |
---|
506 | n/a | |
---|
507 | n/a | # spec_from_loader() |
---|
508 | n/a | |
---|
509 | n/a | def test_spec_from_loader_default(self): |
---|
510 | n/a | spec = self.util.spec_from_loader(self.name, self.loader) |
---|
511 | n/a | |
---|
512 | n/a | self.assertEqual(spec.name, self.name) |
---|
513 | n/a | self.assertEqual(spec.loader, self.loader) |
---|
514 | n/a | self.assertIs(spec.origin, None) |
---|
515 | n/a | self.assertIs(spec.loader_state, None) |
---|
516 | n/a | self.assertIs(spec.submodule_search_locations, None) |
---|
517 | n/a | self.assertIs(spec.cached, None) |
---|
518 | n/a | self.assertFalse(spec.has_location) |
---|
519 | n/a | |
---|
520 | n/a | def test_spec_from_loader_default_with_bad_is_package(self): |
---|
521 | n/a | class Loader: |
---|
522 | n/a | def is_package(self, name): |
---|
523 | n/a | raise ImportError |
---|
524 | n/a | loader = Loader() |
---|
525 | n/a | spec = self.util.spec_from_loader(self.name, loader) |
---|
526 | n/a | |
---|
527 | n/a | self.assertEqual(spec.name, self.name) |
---|
528 | n/a | self.assertEqual(spec.loader, loader) |
---|
529 | n/a | self.assertIs(spec.origin, None) |
---|
530 | n/a | self.assertIs(spec.loader_state, None) |
---|
531 | n/a | self.assertIs(spec.submodule_search_locations, None) |
---|
532 | n/a | self.assertIs(spec.cached, None) |
---|
533 | n/a | self.assertFalse(spec.has_location) |
---|
534 | n/a | |
---|
535 | n/a | def test_spec_from_loader_origin(self): |
---|
536 | n/a | origin = 'somewhere over the rainbow' |
---|
537 | n/a | spec = self.util.spec_from_loader(self.name, self.loader, |
---|
538 | n/a | origin=origin) |
---|
539 | n/a | |
---|
540 | n/a | self.assertEqual(spec.name, self.name) |
---|
541 | n/a | self.assertEqual(spec.loader, self.loader) |
---|
542 | n/a | self.assertIs(spec.origin, origin) |
---|
543 | n/a | self.assertIs(spec.loader_state, None) |
---|
544 | n/a | self.assertIs(spec.submodule_search_locations, None) |
---|
545 | n/a | self.assertIs(spec.cached, None) |
---|
546 | n/a | self.assertFalse(spec.has_location) |
---|
547 | n/a | |
---|
548 | n/a | def test_spec_from_loader_is_package_false(self): |
---|
549 | n/a | spec = self.util.spec_from_loader(self.name, self.loader, |
---|
550 | n/a | is_package=False) |
---|
551 | n/a | |
---|
552 | n/a | self.assertEqual(spec.name, self.name) |
---|
553 | n/a | self.assertEqual(spec.loader, self.loader) |
---|
554 | n/a | self.assertIs(spec.origin, None) |
---|
555 | n/a | self.assertIs(spec.loader_state, None) |
---|
556 | n/a | self.assertIs(spec.submodule_search_locations, None) |
---|
557 | n/a | self.assertIs(spec.cached, None) |
---|
558 | n/a | self.assertFalse(spec.has_location) |
---|
559 | n/a | |
---|
560 | n/a | def test_spec_from_loader_is_package_true(self): |
---|
561 | n/a | spec = self.util.spec_from_loader(self.name, self.loader, |
---|
562 | n/a | is_package=True) |
---|
563 | n/a | |
---|
564 | n/a | self.assertEqual(spec.name, self.name) |
---|
565 | n/a | self.assertEqual(spec.loader, self.loader) |
---|
566 | n/a | self.assertIs(spec.origin, None) |
---|
567 | n/a | self.assertIs(spec.loader_state, None) |
---|
568 | n/a | self.assertEqual(spec.submodule_search_locations, []) |
---|
569 | n/a | self.assertIs(spec.cached, None) |
---|
570 | n/a | self.assertFalse(spec.has_location) |
---|
571 | n/a | |
---|
572 | n/a | def test_spec_from_loader_origin_and_is_package(self): |
---|
573 | n/a | origin = 'where the streets have no name' |
---|
574 | n/a | spec = self.util.spec_from_loader(self.name, self.loader, |
---|
575 | n/a | origin=origin, is_package=True) |
---|
576 | n/a | |
---|
577 | n/a | self.assertEqual(spec.name, self.name) |
---|
578 | n/a | self.assertEqual(spec.loader, self.loader) |
---|
579 | n/a | self.assertIs(spec.origin, origin) |
---|
580 | n/a | self.assertIs(spec.loader_state, None) |
---|
581 | n/a | self.assertEqual(spec.submodule_search_locations, []) |
---|
582 | n/a | self.assertIs(spec.cached, None) |
---|
583 | n/a | self.assertFalse(spec.has_location) |
---|
584 | n/a | |
---|
585 | n/a | def test_spec_from_loader_is_package_with_loader_false(self): |
---|
586 | n/a | loader = TestLoader(is_package=False) |
---|
587 | n/a | spec = self.util.spec_from_loader(self.name, loader) |
---|
588 | n/a | |
---|
589 | n/a | self.assertEqual(spec.name, self.name) |
---|
590 | n/a | self.assertEqual(spec.loader, loader) |
---|
591 | n/a | self.assertIs(spec.origin, None) |
---|
592 | n/a | self.assertIs(spec.loader_state, None) |
---|
593 | n/a | self.assertIs(spec.submodule_search_locations, None) |
---|
594 | n/a | self.assertIs(spec.cached, None) |
---|
595 | n/a | self.assertFalse(spec.has_location) |
---|
596 | n/a | |
---|
597 | n/a | def test_spec_from_loader_is_package_with_loader_true(self): |
---|
598 | n/a | loader = TestLoader(is_package=True) |
---|
599 | n/a | spec = self.util.spec_from_loader(self.name, loader) |
---|
600 | n/a | |
---|
601 | n/a | self.assertEqual(spec.name, self.name) |
---|
602 | n/a | self.assertEqual(spec.loader, loader) |
---|
603 | n/a | self.assertIs(spec.origin, None) |
---|
604 | n/a | self.assertIs(spec.loader_state, None) |
---|
605 | n/a | self.assertEqual(spec.submodule_search_locations, []) |
---|
606 | n/a | self.assertIs(spec.cached, None) |
---|
607 | n/a | self.assertFalse(spec.has_location) |
---|
608 | n/a | |
---|
609 | n/a | def test_spec_from_loader_default_with_file_loader(self): |
---|
610 | n/a | spec = self.util.spec_from_loader(self.name, self.fileloader) |
---|
611 | n/a | |
---|
612 | n/a | self.assertEqual(spec.name, self.name) |
---|
613 | n/a | self.assertEqual(spec.loader, self.fileloader) |
---|
614 | n/a | self.assertEqual(spec.origin, self.path) |
---|
615 | n/a | self.assertIs(spec.loader_state, None) |
---|
616 | n/a | self.assertIs(spec.submodule_search_locations, None) |
---|
617 | n/a | self.assertEqual(spec.cached, self.cached) |
---|
618 | n/a | self.assertTrue(spec.has_location) |
---|
619 | n/a | |
---|
620 | n/a | def test_spec_from_loader_is_package_false_with_fileloader(self): |
---|
621 | n/a | spec = self.util.spec_from_loader(self.name, self.fileloader, |
---|
622 | n/a | is_package=False) |
---|
623 | n/a | |
---|
624 | n/a | self.assertEqual(spec.name, self.name) |
---|
625 | n/a | self.assertEqual(spec.loader, self.fileloader) |
---|
626 | n/a | self.assertEqual(spec.origin, self.path) |
---|
627 | n/a | self.assertIs(spec.loader_state, None) |
---|
628 | n/a | self.assertIs(spec.submodule_search_locations, None) |
---|
629 | n/a | self.assertEqual(spec.cached, self.cached) |
---|
630 | n/a | self.assertTrue(spec.has_location) |
---|
631 | n/a | |
---|
632 | n/a | def test_spec_from_loader_is_package_true_with_fileloader(self): |
---|
633 | n/a | spec = self.util.spec_from_loader(self.name, self.fileloader, |
---|
634 | n/a | is_package=True) |
---|
635 | n/a | |
---|
636 | n/a | self.assertEqual(spec.name, self.name) |
---|
637 | n/a | self.assertEqual(spec.loader, self.fileloader) |
---|
638 | n/a | self.assertEqual(spec.origin, self.path) |
---|
639 | n/a | self.assertIs(spec.loader_state, None) |
---|
640 | n/a | self.assertEqual(spec.submodule_search_locations, ['']) |
---|
641 | n/a | self.assertEqual(spec.cached, self.cached) |
---|
642 | n/a | self.assertTrue(spec.has_location) |
---|
643 | n/a | |
---|
644 | n/a | # spec_from_file_location() |
---|
645 | n/a | |
---|
646 | n/a | def test_spec_from_file_location_default(self): |
---|
647 | n/a | spec = self.util.spec_from_file_location(self.name, self.path) |
---|
648 | n/a | |
---|
649 | n/a | self.assertEqual(spec.name, self.name) |
---|
650 | n/a | # Need to use a circuitous route to get at importlib.machinery to make |
---|
651 | n/a | # sure the same class object is used in the isinstance() check as |
---|
652 | n/a | # would have been used to create the loader. |
---|
653 | n/a | self.assertIsInstance(spec.loader, |
---|
654 | n/a | self.util.abc.machinery.SourceFileLoader) |
---|
655 | n/a | self.assertEqual(spec.loader.name, self.name) |
---|
656 | n/a | self.assertEqual(spec.loader.path, self.path) |
---|
657 | n/a | self.assertEqual(spec.origin, self.path) |
---|
658 | n/a | self.assertIs(spec.loader_state, None) |
---|
659 | n/a | self.assertIs(spec.submodule_search_locations, None) |
---|
660 | n/a | self.assertEqual(spec.cached, self.cached) |
---|
661 | n/a | self.assertTrue(spec.has_location) |
---|
662 | n/a | |
---|
663 | n/a | def test_spec_from_file_location_path_like_arg(self): |
---|
664 | n/a | spec = self.util.spec_from_file_location(self.name, |
---|
665 | n/a | pathlib.PurePath(self.path)) |
---|
666 | n/a | self.assertEqual(spec.origin, self.path) |
---|
667 | n/a | |
---|
668 | n/a | def test_spec_from_file_location_default_without_location(self): |
---|
669 | n/a | spec = self.util.spec_from_file_location(self.name) |
---|
670 | n/a | |
---|
671 | n/a | self.assertIs(spec, None) |
---|
672 | n/a | |
---|
673 | n/a | def test_spec_from_file_location_default_bad_suffix(self): |
---|
674 | n/a | spec = self.util.spec_from_file_location(self.name, 'spam.eggs') |
---|
675 | n/a | |
---|
676 | n/a | self.assertIs(spec, None) |
---|
677 | n/a | |
---|
678 | n/a | def test_spec_from_file_location_loader_no_location(self): |
---|
679 | n/a | spec = self.util.spec_from_file_location(self.name, |
---|
680 | n/a | loader=self.fileloader) |
---|
681 | n/a | |
---|
682 | n/a | self.assertEqual(spec.name, self.name) |
---|
683 | n/a | self.assertEqual(spec.loader, self.fileloader) |
---|
684 | n/a | self.assertEqual(spec.origin, self.path) |
---|
685 | n/a | self.assertIs(spec.loader_state, None) |
---|
686 | n/a | self.assertIs(spec.submodule_search_locations, None) |
---|
687 | n/a | self.assertEqual(spec.cached, self.cached) |
---|
688 | n/a | self.assertTrue(spec.has_location) |
---|
689 | n/a | |
---|
690 | n/a | def test_spec_from_file_location_loader_no_location_no_get_filename(self): |
---|
691 | n/a | spec = self.util.spec_from_file_location(self.name, |
---|
692 | n/a | loader=self.loader) |
---|
693 | n/a | |
---|
694 | n/a | self.assertEqual(spec.name, self.name) |
---|
695 | n/a | self.assertEqual(spec.loader, self.loader) |
---|
696 | n/a | self.assertEqual(spec.origin, '<unknown>') |
---|
697 | n/a | self.assertIs(spec.loader_state, None) |
---|
698 | n/a | self.assertIs(spec.submodule_search_locations, None) |
---|
699 | n/a | self.assertIs(spec.cached, None) |
---|
700 | n/a | self.assertTrue(spec.has_location) |
---|
701 | n/a | |
---|
702 | n/a | def test_spec_from_file_location_loader_no_location_bad_get_filename(self): |
---|
703 | n/a | class Loader: |
---|
704 | n/a | def get_filename(self, name): |
---|
705 | n/a | raise ImportError |
---|
706 | n/a | loader = Loader() |
---|
707 | n/a | spec = self.util.spec_from_file_location(self.name, loader=loader) |
---|
708 | n/a | |
---|
709 | n/a | self.assertEqual(spec.name, self.name) |
---|
710 | n/a | self.assertEqual(spec.loader, loader) |
---|
711 | n/a | self.assertEqual(spec.origin, '<unknown>') |
---|
712 | n/a | self.assertIs(spec.loader_state, None) |
---|
713 | n/a | self.assertIs(spec.submodule_search_locations, None) |
---|
714 | n/a | self.assertIs(spec.cached, None) |
---|
715 | n/a | self.assertTrue(spec.has_location) |
---|
716 | n/a | |
---|
717 | n/a | def test_spec_from_file_location_smsl_none(self): |
---|
718 | n/a | spec = self.util.spec_from_file_location(self.name, self.path, |
---|
719 | n/a | loader=self.fileloader, |
---|
720 | n/a | submodule_search_locations=None) |
---|
721 | n/a | |
---|
722 | n/a | self.assertEqual(spec.name, self.name) |
---|
723 | n/a | self.assertEqual(spec.loader, self.fileloader) |
---|
724 | n/a | self.assertEqual(spec.origin, self.path) |
---|
725 | n/a | self.assertIs(spec.loader_state, None) |
---|
726 | n/a | self.assertIs(spec.submodule_search_locations, None) |
---|
727 | n/a | self.assertEqual(spec.cached, self.cached) |
---|
728 | n/a | self.assertTrue(spec.has_location) |
---|
729 | n/a | |
---|
730 | n/a | def test_spec_from_file_location_smsl_empty(self): |
---|
731 | n/a | spec = self.util.spec_from_file_location(self.name, self.path, |
---|
732 | n/a | loader=self.fileloader, |
---|
733 | n/a | submodule_search_locations=[]) |
---|
734 | n/a | |
---|
735 | n/a | self.assertEqual(spec.name, self.name) |
---|
736 | n/a | self.assertEqual(spec.loader, self.fileloader) |
---|
737 | n/a | self.assertEqual(spec.origin, self.path) |
---|
738 | n/a | self.assertIs(spec.loader_state, None) |
---|
739 | n/a | self.assertEqual(spec.submodule_search_locations, ['']) |
---|
740 | n/a | self.assertEqual(spec.cached, self.cached) |
---|
741 | n/a | self.assertTrue(spec.has_location) |
---|
742 | n/a | |
---|
743 | n/a | def test_spec_from_file_location_smsl_not_empty(self): |
---|
744 | n/a | spec = self.util.spec_from_file_location(self.name, self.path, |
---|
745 | n/a | loader=self.fileloader, |
---|
746 | n/a | submodule_search_locations=['eggs']) |
---|
747 | n/a | |
---|
748 | n/a | self.assertEqual(spec.name, self.name) |
---|
749 | n/a | self.assertEqual(spec.loader, self.fileloader) |
---|
750 | n/a | self.assertEqual(spec.origin, self.path) |
---|
751 | n/a | self.assertIs(spec.loader_state, None) |
---|
752 | n/a | self.assertEqual(spec.submodule_search_locations, ['eggs']) |
---|
753 | n/a | self.assertEqual(spec.cached, self.cached) |
---|
754 | n/a | self.assertTrue(spec.has_location) |
---|
755 | n/a | |
---|
756 | n/a | def test_spec_from_file_location_smsl_default(self): |
---|
757 | n/a | spec = self.util.spec_from_file_location(self.name, self.path, |
---|
758 | n/a | loader=self.pkgloader) |
---|
759 | n/a | |
---|
760 | n/a | self.assertEqual(spec.name, self.name) |
---|
761 | n/a | self.assertEqual(spec.loader, self.pkgloader) |
---|
762 | n/a | self.assertEqual(spec.origin, self.path) |
---|
763 | n/a | self.assertIs(spec.loader_state, None) |
---|
764 | n/a | self.assertEqual(spec.submodule_search_locations, ['']) |
---|
765 | n/a | self.assertEqual(spec.cached, self.cached) |
---|
766 | n/a | self.assertTrue(spec.has_location) |
---|
767 | n/a | |
---|
768 | n/a | def test_spec_from_file_location_smsl_default_not_package(self): |
---|
769 | n/a | class Loader: |
---|
770 | n/a | def is_package(self, name): |
---|
771 | n/a | return False |
---|
772 | n/a | loader = Loader() |
---|
773 | n/a | spec = self.util.spec_from_file_location(self.name, self.path, |
---|
774 | n/a | loader=loader) |
---|
775 | n/a | |
---|
776 | n/a | self.assertEqual(spec.name, self.name) |
---|
777 | n/a | self.assertEqual(spec.loader, loader) |
---|
778 | n/a | self.assertEqual(spec.origin, self.path) |
---|
779 | n/a | self.assertIs(spec.loader_state, None) |
---|
780 | n/a | self.assertIs(spec.submodule_search_locations, None) |
---|
781 | n/a | self.assertEqual(spec.cached, self.cached) |
---|
782 | n/a | self.assertTrue(spec.has_location) |
---|
783 | n/a | |
---|
784 | n/a | def test_spec_from_file_location_smsl_default_no_is_package(self): |
---|
785 | n/a | spec = self.util.spec_from_file_location(self.name, self.path, |
---|
786 | n/a | loader=self.fileloader) |
---|
787 | n/a | |
---|
788 | n/a | self.assertEqual(spec.name, self.name) |
---|
789 | n/a | self.assertEqual(spec.loader, self.fileloader) |
---|
790 | n/a | self.assertEqual(spec.origin, self.path) |
---|
791 | n/a | self.assertIs(spec.loader_state, None) |
---|
792 | n/a | self.assertIs(spec.submodule_search_locations, None) |
---|
793 | n/a | self.assertEqual(spec.cached, self.cached) |
---|
794 | n/a | self.assertTrue(spec.has_location) |
---|
795 | n/a | |
---|
796 | n/a | def test_spec_from_file_location_smsl_default_bad_is_package(self): |
---|
797 | n/a | class Loader: |
---|
798 | n/a | def is_package(self, name): |
---|
799 | n/a | raise ImportError |
---|
800 | n/a | loader = Loader() |
---|
801 | n/a | spec = self.util.spec_from_file_location(self.name, self.path, |
---|
802 | n/a | loader=loader) |
---|
803 | n/a | |
---|
804 | n/a | self.assertEqual(spec.name, self.name) |
---|
805 | n/a | self.assertEqual(spec.loader, loader) |
---|
806 | n/a | self.assertEqual(spec.origin, self.path) |
---|
807 | n/a | self.assertIs(spec.loader_state, None) |
---|
808 | n/a | self.assertIs(spec.submodule_search_locations, None) |
---|
809 | n/a | self.assertEqual(spec.cached, self.cached) |
---|
810 | n/a | self.assertTrue(spec.has_location) |
---|
811 | n/a | |
---|
812 | n/a | |
---|
813 | n/a | (Frozen_FactoryTests, |
---|
814 | n/a | Source_FactoryTests |
---|
815 | n/a | ) = test_util.test_both(FactoryTests, util=util, machinery=machinery) |
---|
816 | n/a | |
---|
817 | n/a | |
---|
818 | n/a | if __name__ == '__main__': |
---|
819 | n/a | unittest.main() |
---|