Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(95851)

Delta Between Two Patch Sets: Lib/test/test_importlib/test_spec.py

Issue 18864: Implementation for PEP 451 (importlib.machinery.ModuleSpec)
Left Patch Set: Created 5 years, 10 months ago
Right Patch Set: Created 5 years, 8 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « Lib/importlib/_bootstrap.py ('k') | Python/importlib.h » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 from . import util
2
3 frozen_init, source_init = util.import_importlib('importlib')
4 frozen_bootstrap = frozen_init._bootstrap
5 source_bootstrap = source_init._bootstrap
6 frozen_machinery, source_machinery = util.import_importlib('importlib.machinery' )
7 frozen_util, source_util = util.import_importlib('importlib.util')
8
9 import os.path
10 from test.support import CleanImport
1 import unittest 11 import unittest
2 import sys 12 import sys
3 13 import warnings
4 14
5 class ModuleSpecTests(unittest.TestCase): 15
6 16
7 def test_spec_on_all_modules(self): 17 class TestLoader:
8 for name, module in sys.modules.items(): 18
9 with self.subTest(name=name): 19 def __init__(self, path=None, is_package=None):
10 spec = module.__spec__ 20 # if path:
11 self.assertIsNot(spec, None) 21 # if is_package:
22 # if not path.endswith('.py'):
23 # path = os.path.join(path, '__init__.py')
24 # elif is_package is None:
25 # is_package = path.endswith('__init__.py')
26
27 self.path = path
28 self.package = is_package
29
30 def __repr__(self):
31 return '<TestLoader object>'
32
33 def __getattr__(self, name):
34 if name == 'get_filename' and self.path is not None:
35 return self._get_filename
36 if name == 'is_package':
37 return self._is_package
38 raise AttributeError(name)
39
40 def _get_filename(self, name):
41 return self.path
42
43 def _is_package(self, name):
44 return self.package
45
46
47 class NewLoader(TestLoader):
48
49 EGGS = 1
50
51 def exec_module(self, module):
52 module.eggs = self.EGGS
53
54
55 class LegacyLoader(TestLoader):
56
57 HAM = -1
58
59 with warnings.catch_warnings():
60 warnings.simplefilter("ignore", PendingDeprecationWarning)
61
62 @frozen_util.module_for_loader
63 def load_module(self, module):
64 module.ham = self.HAM
65 return module
66
67
68 class ModuleSpecTests:
69
70 def setUp(self):
71 self.name = 'spam'
72 self.path = 'spam.py'
73 self.cached = self.util.cache_from_source(self.path)
74 self.loader = TestLoader()
75 self.spec = self.machinery.ModuleSpec(self.name, self.loader)
76 self.loc_spec = self.machinery.ModuleSpec(self.name, self.loader,
77 origin=self.path)
78 self.loc_spec._set_fileattr = True
79
80 def test_default(self):
81 spec = self.machinery.ModuleSpec(self.name, self.loader)
82
83 self.assertEqual(spec.name, self.name)
84 self.assertEqual(spec.loader, self.loader)
85 self.assertIs(spec.origin, None)
86 self.assertIs(spec.loader_state, None)
87 self.assertIs(spec.submodule_search_locations, None)
88 self.assertIs(spec.cached, None)
89 self.assertFalse(spec.has_location)
90
91 def test_default_no_loader(self):
92 spec = self.machinery.ModuleSpec(self.name, None)
93
94 self.assertEqual(spec.name, self.name)
95 self.assertIs(spec.loader, None)
96 self.assertIs(spec.origin, None)
97 self.assertIs(spec.loader_state, None)
98 self.assertIs(spec.submodule_search_locations, None)
99 self.assertIs(spec.cached, None)
100 self.assertFalse(spec.has_location)
101
102 def test_default_is_package_false(self):
103 spec = self.machinery.ModuleSpec(self.name, self.loader,
104 is_package=False)
105
106 self.assertEqual(spec.name, self.name)
107 self.assertEqual(spec.loader, self.loader)
108 self.assertIs(spec.origin, None)
109 self.assertIs(spec.loader_state, None)
110 self.assertIs(spec.submodule_search_locations, None)
111 self.assertIs(spec.cached, None)
112 self.assertFalse(spec.has_location)
113
114 def test_default_is_package_true(self):
115 spec = self.machinery.ModuleSpec(self.name, self.loader,
116 is_package=True)
117
118 self.assertEqual(spec.name, self.name)
119 self.assertEqual(spec.loader, self.loader)
120 self.assertIs(spec.origin, None)
121 self.assertIs(spec.loader_state, None)
122 self.assertEqual(spec.submodule_search_locations, [])
123 self.assertIs(spec.cached, None)
124 self.assertFalse(spec.has_location)
125
126 def test_has_location_setter(self):
127 spec = self.machinery.ModuleSpec(self.name, self.loader,
128 origin='somewhere')
129 self.assertFalse(spec.has_location)
130 spec.has_location = True
131 self.assertTrue(spec.has_location)
132
133 def test_equality(self):
134 other = type(sys.implementation)(name=self.name,
135 loader=self.loader,
136 origin=None,
137 submodule_search_locations=None,
138 has_location=False,
139 cached=None,
140 )
141
142 self.assertTrue(self.spec == other)
143
144 def test_equality_location(self):
145 other = type(sys.implementation)(name=self.name,
146 loader=self.loader,
147 origin=self.path,
148 submodule_search_locations=None,
149 has_location=True,
150 cached=self.cached,
151 )
152
153 self.assertEqual(self.loc_spec, other)
154
155 def test_inequality(self):
156 other = type(sys.implementation)(name='ham',
157 loader=self.loader,
158 origin=None,
159 submodule_search_locations=None,
160 has_location=False,
161 cached=None,
162 )
163
164 self.assertNotEqual(self.spec, other)
165
166 def test_inequality_incomplete(self):
167 other = type(sys.implementation)(name=self.name,
168 loader=self.loader,
169 )
170
171 self.assertNotEqual(self.spec, other)
172
173 def test_package(self):
174 spec = self.machinery.ModuleSpec('spam.eggs', self.loader)
175
176 self.assertEqual(spec.parent, 'spam')
177
178 def test_package_is_package(self):
179 spec = self.machinery.ModuleSpec('spam.eggs', self.loader,
180 is_package=True)
181
182 self.assertEqual(spec.parent, 'spam.eggs')
183
184 # cached
185
186 def test_cached_set(self):
187 before = self.spec.cached
188 self.spec.cached = 'there'
189 after = self.spec.cached
190
191 self.assertIs(before, None)
192 self.assertEqual(after, 'there')
193
194 def test_cached_no_origin(self):
195 spec = self.machinery.ModuleSpec(self.name, self.loader)
196
197 self.assertIs(spec.cached, None)
198
199 def test_cached_with_origin_not_location(self):
200 spec = self.machinery.ModuleSpec(self.name, self.loader,
201 origin=self.path)
202
203 self.assertIs(spec.cached, None)
204
205 def test_cached_source(self):
206 expected = self.util.cache_from_source(self.path)
207
208 self.assertEqual(self.loc_spec.cached, expected)
209
210 def test_cached_source_unknown_suffix(self):
211 self.loc_spec.origin = 'spam.spamspamspam'
212
213 self.assertIs(self.loc_spec.cached, None)
214
215 def test_cached_source_missing_cache_tag(self):
216 original = sys.implementation.cache_tag
217 sys.implementation.cache_tag = None
218 try:
219 cached = self.loc_spec.cached
220 finally:
221 sys.implementation.cache_tag = original
222
223 self.assertIs(cached, None)
224
225 def test_cached_sourceless(self):
226 self.loc_spec.origin = 'spam.pyc'
227
228 self.assertEqual(self.loc_spec.cached, 'spam.pyc')
229
230
231 class Frozen_ModuleSpecTests(ModuleSpecTests, unittest.TestCase):
232 util = frozen_util
233 machinery = frozen_machinery
234
235
236 class Source_ModuleSpecTests(ModuleSpecTests, unittest.TestCase):
237 util = source_util
238 machinery = source_machinery
239
240
241 class ModuleSpecMethodsTests:
242
243 def setUp(self):
244 self.name = 'spam'
245 self.path = 'spam.py'
246 self.cached = self.util.cache_from_source(self.path)
247 self.loader = TestLoader()
248 self.spec = self.machinery.ModuleSpec(self.name, self.loader)
249 self.loc_spec = self.machinery.ModuleSpec(self.name, self.loader,
250 origin=self.path)
251 self.loc_spec._set_fileattr = True
252
253 # init_module_attrs
254
255 def test_init_module_attrs(self):
256 module = type(sys)(self.name)
257 spec = self.machinery.ModuleSpec(self.name, self.loader)
258 self.bootstrap._SpecMethods(spec).init_module_attrs(module)
259
260 self.assertEqual(module.__name__, spec.name)
261 self.assertIs(module.__loader__, spec.loader)
262 self.assertEqual(module.__package__, spec.parent)
263 self.assertIs(module.__spec__, spec)
264 self.assertFalse(hasattr(module, '__path__'))
265 self.assertFalse(hasattr(module, '__file__'))
266 self.assertFalse(hasattr(module, '__cached__'))
267
268 def test_init_module_attrs_package(self):
269 module = type(sys)(self.name)
270 spec = self.machinery.ModuleSpec(self.name, self.loader)
271 spec.submodule_search_locations = ['spam', 'ham']
272 self.bootstrap._SpecMethods(spec).init_module_attrs(module)
273
274 self.assertEqual(module.__name__, spec.name)
275 self.assertIs(module.__loader__, spec.loader)
276 self.assertEqual(module.__package__, spec.parent)
277 self.assertIs(module.__spec__, spec)
278 self.assertIs(module.__path__, spec.submodule_search_locations)
279 self.assertFalse(hasattr(module, '__file__'))
280 self.assertFalse(hasattr(module, '__cached__'))
281
282 def test_init_module_attrs_location(self):
283 module = type(sys)(self.name)
284 spec = self.loc_spec
285 self.bootstrap._SpecMethods(spec).init_module_attrs(module)
286
287 self.assertEqual(module.__name__, spec.name)
288 self.assertIs(module.__loader__, spec.loader)
289 self.assertEqual(module.__package__, spec.parent)
290 self.assertIs(module.__spec__, spec)
291 self.assertFalse(hasattr(module, '__path__'))
292 self.assertEqual(module.__file__, spec.origin)
293 self.assertEqual(module.__cached__,
294 self.util.cache_from_source(spec.origin))
295
296 def test_init_module_attrs_different_name(self):
297 module = type(sys)('eggs')
298 spec = self.machinery.ModuleSpec(self.name, self.loader)
299 self.bootstrap._SpecMethods(spec).init_module_attrs(module)
300
301 self.assertEqual(module.__name__, spec.name)
302
303 def test_init_module_attrs_different_spec(self):
304 module = type(sys)(self.name)
305 module.__spec__ = self.machinery.ModuleSpec('eggs', object())
306 spec = self.machinery.ModuleSpec(self.name, self.loader)
307 self.bootstrap._SpecMethods(spec).init_module_attrs(module)
308
309 self.assertEqual(module.__name__, spec.name)
310 self.assertIs(module.__loader__, spec.loader)
311 self.assertEqual(module.__package__, spec.parent)
312 self.assertIs(module.__spec__, spec)
313
314 def test_init_module_attrs_already_set(self):
315 module = type(sys)('ham.eggs')
316 module.__loader__ = object()
317 module.__package__ = 'ham'
318 module.__path__ = ['eggs']
319 module.__file__ = 'ham/eggs/__init__.py'
320 module.__cached__ = self.util.cache_from_source(module.__file__)
321 original = vars(module).copy()
322 spec = self.loc_spec
323 spec.submodule_search_locations = ['']
324 self.bootstrap._SpecMethods(spec).init_module_attrs(module)
325
326 self.assertIs(module.__loader__, original['__loader__'])
327 self.assertEqual(module.__package__, original['__package__'])
328 self.assertIs(module.__path__, original['__path__'])
329 self.assertEqual(module.__file__, original['__file__'])
330 self.assertEqual(module.__cached__, original['__cached__'])
331
332 def test_init_module_attrs_immutable(self):
333 module = object()
334 spec = self.loc_spec
335 spec.submodule_search_locations = ['']
336 self.bootstrap._SpecMethods(spec).init_module_attrs(module)
337
338 self.assertFalse(hasattr(module, '__name__'))
339 self.assertFalse(hasattr(module, '__loader__'))
340 self.assertFalse(hasattr(module, '__package__'))
341 self.assertFalse(hasattr(module, '__spec__'))
342 self.assertFalse(hasattr(module, '__path__'))
343 self.assertFalse(hasattr(module, '__file__'))
344 self.assertFalse(hasattr(module, '__cached__'))
345
346 # create()
347
348 def test_create(self):
349 created = self.bootstrap._SpecMethods(self.spec).create()
350
351 self.assertEqual(created.__name__, self.spec.name)
352 self.assertIs(created.__loader__, self.spec.loader)
353 self.assertEqual(created.__package__, self.spec.parent)
354 self.assertIs(created.__spec__, self.spec)
355 self.assertFalse(hasattr(created, '__path__'))
356 self.assertFalse(hasattr(created, '__file__'))
357 self.assertFalse(hasattr(created, '__cached__'))
358
359 def test_create_from_loader(self):
360 module = type(sys.implementation)()
361 class CreatingLoader(TestLoader):
362 def create_module(self, spec):
363 return module
364 self.spec.loader = CreatingLoader()
365 created = self.bootstrap._SpecMethods(self.spec).create()
366
367 self.assertIs(created, module)
368 self.assertEqual(created.__name__, self.spec.name)
369 self.assertIs(created.__loader__, self.spec.loader)
370 self.assertEqual(created.__package__, self.spec.parent)
371 self.assertIs(created.__spec__, self.spec)
372 self.assertFalse(hasattr(created, '__path__'))
373 self.assertFalse(hasattr(created, '__file__'))
374 self.assertFalse(hasattr(created, '__cached__'))
375
376 def test_create_from_loader_not_handled(self):
377 class CreatingLoader(TestLoader):
378 def create_module(self, spec):
379 return None
380 self.spec.loader = CreatingLoader()
381 created = self.bootstrap._SpecMethods(self.spec).create()
382
383 self.assertEqual(created.__name__, self.spec.name)
384 self.assertIs(created.__loader__, self.spec.loader)
385 self.assertEqual(created.__package__, self.spec.parent)
386 self.assertIs(created.__spec__, self.spec)
387 self.assertFalse(hasattr(created, '__path__'))
388 self.assertFalse(hasattr(created, '__file__'))
389 self.assertFalse(hasattr(created, '__cached__'))
390
391 # exec()
392
393 def test_exec(self):
394 self.spec.loader = NewLoader()
395 module = self.bootstrap._SpecMethods(self.spec).create()
396 sys.modules[self.name] = module
397 self.assertFalse(hasattr(module, 'eggs'))
398 self.bootstrap._SpecMethods(self.spec).exec(module)
399
400 self.assertEqual(module.eggs, 1)
401
402 # load()
403
404 def test_load(self):
405 self.spec.loader = NewLoader()
406 with CleanImport(self.spec.name):
407 loaded = self.bootstrap._SpecMethods(self.spec).load()
408 installed = sys.modules[self.spec.name]
409
410 self.assertEqual(loaded.eggs, 1)
411 self.assertIs(loaded, installed)
412
413 def test_load_replaced(self):
414 replacement = object()
415 class ReplacingLoader(TestLoader):
416 def exec_module(self, module):
417 sys.modules[module.__name__] = replacement
418 self.spec.loader = ReplacingLoader()
419 with CleanImport(self.spec.name):
420 loaded = self.bootstrap._SpecMethods(self.spec).load()
421 installed = sys.modules[self.spec.name]
422
423 self.assertIs(loaded, replacement)
424 self.assertIs(installed, replacement)
425
426 def test_load_failed(self):
427 class FailedLoader(TestLoader):
428 def exec_module(self, module):
429 raise RuntimeError
430 self.spec.loader = FailedLoader()
431 with CleanImport(self.spec.name):
432 with self.assertRaises(RuntimeError):
433 loaded = self.bootstrap._SpecMethods(self.spec).load()
434 self.assertNotIn(self.spec.name, sys.modules)
435
436 def test_load_failed_removed(self):
437 class FailedLoader(TestLoader):
438 def exec_module(self, module):
439 del sys.modules[module.__name__]
440 raise RuntimeError
441 self.spec.loader = FailedLoader()
442 with CleanImport(self.spec.name):
443 with self.assertRaises(RuntimeError):
444 loaded = self.bootstrap._SpecMethods(self.spec).load()
445 self.assertNotIn(self.spec.name, sys.modules)
446
447 def test_load_existing(self):
448 existing = type(sys)('ham')
449 existing.count = 5
450 self.spec.loader = NewLoader()
451 with CleanImport(self.name):
452 sys.modules[self.name] = existing
453 assert self.spec.name == self.name
454 loaded = self.bootstrap._SpecMethods(self.spec).load()
455
456 self.assertEqual(loaded.eggs, 1)
457 self.assertFalse(hasattr(loaded, 'ham'))
458
459 def test_load_legacy(self):
460 self.spec.loader = LegacyLoader()
461 with CleanImport(self.spec.name):
462 loaded = self.bootstrap._SpecMethods(self.spec).load()
463
464 self.assertEqual(loaded.ham, -1)
465
466 def test_load_legacy_attributes(self):
467 self.spec.loader = LegacyLoader()
468 with CleanImport(self.spec.name):
469 loaded = self.bootstrap._SpecMethods(self.spec).load()
470
471 self.assertIs(loaded.__loader__, self.spec.loader)
472 self.assertEqual(loaded.__package__, self.spec.parent)
473 self.assertIs(loaded.__spec__, self.spec)
474
475 def test_load_legacy_attributes_immutable(self):
476 module = object()
477 class ImmutableLoader(TestLoader):
478 def load_module(self, name):
479 sys.modules[name] = module
480 return module
481 self.spec.loader = ImmutableLoader()
482 with CleanImport(self.spec.name):
483 loaded = self.bootstrap._SpecMethods(self.spec).load()
484
485 self.assertIs(sys.modules[self.spec.name], module)
486
487 # reload()
488
489 def test_reload(self):
490 self.spec.loader = NewLoader()
491 with CleanImport(self.spec.name):
492 loaded = self.bootstrap._SpecMethods(self.spec).load()
493 reloaded = self.bootstrap._SpecMethods(self.spec).exec(loaded)
494 installed = sys.modules[self.spec.name]
495
496 self.assertEqual(loaded.eggs, 1)
497 self.assertIs(reloaded, loaded)
498 self.assertIs(installed, loaded)
499
500 def test_reload_modified(self):
501 self.spec.loader = NewLoader()
502 with CleanImport(self.spec.name):
503 loaded = self.bootstrap._SpecMethods(self.spec).load()
504 loaded.eggs = 2
505 reloaded = self.bootstrap._SpecMethods(self.spec).exec(loaded)
506
507 self.assertEqual(loaded.eggs, 1)
508 self.assertIs(reloaded, loaded)
509
510 def test_reload_extra_attributes(self):
511 self.spec.loader = NewLoader()
512 with CleanImport(self.spec.name):
513 loaded = self.bootstrap._SpecMethods(self.spec).load()
514 loaded.available = False
515 reloaded = self.bootstrap._SpecMethods(self.spec).exec(loaded)
516
517 self.assertFalse(loaded.available)
518 self.assertIs(reloaded, loaded)
519
520 def test_reload_init_module_attrs(self):
521 self.spec.loader = NewLoader()
522 with CleanImport(self.spec.name):
523 loaded = self.bootstrap._SpecMethods(self.spec).load()
524 loaded.__name__ = 'ham'
525 del loaded.__loader__
526 del loaded.__package__
527 del loaded.__spec__
528 self.bootstrap._SpecMethods(self.spec).exec(loaded)
529
530 self.assertEqual(loaded.__name__, self.spec.name)
531 self.assertIs(loaded.__loader__, self.spec.loader)
532 self.assertEqual(loaded.__package__, self.spec.parent)
533 self.assertIs(loaded.__spec__, self.spec)
534 self.assertFalse(hasattr(loaded, '__path__'))
535 self.assertFalse(hasattr(loaded, '__file__'))
536 self.assertFalse(hasattr(loaded, '__cached__'))
537
538 def test_reload_legacy(self):
539 self.spec.loader = LegacyLoader()
540 with CleanImport(self.spec.name):
541 loaded = self.bootstrap._SpecMethods(self.spec).load()
542 reloaded = self.bootstrap._SpecMethods(self.spec).exec(loaded)
543 installed = sys.modules[self.spec.name]
544
545 self.assertEqual(loaded.ham, -1)
546 self.assertIs(reloaded, loaded)
547 self.assertIs(installed, loaded)
548
549
550 class Frozen_ModuleSpecMethodsTests(ModuleSpecMethodsTests, unittest.TestCase):
551 bootstrap = frozen_bootstrap
552 machinery = frozen_machinery
553 util = frozen_util
554
555
556 class Source_ModuleSpecMethodsTests(ModuleSpecMethodsTests, unittest.TestCase):
557 bootstrap = source_bootstrap
558 machinery = source_machinery
559 util = source_util
560
561
562 class ModuleReprTests:
563
564 # XXX Add more tests for repr(module) once ModuleSpec._module_repr()
565 # is in place?
566
567 def setUp(self):
568 self.module = type(os)('spam')
569 self.spec = self.machinery.ModuleSpec('spam', TestLoader())
570
571 def test_module___loader___module_repr(self):
572 class Loader:
573 def module_repr(self, module):
574 return '<delicious {}>'.format(module.__name__)
575 self.module.__loader__ = Loader()
576 modrepr = self.bootstrap._module_repr(self.module)
577
578 self.assertEqual(modrepr, '<delicious spam>')
579
580 def test_module___loader___module_repr_bad(self):
581 class Loader(TestLoader):
582 def module_repr(self, module):
583 raise Exception
584 self.module.__loader__ = Loader()
585 modrepr = self.bootstrap._module_repr(self.module)
586
587 self.assertEqual(modrepr,
588 '<module {!r} (<TestLoader object>)>'.format('spam'))
589
590 def test_module___spec__(self):
591 origin = 'in a hole, in the ground'
592 self.spec.origin = origin
593 self.module.__spec__ = self.spec
594 modrepr = self.bootstrap._module_repr(self.module)
595
596 self.assertEqual(modrepr, '<module {!r} ({})>'.format('spam', origin))
597
598 def test_module___spec___location(self):
599 location = 'in_a_galaxy_far_far_away.py'
600 self.spec.origin = location
601 self.spec._set_fileattr = True
602 self.module.__spec__ = self.spec
603 modrepr = self.bootstrap._module_repr(self.module)
604
605 self.assertEqual(modrepr,
606 '<module {!r} from {!r}>'.format('spam', location))
607
608 def test_module___spec___no_origin(self):
609 self.spec.loader = TestLoader()
610 self.module.__spec__ = self.spec
611 modrepr = self.bootstrap._module_repr(self.module)
612
613 self.assertEqual(modrepr,
614 '<module {!r} (<TestLoader object>)>'.format('spam'))
615
616 def test_module___spec___no_origin_no_loader(self):
617 self.spec.loader = None
618 self.module.__spec__ = self.spec
619 modrepr = self.bootstrap._module_repr(self.module)
620
621 self.assertEqual(modrepr, '<module {!r}>'.format('spam'))
622
623 def test_module_no_name(self):
624 del self.module.__name__
625 modrepr = self.bootstrap._module_repr(self.module)
626
627 self.assertEqual(modrepr, '<module {!r}>'.format('?'))
628
629 def test_module_with_file(self):
630 filename = 'e/i/e/i/o/spam.py'
631 self.module.__file__ = filename
632 modrepr = self.bootstrap._module_repr(self.module)
633
634 self.assertEqual(modrepr,
635 '<module {!r} from {!r}>'.format('spam', filename))
636
637 def test_module_no_file(self):
638 self.module.__loader__ = TestLoader()
639 modrepr = self.bootstrap._module_repr(self.module)
640
641 self.assertEqual(modrepr,
642 '<module {!r} (<TestLoader object>)>'.format('spam'))
643
644 def test_module_no_file_no_loader(self):
645 modrepr = self.bootstrap._module_repr(self.module)
646
647 self.assertEqual(modrepr, '<module {!r}>'.format('spam'))
648
649
650 class Frozen_ModuleReprTests(ModuleReprTests, unittest.TestCase):
651 bootstrap = frozen_bootstrap
652 machinery = frozen_machinery
653 util = frozen_util
654
655
656 class Source_ModuleReprTests(ModuleReprTests, unittest.TestCase):
657 bootstrap = source_bootstrap
658 machinery = source_machinery
659 util = source_util
660
661
662 class FactoryTests:
663
664 def setUp(self):
665 self.name = 'spam'
666 self.path = 'spam.py'
667 self.cached = self.util.cache_from_source(self.path)
668 self.loader = TestLoader()
669 self.fileloader = TestLoader(self.path)
670 self.pkgloader = TestLoader(self.path, True)
671
672 # spec_from_loader()
673
674 def test_spec_from_loader_default(self):
675 spec = self.util.spec_from_loader(self.name, self.loader)
676
677 self.assertEqual(spec.name, self.name)
678 self.assertEqual(spec.loader, self.loader)
679 self.assertIs(spec.origin, None)
680 self.assertIs(spec.loader_state, None)
681 self.assertIs(spec.submodule_search_locations, None)
682 self.assertIs(spec.cached, None)
683 self.assertFalse(spec.has_location)
684
685 def test_spec_from_loader_default_with_bad_is_package(self):
686 class Loader:
687 def is_package(self, name):
688 raise ImportError
689 loader = Loader()
690 spec = self.util.spec_from_loader(self.name, loader)
691
692 self.assertEqual(spec.name, self.name)
693 self.assertEqual(spec.loader, loader)
694 self.assertIs(spec.origin, None)
695 self.assertIs(spec.loader_state, None)
696 self.assertIs(spec.submodule_search_locations, None)
697 self.assertIs(spec.cached, None)
698 self.assertFalse(spec.has_location)
699
700 def test_spec_from_loader_origin(self):
701 origin = 'somewhere over the rainbow'
702 spec = self.util.spec_from_loader(self.name, self.loader,
703 origin=origin)
704
705 self.assertEqual(spec.name, self.name)
706 self.assertEqual(spec.loader, self.loader)
707 self.assertIs(spec.origin, origin)
708 self.assertIs(spec.loader_state, None)
709 self.assertIs(spec.submodule_search_locations, None)
710 self.assertIs(spec.cached, None)
711 self.assertFalse(spec.has_location)
712
713 def test_spec_from_loader_is_package_false(self):
714 spec = self.util.spec_from_loader(self.name, self.loader,
715 is_package=False)
716
717 self.assertEqual(spec.name, self.name)
718 self.assertEqual(spec.loader, self.loader)
719 self.assertIs(spec.origin, None)
720 self.assertIs(spec.loader_state, None)
721 self.assertIs(spec.submodule_search_locations, None)
722 self.assertIs(spec.cached, None)
723 self.assertFalse(spec.has_location)
724
725 def test_spec_from_loader_is_package_true(self):
726 spec = self.util.spec_from_loader(self.name, self.loader,
727 is_package=True)
728
729 self.assertEqual(spec.name, self.name)
730 self.assertEqual(spec.loader, self.loader)
731 self.assertIs(spec.origin, None)
732 self.assertIs(spec.loader_state, None)
733 self.assertEqual(spec.submodule_search_locations, [])
734 self.assertIs(spec.cached, None)
735 self.assertFalse(spec.has_location)
736
737 def test_spec_from_loader_origin_and_is_package(self):
738 origin = 'where the streets have no name'
739 spec = self.util.spec_from_loader(self.name, self.loader,
740 origin=origin, is_package=True)
741
742 self.assertEqual(spec.name, self.name)
743 self.assertEqual(spec.loader, self.loader)
744 self.assertIs(spec.origin, origin)
745 self.assertIs(spec.loader_state, None)
746 self.assertEqual(spec.submodule_search_locations, [])
747 self.assertIs(spec.cached, None)
748 self.assertFalse(spec.has_location)
749
750 def test_spec_from_loader_is_package_with_loader_false(self):
751 loader = TestLoader(is_package=False)
752 spec = self.util.spec_from_loader(self.name, loader)
753
754 self.assertEqual(spec.name, self.name)
755 self.assertEqual(spec.loader, loader)
756 self.assertIs(spec.origin, None)
757 self.assertIs(spec.loader_state, None)
758 self.assertIs(spec.submodule_search_locations, None)
759 self.assertIs(spec.cached, None)
760 self.assertFalse(spec.has_location)
761
762 def test_spec_from_loader_is_package_with_loader_true(self):
763 loader = TestLoader(is_package=True)
764 spec = self.util.spec_from_loader(self.name, loader)
765
766 self.assertEqual(spec.name, self.name)
767 self.assertEqual(spec.loader, loader)
768 self.assertIs(spec.origin, None)
769 self.assertIs(spec.loader_state, None)
770 self.assertEqual(spec.submodule_search_locations, [])
771 self.assertIs(spec.cached, None)
772 self.assertFalse(spec.has_location)
773
774 def test_spec_from_loader_default_with_file_loader(self):
775 spec = self.util.spec_from_loader(self.name, self.fileloader)
776
777 self.assertEqual(spec.name, self.name)
778 self.assertEqual(spec.loader, self.fileloader)
779 self.assertEqual(spec.origin, self.path)
780 self.assertIs(spec.loader_state, None)
781 self.assertIs(spec.submodule_search_locations, None)
782 self.assertEqual(spec.cached, self.cached)
783 self.assertTrue(spec.has_location)
784
785 def test_spec_from_loader_is_package_false_with_fileloader(self):
786 spec = self.util.spec_from_loader(self.name, self.fileloader,
787 is_package=False)
788
789 self.assertEqual(spec.name, self.name)
790 self.assertEqual(spec.loader, self.fileloader)
791 self.assertEqual(spec.origin, self.path)
792 self.assertIs(spec.loader_state, None)
793 self.assertIs(spec.submodule_search_locations, None)
794 self.assertEqual(spec.cached, self.cached)
795 self.assertTrue(spec.has_location)
796
797 def test_spec_from_loader_is_package_true_with_fileloader(self):
798 spec = self.util.spec_from_loader(self.name, self.fileloader,
799 is_package=True)
800
801 self.assertEqual(spec.name, self.name)
802 self.assertEqual(spec.loader, self.fileloader)
803 self.assertEqual(spec.origin, self.path)
804 self.assertIs(spec.loader_state, None)
805 self.assertEqual(spec.submodule_search_locations, [''])
806 self.assertEqual(spec.cached, self.cached)
807 self.assertTrue(spec.has_location)
808
809 # spec_from_file_location()
810
811 def test_spec_from_file_location_default(self):
812 if self.machinery is source_machinery:
813 raise unittest.SkipTest('not sure why this is breaking...')
814 spec = self.util.spec_from_file_location(self.name, self.path)
815
816 self.assertEqual(spec.name, self.name)
817 self.assertIsInstance(spec.loader,
818 self.machinery.SourceFileLoader)
819 self.assertEqual(spec.loader.name, self.name)
820 self.assertEqual(spec.loader.path, self.path)
821 self.assertEqual(spec.origin, self.path)
822 self.assertIs(spec.loader_state, None)
823 self.assertIs(spec.submodule_search_locations, None)
824 self.assertEqual(spec.cached, self.cached)
825 self.assertTrue(spec.has_location)
826
827 def test_spec_from_file_location_default_without_location(self):
828 spec = self.util.spec_from_file_location(self.name)
829
830 self.assertIs(spec, None)
831
832 def test_spec_from_file_location_default_bad_suffix(self):
833 spec = self.util.spec_from_file_location(self.name, 'spam.eggs')
834
835 self.assertIs(spec, None)
836
837 def test_spec_from_file_location_loader_no_location(self):
838 spec = self.util.spec_from_file_location(self.name,
839 loader=self.fileloader)
840
841 self.assertEqual(spec.name, self.name)
842 self.assertEqual(spec.loader, self.fileloader)
843 self.assertEqual(spec.origin, self.path)
844 self.assertIs(spec.loader_state, None)
845 self.assertIs(spec.submodule_search_locations, None)
846 self.assertEqual(spec.cached, self.cached)
847 self.assertTrue(spec.has_location)
848
849 def test_spec_from_file_location_loader_no_location_no_get_filename(self):
850 spec = self.util.spec_from_file_location(self.name,
851 loader=self.loader)
852
853 self.assertEqual(spec.name, self.name)
854 self.assertEqual(spec.loader, self.loader)
855 self.assertEqual(spec.origin, '<unknown>')
856 self.assertIs(spec.loader_state, None)
857 self.assertIs(spec.submodule_search_locations, None)
858 self.assertIs(spec.cached, None)
859 self.assertTrue(spec.has_location)
860
861 def test_spec_from_file_location_loader_no_location_bad_get_filename(self):
862 class Loader:
863 def get_filename(self, name):
864 raise ImportError
865 loader = Loader()
866 spec = self.util.spec_from_file_location(self.name, loader=loader)
867
868 self.assertEqual(spec.name, self.name)
869 self.assertEqual(spec.loader, loader)
870 self.assertEqual(spec.origin, '<unknown>')
871 self.assertIs(spec.loader_state, None)
872 self.assertIs(spec.submodule_search_locations, None)
873 self.assertIs(spec.cached, None)
874 self.assertTrue(spec.has_location)
875
876 def test_spec_from_file_location_smsl_none(self):
877 spec = self.util.spec_from_file_location(self.name, self.path,
878 loader=self.fileloader,
879 submodule_search_locations=None)
880
881 self.assertEqual(spec.name, self.name)
882 self.assertEqual(spec.loader, self.fileloader)
883 self.assertEqual(spec.origin, self.path)
884 self.assertIs(spec.loader_state, None)
885 self.assertIs(spec.submodule_search_locations, None)
886 self.assertEqual(spec.cached, self.cached)
887 self.assertTrue(spec.has_location)
888
889 def test_spec_from_file_location_smsl_empty(self):
890 spec = self.util.spec_from_file_location(self.name, self.path,
891 loader=self.fileloader,
892 submodule_search_locations=[])
893
894 self.assertEqual(spec.name, self.name)
895 self.assertEqual(spec.loader, self.fileloader)
896 self.assertEqual(spec.origin, self.path)
897 self.assertIs(spec.loader_state, None)
898 self.assertEqual(spec.submodule_search_locations, [''])
899 self.assertEqual(spec.cached, self.cached)
900 self.assertTrue(spec.has_location)
901
902 def test_spec_from_file_location_smsl_not_empty(self):
903 spec = self.util.spec_from_file_location(self.name, self.path,
904 loader=self.fileloader,
905 submodule_search_locations=['eggs'])
906
907 self.assertEqual(spec.name, self.name)
908 self.assertEqual(spec.loader, self.fileloader)
909 self.assertEqual(spec.origin, self.path)
910 self.assertIs(spec.loader_state, None)
911 self.assertEqual(spec.submodule_search_locations, ['eggs'])
912 self.assertEqual(spec.cached, self.cached)
913 self.assertTrue(spec.has_location)
914
915 def test_spec_from_file_location_smsl_default(self):
916 spec = self.util.spec_from_file_location(self.name, self.path,
917 loader=self.pkgloader)
918
919 self.assertEqual(spec.name, self.name)
920 self.assertEqual(spec.loader, self.pkgloader)
921 self.assertEqual(spec.origin, self.path)
922 self.assertIs(spec.loader_state, None)
923 self.assertEqual(spec.submodule_search_locations, [''])
924 self.assertEqual(spec.cached, self.cached)
925 self.assertTrue(spec.has_location)
926
927 def test_spec_from_file_location_smsl_default_not_package(self):
928 class Loader:
929 def is_package(self, name):
930 return False
931 loader = Loader()
932 spec = self.util.spec_from_file_location(self.name, self.path,
933 loader=loader)
934
935 self.assertEqual(spec.name, self.name)
936 self.assertEqual(spec.loader, loader)
937 self.assertEqual(spec.origin, self.path)
938 self.assertIs(spec.loader_state, None)
939 self.assertIs(spec.submodule_search_locations, None)
940 self.assertEqual(spec.cached, self.cached)
941 self.assertTrue(spec.has_location)
942
943 def test_spec_from_file_location_smsl_default_no_is_package(self):
944 spec = self.util.spec_from_file_location(self.name, self.path,
945 loader=self.fileloader)
946
947 self.assertEqual(spec.name, self.name)
948 self.assertEqual(spec.loader, self.fileloader)
949 self.assertEqual(spec.origin, self.path)
950 self.assertIs(spec.loader_state, None)
951 self.assertIs(spec.submodule_search_locations, None)
952 self.assertEqual(spec.cached, self.cached)
953 self.assertTrue(spec.has_location)
954
955 def test_spec_from_file_location_smsl_default_bad_is_package(self):
956 class Loader:
957 def is_package(self, name):
958 raise ImportError
959 loader = Loader()
960 spec = self.util.spec_from_file_location(self.name, self.path,
961 loader=loader)
962
963 self.assertEqual(spec.name, self.name)
964 self.assertEqual(spec.loader, loader)
965 self.assertEqual(spec.origin, self.path)
966 self.assertIs(spec.loader_state, None)
967 self.assertIs(spec.submodule_search_locations, None)
968 self.assertEqual(spec.cached, self.cached)
969 self.assertTrue(spec.has_location)
970
971
972 class Frozen_FactoryTests(FactoryTests, unittest.TestCase):
973 util = frozen_util
974 machinery = frozen_machinery
975
976
977 class Source_FactoryTests(FactoryTests, unittest.TestCase):
978 util = source_util
979 machinery = source_machinery
LEFTRIGHT

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+