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