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