Debian Codegraph @

Lib/test/test_capi/test_import.py

raw
1 import importlib.util
2 import os.path
3 import sys
4 import types
5 import unittest
6 from test.support import os_helper
7 from test.support import import_helper
8 from test.support.warnings_helper import check_warnings
9
10 _testcapi = import_helper.import_module('_testcapi')
11 _testlimitedcapi = import_helper.import_module('_testlimitedcapi')
12 NULL = None
13
14
15 class ImportTests(unittest.TestCase):
16 def test_getmagicnumber(self):
17 # Test PyImport_GetMagicNumber()
18 magic = _testlimitedcapi.PyImport_GetMagicNumber()
19 self.assertEqual(magic,
20 int.from_bytes(importlib.util.MAGIC_NUMBER, 'little'))
21
22 def test_getmagictag(self):
23 # Test PyImport_GetMagicTag()
24 tag = _testlimitedcapi.PyImport_GetMagicTag()
25 self.assertEqual(tag, sys.implementation.cache_tag)
26
27 def test_getmoduledict(self):
28 # Test PyImport_GetModuleDict()
29 modules = _testlimitedcapi.PyImport_GetModuleDict()
30 self.assertIs(modules, sys.modules)
31
32 def check_import_loaded_module(self, import_module):
33 for name in ('os', 'sys', 'test', 'unittest'):
34 with self.subTest(name=name):
35 self.assertIn(name, sys.modules)
36 old_module = sys.modules[name]
37 module = import_module(name)
38 self.assertIsInstance(module, types.ModuleType)
39 self.assertIs(module, old_module)
40
41 def check_import_fresh_module(self, import_module):
42 old_modules = dict(sys.modules)
43 try:
44 for name in ('colorsys', 'math'):
45 with self.subTest(name=name):
46 sys.modules.pop(name, None)
47 module = import_module(name)
48 self.assertIsInstance(module, types.ModuleType)
49 self.assertIs(module, sys.modules[name])
50 self.assertEqual(module.__name__, name)
51 finally:
52 sys.modules.clear()
53 sys.modules.update(old_modules)
54
55 def test_getmodule(self):
56 # Test PyImport_GetModule()
57 getmodule = _testlimitedcapi.PyImport_GetModule
58 self.check_import_loaded_module(getmodule)
59
60 nonexistent = 'nonexistent'
61 self.assertNotIn(nonexistent, sys.modules)
62 self.assertIs(getmodule(nonexistent), KeyError)
63 self.assertIs(getmodule(''), KeyError)
64 self.assertIs(getmodule(object()), KeyError)
65
66 self.assertRaises(TypeError, getmodule, []) # unhashable
67 # CRASHES getmodule(NULL)
68
69 def check_addmodule(self, add_module, accept_nonstr=False):
70 # create a new module
71 names = ['nonexistent']
72 if accept_nonstr:
73 names.append(b'\xff') # non-UTF-8
74 # PyImport_AddModuleObject() accepts non-string names
75 names.append(tuple(['hashable non-string']))
76 for name in names:
77 with self.subTest(name=name):
78 self.assertNotIn(name, sys.modules)
79 try:
80 module = add_module(name)
81 self.assertIsInstance(module, types.ModuleType)
82 self.assertEqual(module.__name__, name)
83 self.assertIs(module, sys.modules[name])
84 finally:
85 sys.modules.pop(name, None)
86
87 # get an existing module
88 self.check_import_loaded_module(add_module)
89
90 def test_addmoduleobject(self):
91 # Test PyImport_AddModuleObject()
92 addmoduleobject = _testlimitedcapi.PyImport_AddModuleObject
93 self.check_addmodule(addmoduleobject, accept_nonstr=True)
94
95 self.assertRaises(TypeError, addmoduleobject, []) # unhashable
96 # CRASHES addmoduleobject(NULL)
97
98 def test_addmodule(self):
99 # Test PyImport_AddModule()
100 addmodule = _testlimitedcapi.PyImport_AddModule
101 self.check_addmodule(addmodule)
102
103 self.assertRaises(UnicodeDecodeError, addmodule, b'\xff')
104 # CRASHES addmodule(NULL)
105
106 def test_addmoduleref(self):
107 # Test PyImport_AddModuleRef()
108 addmoduleref = _testlimitedcapi.PyImport_AddModuleRef
109 self.check_addmodule(addmoduleref)
110
111 self.assertRaises(UnicodeDecodeError, addmoduleref, b'\xff')
112 # CRASHES addmoduleref(NULL)
113
114 def check_import_func(self, import_module):
115 self.check_import_loaded_module(import_module)
116 self.check_import_fresh_module(import_module)
117 self.assertRaises(ModuleNotFoundError, import_module, 'nonexistent')
118 self.assertRaises(ValueError, import_module, '')
119
120 def test_import(self):
121 # Test PyImport_Import()
122 import_ = _testlimitedcapi.PyImport_Import
123 self.check_import_func(import_)
124
125 self.assertRaises(TypeError, import_, b'os')
126 self.assertRaises(SystemError, import_, NULL)
127
128 def test_importmodule(self):
129 # Test PyImport_ImportModule()
130 importmodule = _testlimitedcapi.PyImport_ImportModule
131 self.check_import_func(importmodule)
132
133 self.assertRaises(UnicodeDecodeError, importmodule, b'\xff')
134 # CRASHES importmodule(NULL)
135
136 def test_importmodulenoblock(self):
137 # Test deprecated PyImport_ImportModuleNoBlock()
138 importmodulenoblock = _testlimitedcapi.PyImport_ImportModuleNoBlock
139 with check_warnings(('', DeprecationWarning)):
140 self.check_import_func(importmodulenoblock)
141 self.assertRaises(UnicodeDecodeError, importmodulenoblock, b'\xff')
142
143 # CRASHES importmodulenoblock(NULL)
144
145 def check_frozen_import(self, import_frozen_module):
146 # Importing a frozen module executes its code, so start by unloading
147 # the module to execute the code in a new (temporary) module.
148 old_zipimport = sys.modules.pop('zipimport')
149 try:
150 self.assertEqual(import_frozen_module('zipimport'), 1)
151
152 # import zipimport again
153 self.assertEqual(import_frozen_module('zipimport'), 1)
154 finally:
155 sys.modules['zipimport'] = old_zipimport
156
157 # not a frozen module
158 self.assertEqual(import_frozen_module('sys'), 0)
159 self.assertEqual(import_frozen_module('nonexistent'), 0)
160 self.assertEqual(import_frozen_module(''), 0)
161
162 def test_importfrozenmodule(self):
163 # Test PyImport_ImportFrozenModule()
164 importfrozenmodule = _testlimitedcapi.PyImport_ImportFrozenModule
165 self.check_frozen_import(importfrozenmodule)
166
167 self.assertRaises(UnicodeDecodeError, importfrozenmodule, b'\xff')
168 # CRASHES importfrozenmodule(NULL)
169
170 def test_importfrozenmoduleobject(self):
171 # Test PyImport_ImportFrozenModuleObject()
172 importfrozenmoduleobject = _testlimitedcapi.PyImport_ImportFrozenModuleObject
173 self.check_frozen_import(importfrozenmoduleobject)
174 self.assertEqual(importfrozenmoduleobject(b'zipimport'), 0)
175 self.assertEqual(importfrozenmoduleobject(NULL), 0)
176
177 def test_importmoduleex(self):
178 # Test PyImport_ImportModuleEx()
179 importmoduleex = _testlimitedcapi.PyImport_ImportModuleEx
180 self.check_import_func(lambda name: importmoduleex(name, NULL, NULL, NULL))
181
182 self.assertRaises(ModuleNotFoundError, importmoduleex, 'nonexistent', NULL, NULL, NULL)
183 self.assertRaises(ValueError, importmoduleex, '', NULL, NULL, NULL)
184 self.assertRaises(UnicodeDecodeError, importmoduleex, b'\xff', NULL, NULL, NULL)
185 # CRASHES importmoduleex(NULL, NULL, NULL, NULL)
186
187 def check_importmodulelevel(self, importmodulelevel):
188 self.check_import_func(lambda name: importmodulelevel(name, NULL, NULL, NULL, 0))
189
190 self.assertRaises(ModuleNotFoundError, importmodulelevel, 'nonexistent', NULL, NULL, NULL, 0)
191 self.assertRaises(ValueError, importmodulelevel, '', NULL, NULL, NULL, 0)
192
193 if __package__:
194 self.assertIs(importmodulelevel('test_import', globals(), NULL, NULL, 1),
195 sys.modules['test.test_capi.test_import'])
196 self.assertIs(importmodulelevel('test_capi', globals(), NULL, NULL, 2),
197 sys.modules['test.test_capi'])
198 self.assertRaises(ValueError, importmodulelevel, 'os', NULL, NULL, NULL, -1)
199 with self.assertWarns(ImportWarning):
200 self.assertRaises(KeyError, importmodulelevel, 'test_import', {}, NULL, NULL, 1)
201 self.assertRaises(TypeError, importmodulelevel, 'test_import', [], NULL, NULL, 1)
202
203 def test_importmodulelevel(self):
204 # Test PyImport_ImportModuleLevel()
205 importmodulelevel = _testlimitedcapi.PyImport_ImportModuleLevel
206 self.check_importmodulelevel(importmodulelevel)
207
208 self.assertRaises(UnicodeDecodeError, importmodulelevel, b'\xff', NULL, NULL, NULL, 0)
209 # CRASHES importmodulelevel(NULL, NULL, NULL, NULL, 0)
210
211 def test_importmodulelevelobject(self):
212 # Test PyImport_ImportModuleLevelObject()
213 importmodulelevel = _testlimitedcapi.PyImport_ImportModuleLevelObject
214 self.check_importmodulelevel(importmodulelevel)
215
216 self.assertRaises(TypeError, importmodulelevel, b'os', NULL, NULL, NULL, 0)
217 self.assertRaises(ValueError, importmodulelevel, NULL, NULL, NULL, NULL, 0)
218
219 def check_executecodemodule(self, execute_code, *args):
220 name = 'test_import_executecode'
221 try:
222 # Create a temporary module where the code will be executed
223 self.assertNotIn(name, sys.modules)
224 module = _testlimitedcapi.PyImport_AddModuleRef(name)
225 self.assertNotHasAttr(module, 'attr')
226
227 # Execute the code
228 code = compile('attr = 1', '<test>', 'exec')
229 module2 = execute_code(name, code, *args)
230 self.assertIs(module2, module)
231
232 # Check the function side effects
233 self.assertEqual(module.attr, 1)
234 finally:
235 sys.modules.pop(name, None)
236 return module.__spec__.origin
237
238 def test_executecodemodule(self):
239 # Test PyImport_ExecCodeModule()
240 execcodemodule = _testlimitedcapi.PyImport_ExecCodeModule
241 self.check_executecodemodule(execcodemodule)
242
243 code = compile('attr = 1', '<test>', 'exec')
244 self.assertRaises(UnicodeDecodeError, execcodemodule, b'\xff', code)
245 # CRASHES execcodemodule(NULL, code)
246 # CRASHES execcodemodule(name, NULL)
247
248 def test_executecodemoduleex(self):
249 # Test PyImport_ExecCodeModuleEx()
250 execcodemoduleex = _testlimitedcapi.PyImport_ExecCodeModuleEx
251
252 # Test NULL path (it should not crash)
253 self.check_executecodemodule(execcodemoduleex, NULL)
254
255 # Test non-NULL path
256 pathname = b'pathname'
257 origin = self.check_executecodemodule(execcodemoduleex, pathname)
258 self.assertEqual(origin, os.path.abspath(os.fsdecode(pathname)))
259
260 pathname = os_helper.TESTFN_UNDECODABLE
261 if pathname:
262 origin = self.check_executecodemodule(execcodemoduleex, pathname)
263 self.assertEqual(origin, os.path.abspath(os.fsdecode(pathname)))
264
265 code = compile('attr = 1', '<test>', 'exec')
266 self.assertRaises(UnicodeDecodeError, execcodemoduleex, b'\xff', code, NULL)
267 # CRASHES execcodemoduleex(NULL, code, NULL)
268 # CRASHES execcodemoduleex(name, NULL, NULL)
269
270 def check_executecode_pathnames(self, execute_code_func, object=False):
271 # Test non-NULL pathname and NULL cpathname
272
273 # Test NULL paths (it should not crash)
274 self.check_executecodemodule(execute_code_func, NULL, NULL)
275
276 pathname = 'pathname'
277 origin = self.check_executecodemodule(execute_code_func, pathname, NULL)
278 self.assertEqual(origin, os.path.abspath(os.fsdecode(pathname)))
279 origin = self.check_executecodemodule(execute_code_func, NULL, pathname)
280 if not object:
281 self.assertEqual(origin, os.path.abspath(os.fsdecode(pathname)))
282
283 pathname = os_helper.TESTFN_UNDECODABLE
284 if pathname:
285 if object:
286 pathname = os.fsdecode(pathname)
287 origin = self.check_executecodemodule(execute_code_func, pathname, NULL)
288 self.assertEqual(origin, os.path.abspath(os.fsdecode(pathname)))
289 self.check_executecodemodule(execute_code_func, NULL, pathname)
290
291 # Test NULL pathname and non-NULL cpathname
292 pyc_filename = importlib.util.cache_from_source(__file__)
293 py_filename = importlib.util.source_from_cache(pyc_filename)
294 origin = self.check_executecodemodule(execute_code_func, NULL, pyc_filename)
295 if not object:
296 self.assertEqual(origin, py_filename)
297
298 def test_executecodemodulewithpathnames(self):
299 # Test PyImport_ExecCodeModuleWithPathnames()
300 execute_code_func = _testlimitedcapi.PyImport_ExecCodeModuleWithPathnames
301 self.check_executecode_pathnames(execute_code_func)
302
303 code = compile('attr = 1', '<test>', 'exec')
304 self.assertRaises(UnicodeDecodeError, execute_code_func, b'\xff', code, NULL, NULL)
305 # CRASHES execute_code_func(NULL, code, NULL, NULL)
306 # CRASHES execute_code_func(name, NULL, NULL, NULL)
307
308 def test_executecodemoduleobject(self):
309 # Test PyImport_ExecCodeModuleObject()
310 execute_code_func = _testlimitedcapi.PyImport_ExecCodeModuleObject
311 self.check_executecode_pathnames(execute_code_func, object=True)
312
313 code = compile('attr = 1', '<test>', 'exec')
314 self.assertRaises(TypeError, execute_code_func, [], code, NULL, NULL)
315 nonstring = tuple(['hashable non-string'])
316 self.assertRaises(AttributeError, execute_code_func, nonstring, code, NULL, NULL)
317 sys.modules.pop(nonstring, None)
318 # CRASHES execute_code_func(NULL, code, NULL, NULL)
319 # CRASHES execute_code_func(name, NULL, NULL, NULL)
320
321 def check_importmoduleattr(self, importmoduleattr):
322 self.assertIs(importmoduleattr('sys', 'argv'), sys.argv)
323 self.assertIs(importmoduleattr('types', 'ModuleType'), types.ModuleType)
324
325 # module name containing a dot
326 attr = importmoduleattr('email.message', 'Message')
327 from email.message import Message
328 self.assertIs(attr, Message)
329
330 with self.assertRaises(ImportError):
331 # nonexistent module
332 importmoduleattr('nonexistentmodule', 'attr')
333 with self.assertRaises(AttributeError):
334 # nonexistent attribute
335 importmoduleattr('sys', 'nonexistentattr')
336 with self.assertRaises(AttributeError):
337 # attribute name containing a dot
338 importmoduleattr('sys', 'implementation.name')
339
340 def test_importmoduleattr(self):
341 # Test PyImport_ImportModuleAttr()
342 importmoduleattr = _testcapi.PyImport_ImportModuleAttr
343 self.check_importmoduleattr(importmoduleattr)
344
345 # Invalid module name type
346 for mod_name in (object(), 123, b'bytes'):
347 with self.subTest(mod_name=mod_name):
348 with self.assertRaises(TypeError):
349 importmoduleattr(mod_name, "attr")
350
351 # Invalid attribute name type
352 for attr_name in (object(), 123, b'bytes'):
353 with self.subTest(attr_name=attr_name):
354 with self.assertRaises(TypeError):
355 importmoduleattr("sys", attr_name)
356
357 with self.assertRaises(SystemError):
358 importmoduleattr(NULL, "argv")
359 # CRASHES importmoduleattr("sys", NULL)
360
361 def test_importmoduleattrstring(self):
362 # Test PyImport_ImportModuleAttrString()
363 importmoduleattr = _testcapi.PyImport_ImportModuleAttrString
364 self.check_importmoduleattr(importmoduleattr)
365
366 with self.assertRaises(UnicodeDecodeError):
367 importmoduleattr(b"sys\xff", "argv")
368 with self.assertRaises(UnicodeDecodeError):
369 importmoduleattr("sys", b"argv\xff")
370
371 # CRASHES importmoduleattr(NULL, "argv")
372 # CRASHES importmoduleattr("sys", NULL)
373
374 # TODO: test PyImport_GetImporter()
375 # TODO: test PyImport_ReloadModule()
376 # TODO: test PyImport_ExtendInittab()
377 # PyImport_AppendInittab() is tested by test_embed
378
379
380 if __name__ == "__main__":
381 unittest.main()