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

Python code coverage for Lib/test/test_importlib/frozen/test_loader.py

#countcontent
1n/afrom .. import abc
2n/afrom .. import util
3n/a
4n/amachinery = util.import_importlib('importlib.machinery')
5n/a
6n/afrom test.support import captured_stdout
7n/aimport types
8n/aimport unittest
9n/aimport warnings
10n/a
11n/a
12n/aclass ExecModuleTests(abc.LoaderTests):
13n/a
14n/a def exec_module(self, name):
15n/a with util.uncache(name), captured_stdout() as stdout:
16n/a spec = self.machinery.ModuleSpec(
17n/a name, self.machinery.FrozenImporter, origin='frozen',
18n/a is_package=self.machinery.FrozenImporter.is_package(name))
19n/a module = types.ModuleType(name)
20n/a module.__spec__ = spec
21n/a assert not hasattr(module, 'initialized')
22n/a self.machinery.FrozenImporter.exec_module(module)
23n/a self.assertTrue(module.initialized)
24n/a self.assertTrue(hasattr(module, '__spec__'))
25n/a self.assertEqual(module.__spec__.origin, 'frozen')
26n/a return module, stdout.getvalue()
27n/a
28n/a def test_module(self):
29n/a name = '__hello__'
30n/a module, output = self.exec_module(name)
31n/a check = {'__name__': name}
32n/a for attr, value in check.items():
33n/a self.assertEqual(getattr(module, attr), value)
34n/a self.assertEqual(output, 'Hello world!\n')
35n/a self.assertTrue(hasattr(module, '__spec__'))
36n/a
37n/a def test_package(self):
38n/a name = '__phello__'
39n/a module, output = self.exec_module(name)
40n/a check = {'__name__': name}
41n/a for attr, value in check.items():
42n/a attr_value = getattr(module, attr)
43n/a self.assertEqual(attr_value, value,
44n/a 'for {name}.{attr}, {given!r} != {expected!r}'.format(
45n/a name=name, attr=attr, given=attr_value,
46n/a expected=value))
47n/a self.assertEqual(output, 'Hello world!\n')
48n/a
49n/a def test_lacking_parent(self):
50n/a name = '__phello__.spam'
51n/a with util.uncache('__phello__'):
52n/a module, output = self.exec_module(name)
53n/a check = {'__name__': name}
54n/a for attr, value in check.items():
55n/a attr_value = getattr(module, attr)
56n/a self.assertEqual(attr_value, value,
57n/a 'for {name}.{attr}, {given} != {expected!r}'.format(
58n/a name=name, attr=attr, given=attr_value,
59n/a expected=value))
60n/a self.assertEqual(output, 'Hello world!\n')
61n/a
62n/a def test_module_repr(self):
63n/a name = '__hello__'
64n/a module, output = self.exec_module(name)
65n/a with warnings.catch_warnings():
66n/a warnings.simplefilter('ignore', DeprecationWarning)
67n/a repr_str = self.machinery.FrozenImporter.module_repr(module)
68n/a self.assertEqual(repr_str,
69n/a "<module '__hello__' (frozen)>")
70n/a
71n/a def test_module_repr_indirect(self):
72n/a name = '__hello__'
73n/a module, output = self.exec_module(name)
74n/a self.assertEqual(repr(module),
75n/a "<module '__hello__' (frozen)>")
76n/a
77n/a # No way to trigger an error in a frozen module.
78n/a test_state_after_failure = None
79n/a
80n/a def test_unloadable(self):
81n/a assert self.machinery.FrozenImporter.find_module('_not_real') is None
82n/a with self.assertRaises(ImportError) as cm:
83n/a self.exec_module('_not_real')
84n/a self.assertEqual(cm.exception.name, '_not_real')
85n/a
86n/a
87n/a(Frozen_ExecModuleTests,
88n/a Source_ExecModuleTests
89n/a ) = util.test_both(ExecModuleTests, machinery=machinery)
90n/a
91n/a
92n/aclass LoaderTests(abc.LoaderTests):
93n/a
94n/a def test_module(self):
95n/a with util.uncache('__hello__'), captured_stdout() as stdout:
96n/a with warnings.catch_warnings():
97n/a warnings.simplefilter('ignore', DeprecationWarning)
98n/a module = self.machinery.FrozenImporter.load_module('__hello__')
99n/a check = {'__name__': '__hello__',
100n/a '__package__': '',
101n/a '__loader__': self.machinery.FrozenImporter,
102n/a }
103n/a for attr, value in check.items():
104n/a self.assertEqual(getattr(module, attr), value)
105n/a self.assertEqual(stdout.getvalue(), 'Hello world!\n')
106n/a self.assertFalse(hasattr(module, '__file__'))
107n/a
108n/a def test_package(self):
109n/a with util.uncache('__phello__'), captured_stdout() as stdout:
110n/a with warnings.catch_warnings():
111n/a warnings.simplefilter('ignore', DeprecationWarning)
112n/a module = self.machinery.FrozenImporter.load_module('__phello__')
113n/a check = {'__name__': '__phello__',
114n/a '__package__': '__phello__',
115n/a '__path__': [],
116n/a '__loader__': self.machinery.FrozenImporter,
117n/a }
118n/a for attr, value in check.items():
119n/a attr_value = getattr(module, attr)
120n/a self.assertEqual(attr_value, value,
121n/a "for __phello__.%s, %r != %r" %
122n/a (attr, attr_value, value))
123n/a self.assertEqual(stdout.getvalue(), 'Hello world!\n')
124n/a self.assertFalse(hasattr(module, '__file__'))
125n/a
126n/a def test_lacking_parent(self):
127n/a with util.uncache('__phello__', '__phello__.spam'), \
128n/a captured_stdout() as stdout:
129n/a with warnings.catch_warnings():
130n/a warnings.simplefilter('ignore', DeprecationWarning)
131n/a module = self.machinery.FrozenImporter.load_module('__phello__.spam')
132n/a check = {'__name__': '__phello__.spam',
133n/a '__package__': '__phello__',
134n/a '__loader__': self.machinery.FrozenImporter,
135n/a }
136n/a for attr, value in check.items():
137n/a attr_value = getattr(module, attr)
138n/a self.assertEqual(attr_value, value,
139n/a "for __phello__.spam.%s, %r != %r" %
140n/a (attr, attr_value, value))
141n/a self.assertEqual(stdout.getvalue(), 'Hello world!\n')
142n/a self.assertFalse(hasattr(module, '__file__'))
143n/a
144n/a def test_module_reuse(self):
145n/a with util.uncache('__hello__'), captured_stdout() as stdout:
146n/a with warnings.catch_warnings():
147n/a warnings.simplefilter('ignore', DeprecationWarning)
148n/a module1 = self.machinery.FrozenImporter.load_module('__hello__')
149n/a module2 = self.machinery.FrozenImporter.load_module('__hello__')
150n/a self.assertIs(module1, module2)
151n/a self.assertEqual(stdout.getvalue(),
152n/a 'Hello world!\nHello world!\n')
153n/a
154n/a def test_module_repr(self):
155n/a with util.uncache('__hello__'), captured_stdout():
156n/a with warnings.catch_warnings():
157n/a warnings.simplefilter('ignore', DeprecationWarning)
158n/a module = self.machinery.FrozenImporter.load_module('__hello__')
159n/a repr_str = self.machinery.FrozenImporter.module_repr(module)
160n/a self.assertEqual(repr_str,
161n/a "<module '__hello__' (frozen)>")
162n/a
163n/a def test_module_repr_indirect(self):
164n/a with util.uncache('__hello__'), captured_stdout():
165n/a module = self.machinery.FrozenImporter.load_module('__hello__')
166n/a self.assertEqual(repr(module),
167n/a "<module '__hello__' (frozen)>")
168n/a
169n/a # No way to trigger an error in a frozen module.
170n/a test_state_after_failure = None
171n/a
172n/a def test_unloadable(self):
173n/a assert self.machinery.FrozenImporter.find_module('_not_real') is None
174n/a with self.assertRaises(ImportError) as cm:
175n/a self.machinery.FrozenImporter.load_module('_not_real')
176n/a self.assertEqual(cm.exception.name, '_not_real')
177n/a
178n/a
179n/a(Frozen_LoaderTests,
180n/a Source_LoaderTests
181n/a ) = util.test_both(LoaderTests, machinery=machinery)
182n/a
183n/a
184n/aclass InspectLoaderTests:
185n/a
186n/a """Tests for the InspectLoader methods for FrozenImporter."""
187n/a
188n/a def test_get_code(self):
189n/a # Make sure that the code object is good.
190n/a name = '__hello__'
191n/a with captured_stdout() as stdout:
192n/a code = self.machinery.FrozenImporter.get_code(name)
193n/a mod = types.ModuleType(name)
194n/a exec(code, mod.__dict__)
195n/a self.assertTrue(hasattr(mod, 'initialized'))
196n/a self.assertEqual(stdout.getvalue(), 'Hello world!\n')
197n/a
198n/a def test_get_source(self):
199n/a # Should always return None.
200n/a result = self.machinery.FrozenImporter.get_source('__hello__')
201n/a self.assertIsNone(result)
202n/a
203n/a def test_is_package(self):
204n/a # Should be able to tell what is a package.
205n/a test_for = (('__hello__', False), ('__phello__', True),
206n/a ('__phello__.spam', False))
207n/a for name, is_package in test_for:
208n/a result = self.machinery.FrozenImporter.is_package(name)
209n/a self.assertEqual(bool(result), is_package)
210n/a
211n/a def test_failure(self):
212n/a # Raise ImportError for modules that are not frozen.
213n/a for meth_name in ('get_code', 'get_source', 'is_package'):
214n/a method = getattr(self.machinery.FrozenImporter, meth_name)
215n/a with self.assertRaises(ImportError) as cm:
216n/a method('importlib')
217n/a self.assertEqual(cm.exception.name, 'importlib')
218n/a
219n/a(Frozen_ILTests,
220n/a Source_ILTests
221n/a ) = util.test_both(InspectLoaderTests, machinery=machinery)
222n/a
223n/a
224n/aif __name__ == '__main__':
225n/a unittest.main()