ยปCore Development>Code coverage>Lib/test/test_importlib/test_spec.py

Python code coverage for Lib/test/test_importlib/test_spec.py

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