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