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

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

#countcontent
1n/afrom .. import abc
2n/afrom .. import util
3n/a
4n/amachinery = util.import_importlib('importlib.machinery')
5n/a
6n/aimport os.path
7n/aimport sys
8n/aimport types
9n/aimport unittest
10n/aimport importlib.util
11n/aimport importlib
12n/a
13n/a
14n/aclass LoaderTests(abc.LoaderTests):
15n/a
16n/a """Test load_module() for extension modules."""
17n/a
18n/a def setUp(self):
19n/a self.loader = self.machinery.ExtensionFileLoader(util.EXTENSIONS.name,
20n/a util.EXTENSIONS.file_path)
21n/a
22n/a def load_module(self, fullname):
23n/a return self.loader.load_module(fullname)
24n/a
25n/a def test_load_module_API(self):
26n/a # Test the default argument for load_module().
27n/a self.loader.load_module()
28n/a self.loader.load_module(None)
29n/a with self.assertRaises(ImportError):
30n/a self.load_module('XXX')
31n/a
32n/a def test_equality(self):
33n/a other = self.machinery.ExtensionFileLoader(util.EXTENSIONS.name,
34n/a util.EXTENSIONS.file_path)
35n/a self.assertEqual(self.loader, other)
36n/a
37n/a def test_inequality(self):
38n/a other = self.machinery.ExtensionFileLoader('_' + util.EXTENSIONS.name,
39n/a util.EXTENSIONS.file_path)
40n/a self.assertNotEqual(self.loader, other)
41n/a
42n/a def test_module(self):
43n/a with util.uncache(util.EXTENSIONS.name):
44n/a module = self.load_module(util.EXTENSIONS.name)
45n/a for attr, value in [('__name__', util.EXTENSIONS.name),
46n/a ('__file__', util.EXTENSIONS.file_path),
47n/a ('__package__', '')]:
48n/a self.assertEqual(getattr(module, attr), value)
49n/a self.assertIn(util.EXTENSIONS.name, sys.modules)
50n/a self.assertIsInstance(module.__loader__,
51n/a self.machinery.ExtensionFileLoader)
52n/a
53n/a # No extension module as __init__ available for testing.
54n/a test_package = None
55n/a
56n/a # No extension module in a package available for testing.
57n/a test_lacking_parent = None
58n/a
59n/a def test_module_reuse(self):
60n/a with util.uncache(util.EXTENSIONS.name):
61n/a module1 = self.load_module(util.EXTENSIONS.name)
62n/a module2 = self.load_module(util.EXTENSIONS.name)
63n/a self.assertIs(module1, module2)
64n/a
65n/a # No easy way to trigger a failure after a successful import.
66n/a test_state_after_failure = None
67n/a
68n/a def test_unloadable(self):
69n/a name = 'asdfjkl;'
70n/a with self.assertRaises(ImportError) as cm:
71n/a self.load_module(name)
72n/a self.assertEqual(cm.exception.name, name)
73n/a
74n/a def test_is_package(self):
75n/a self.assertFalse(self.loader.is_package(util.EXTENSIONS.name))
76n/a for suffix in self.machinery.EXTENSION_SUFFIXES:
77n/a path = os.path.join('some', 'path', 'pkg', '__init__' + suffix)
78n/a loader = self.machinery.ExtensionFileLoader('pkg', path)
79n/a self.assertTrue(loader.is_package('pkg'))
80n/a
81n/a(Frozen_LoaderTests,
82n/a Source_LoaderTests
83n/a ) = util.test_both(LoaderTests, machinery=machinery)
84n/a
85n/aclass MultiPhaseExtensionModuleTests(abc.LoaderTests):
86n/a """Test loading extension modules with multi-phase initialization (PEP 489)
87n/a """
88n/a
89n/a def setUp(self):
90n/a self.name = '_testmultiphase'
91n/a finder = self.machinery.FileFinder(None)
92n/a self.spec = importlib.util.find_spec(self.name)
93n/a assert self.spec
94n/a self.loader = self.machinery.ExtensionFileLoader(
95n/a self.name, self.spec.origin)
96n/a
97n/a # No extension module as __init__ available for testing.
98n/a test_package = None
99n/a
100n/a # No extension module in a package available for testing.
101n/a test_lacking_parent = None
102n/a
103n/a # Handling failure on reload is the up to the module.
104n/a test_state_after_failure = None
105n/a
106n/a def test_module(self):
107n/a '''Test loading an extension module'''
108n/a with util.uncache(self.name):
109n/a module = self.load_module()
110n/a for attr, value in [('__name__', self.name),
111n/a ('__file__', self.spec.origin),
112n/a ('__package__', '')]:
113n/a self.assertEqual(getattr(module, attr), value)
114n/a with self.assertRaises(AttributeError):
115n/a module.__path__
116n/a self.assertIs(module, sys.modules[self.name])
117n/a self.assertIsInstance(module.__loader__,
118n/a self.machinery.ExtensionFileLoader)
119n/a
120n/a def test_functionality(self):
121n/a '''Test basic functionality of stuff defined in an extension module'''
122n/a with util.uncache(self.name):
123n/a module = self.load_module()
124n/a self.assertIsInstance(module, types.ModuleType)
125n/a ex = module.Example()
126n/a self.assertEqual(ex.demo('abcd'), 'abcd')
127n/a self.assertEqual(ex.demo(), None)
128n/a with self.assertRaises(AttributeError):
129n/a ex.abc
130n/a ex.abc = 0
131n/a self.assertEqual(ex.abc, 0)
132n/a self.assertEqual(module.foo(9, 9), 18)
133n/a self.assertIsInstance(module.Str(), str)
134n/a self.assertEqual(module.Str(1) + '23', '123')
135n/a with self.assertRaises(module.error):
136n/a raise module.error()
137n/a self.assertEqual(module.int_const, 1969)
138n/a self.assertEqual(module.str_const, 'something different')
139n/a
140n/a def test_reload(self):
141n/a '''Test that reload didn't re-set the module's attributes'''
142n/a with util.uncache(self.name):
143n/a module = self.load_module()
144n/a ex_class = module.Example
145n/a importlib.reload(module)
146n/a self.assertIs(ex_class, module.Example)
147n/a
148n/a def test_try_registration(self):
149n/a '''Assert that the PyState_{Find,Add,Remove}Module C API doesn't work'''
150n/a module = self.load_module()
151n/a with self.subTest('PyState_FindModule'):
152n/a self.assertEqual(module.call_state_registration_func(0), None)
153n/a with self.subTest('PyState_AddModule'):
154n/a with self.assertRaises(SystemError):
155n/a module.call_state_registration_func(1)
156n/a with self.subTest('PyState_RemoveModule'):
157n/a with self.assertRaises(SystemError):
158n/a module.call_state_registration_func(2)
159n/a
160n/a def load_module(self):
161n/a '''Load the module from the test extension'''
162n/a return self.loader.load_module(self.name)
163n/a
164n/a def load_module_by_name(self, fullname):
165n/a '''Load a module from the test extension by name'''
166n/a origin = self.spec.origin
167n/a loader = self.machinery.ExtensionFileLoader(fullname, origin)
168n/a spec = importlib.util.spec_from_loader(fullname, loader)
169n/a module = importlib.util.module_from_spec(spec)
170n/a loader.exec_module(module)
171n/a return module
172n/a
173n/a def test_load_submodule(self):
174n/a '''Test loading a simulated submodule'''
175n/a module = self.load_module_by_name('pkg.' + self.name)
176n/a self.assertIsInstance(module, types.ModuleType)
177n/a self.assertEqual(module.__name__, 'pkg.' + self.name)
178n/a self.assertEqual(module.str_const, 'something different')
179n/a
180n/a def test_load_short_name(self):
181n/a '''Test loading module with a one-character name'''
182n/a module = self.load_module_by_name('x')
183n/a self.assertIsInstance(module, types.ModuleType)
184n/a self.assertEqual(module.__name__, 'x')
185n/a self.assertEqual(module.str_const, 'something different')
186n/a self.assertNotIn('x', sys.modules)
187n/a
188n/a def test_load_twice(self):
189n/a '''Test that 2 loads result in 2 module objects'''
190n/a module1 = self.load_module_by_name(self.name)
191n/a module2 = self.load_module_by_name(self.name)
192n/a self.assertIsNot(module1, module2)
193n/a
194n/a def test_unloadable(self):
195n/a '''Test nonexistent module'''
196n/a name = 'asdfjkl;'
197n/a with self.assertRaises(ImportError) as cm:
198n/a self.load_module_by_name(name)
199n/a self.assertEqual(cm.exception.name, name)
200n/a
201n/a def test_unloadable_nonascii(self):
202n/a '''Test behavior with nonexistent module with non-ASCII name'''
203n/a name = 'fo\xf3'
204n/a with self.assertRaises(ImportError) as cm:
205n/a self.load_module_by_name(name)
206n/a self.assertEqual(cm.exception.name, name)
207n/a
208n/a def test_nonmodule(self):
209n/a '''Test returning a non-module object from create works'''
210n/a name = self.name + '_nonmodule'
211n/a mod = self.load_module_by_name(name)
212n/a self.assertNotEqual(type(mod), type(unittest))
213n/a self.assertEqual(mod.three, 3)
214n/a
215n/a # issue 27782
216n/a def test_nonmodule_with_methods(self):
217n/a '''Test creating a non-module object with methods defined'''
218n/a name = self.name + '_nonmodule_with_methods'
219n/a mod = self.load_module_by_name(name)
220n/a self.assertNotEqual(type(mod), type(unittest))
221n/a self.assertEqual(mod.three, 3)
222n/a self.assertEqual(mod.bar(10, 1), 9)
223n/a
224n/a def test_null_slots(self):
225n/a '''Test that NULL slots aren't a problem'''
226n/a name = self.name + '_null_slots'
227n/a module = self.load_module_by_name(name)
228n/a self.assertIsInstance(module, types.ModuleType)
229n/a self.assertEqual(module.__name__, name)
230n/a
231n/a def test_bad_modules(self):
232n/a '''Test SystemError is raised for misbehaving extensions'''
233n/a for name_base in [
234n/a 'bad_slot_large',
235n/a 'bad_slot_negative',
236n/a 'create_int_with_state',
237n/a 'negative_size',
238n/a 'export_null',
239n/a 'export_uninitialized',
240n/a 'export_raise',
241n/a 'export_unreported_exception',
242n/a 'create_null',
243n/a 'create_raise',
244n/a 'create_unreported_exception',
245n/a 'nonmodule_with_exec_slots',
246n/a 'exec_err',
247n/a 'exec_raise',
248n/a 'exec_unreported_exception',
249n/a ]:
250n/a with self.subTest(name_base):
251n/a name = self.name + '_' + name_base
252n/a with self.assertRaises(SystemError):
253n/a self.load_module_by_name(name)
254n/a
255n/a def test_nonascii(self):
256n/a '''Test that modules with non-ASCII names can be loaded'''
257n/a # punycode behaves slightly differently in some-ASCII and no-ASCII
258n/a # cases, so test both
259n/a cases = [
260n/a (self.name + '_zkou\u0161ka_na\u010dten\xed', 'Czech'),
261n/a ('\uff3f\u30a4\u30f3\u30dd\u30fc\u30c8\u30c6\u30b9\u30c8',
262n/a 'Japanese'),
263n/a ]
264n/a for name, lang in cases:
265n/a with self.subTest(name):
266n/a module = self.load_module_by_name(name)
267n/a self.assertEqual(module.__name__, name)
268n/a self.assertEqual(module.__doc__, "Module named in %s" % lang)
269n/a
270n/a
271n/a(Frozen_MultiPhaseExtensionModuleTests,
272n/a Source_MultiPhaseExtensionModuleTests
273n/a ) = util.test_both(MultiPhaseExtensionModuleTests, machinery=machinery)
274n/a
275n/a
276n/aif __name__ == '__main__':
277n/a unittest.main()