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