def test_attributes_set(self): # __name__, __loader__, and __package__ should be set (when # is_package() is defined; undefined implicitly tested elsewhere). class FakeLoader: def __init__(self, is_package): self._pkg = is_package def is_package(self, name): return self._pkg @self.module_for_loader def load_module(self, module): return module name = 'pkg.mod' with util.uncache(name): loader = FakeLoader(False) module = loader.load_module(name) self.assertEqual(module.__name__, name) self.assertIs(module.__loader__, loader) self.assertEqual(module.__package__, 'pkg') name = 'pkg.sub' with util.uncache(name): loader = FakeLoader(True) module = loader.load_module(name) self.assertEqual(module.__name__, name) self.assertIs(module.__loader__, loader) self.assertEqual(module.__package__, name)
def test_attributes_set(self): class FakeLoader: def __init__(self, is_package): self._pkg = is_package def is_package(self, name): return self._pkg @self.module_for_loader def load_module(self, module): return module name = 'pkg.mod' with util.uncache(name): loader = FakeLoader(False) module = loader.load_module(name) self.assertEqual(module.__name__, name) self.assertIs(module.__loader__, loader) self.assertEqual(module.__package__, 'pkg') name = 'pkg.sub' with util.uncache(name): loader = FakeLoader(True) module = loader.load_module(name) self.assertEqual(module.__name__, name) self.assertIs(module.__loader__, loader) self.assertEqual(module.__package__, name)
def test_new_module_failure(self): # Test that a module is removed from sys.modules if added but an # exception is raised. name = 'a.b.c' with util.uncache(name): self.raise_exception(name) self.assertNotIn(name, sys.modules)
def test_reload(self): '''Test that reload didn't re-set the module's attributes''' with util.uncache(self.name): module = self.load_module() ex_class = module.Example importlib.reload(module) self.assertIs(ex_class, module.Example)
def test_reload_failure(self): name = 'a.b.c' module = types.ModuleType(name) with util.uncache(name): sys.modules[name] = module self.raise_exception(name) self.assertIs(module, sys.modules[name])
def test_new_module(self): module_name = 'a.b.c' with util.uncache(module_name): module = self.return_module(module_name) self.assertIn(module_name, sys.modules) self.assertIsInstance(module, types.ModuleType) self.assertEqual(module.__name__, module_name)
def test_reload_failure(self): # Test that a failure on reload leaves the module in-place. name = 'a.b.c' module = types.ModuleType(name) with util.uncache(name): sys.modules[name] = module self.raise_exception(name) self.assertIs(module, sys.modules[name])
def test_sys_modules_spec_is_None(self): name = 'some_mod' with util.uncache(name): module = types.ModuleType(name) module.__spec__ = None sys.modules[name] = module with self.assertRaises(ValueError): self.util.find_spec(name)
def test_sys_modules_loader_is_None(self): name = 'some_mod' with util.uncache(name): module = types.ModuleType(name) spec = self.machinery.ModuleSpec(name, None) module.__spec__ = spec sys.modules[name] = module found = self.util.find_spec(name) self.assertEqual(found, spec)
def test_package_settings(self): # __package__ needs to be set, while __path__ is set on if the module # is a package. # Testing the values for a package are covered by test_load_module. self.setUp(is_package=False) with util.uncache(self.name): module = self.loader.load_module(self.name) self.verify_module(module) self.assertTrue(not hasattr(module, '__path__'))
def test_load_module(self): # Loading a module should set __name__, __loader__, __package__, # __path__ (for packages), __file__, and __cached__. # The module should also be put into sys.modules. with util.uncache(self.name): module = self.loader.load_module(self.name) self.verify_module(module) self.assertEqual(module.__path__, [os.path.dirname(self.path)]) self.assertIn(self.name, sys.modules)
def test_new_module(self): # Test that when no module exists in sys.modules a new module is # created. module_name = 'a.b.c' with util.uncache(module_name): module = self.return_module(module_name) self.assertIn(module_name, sys.modules) self.assertIsInstance(module, types.ModuleType) self.assertEqual(module.__name__, module_name)
def test_module(self): with util.uncache(util.EXTENSIONS.name): module = self.load_module(util.EXTENSIONS.name) for attr, value in [('__name__', util.EXTENSIONS.name), ('__file__', util.EXTENSIONS.file_path), ('__package__', '')]: self.assertEqual(getattr(module, attr), value) self.assertIn(util.EXTENSIONS.name, sys.modules) self.assertIsInstance(module.__loader__, self.machinery.ExtensionFileLoader)
def test_sys_modules_spec_is_not_set(self): name = 'some_mod' with util.uncache(name): module = types.ModuleType(name) try: del module.__spec__ except AttributeError: pass sys.modules[name] = module with self.assertRaises(ValueError): self.util.find_spec(name)
def test_sys_modules_without___loader__(self): name = 'some_mod' with util.uncache(name): module = types.ModuleType(name) del module.__loader__ loader = 'a loader!' spec = self.machinery.ModuleSpec(name, loader) module.__spec__ = spec sys.modules[name] = module found = self.util.find_spec(name) self.assertEqual(found, spec)
def test_false_module(self): # If for some odd reason a module is considered false, still return it # from sys.modules. class FalseModule(types.ModuleType): def __bool__(self): return False name = 'mod' module = FalseModule(name) with util.uncache(name): self.assertFalse(module) sys.modules[name] = module given = self.return_module(name) self.assertIs(given, module)
def test_module(self): '''Test loading an extension module''' with util.uncache(self.name): module = self.load_module() for attr, value in [('__name__', self.name), ('__file__', self.spec.origin), ('__package__', '')]: self.assertEqual(getattr(module, attr), value) with self.assertRaises(AttributeError): module.__path__ self.assertIs(module, sys.modules[self.name]) self.assertIsInstance(module.__loader__, self.machinery.ExtensionFileLoader)
def test_false_module(self): class FalseModule(types.ModuleType): def __bool__(self): return False name = 'mod' module = FalseModule(name) with util.uncache(name): self.assertFalse(module) sys.modules[name] = module given = self.return_module(name) self.assertIs(given, module)
def test_file_from_empty_string_dir(self): # Loading a module found from an empty string entry on sys.path should # not only work, but keep all attributes relative. file_path = '_temp.py' with open(file_path, 'w') as file: file.write("# test file for importlib") try: with util.uncache('_temp'): loader = machinery.SourceFileLoader('_temp', file_path) mod = loader.load_module('_temp') self.assertEqual(file_path, mod.__file__) self.assertEqual(importlib.util.cache_from_source(file_path), mod.__cached__) finally: os.unlink(file_path) pycache = os.path.dirname(importlib.util.cache_from_source(file_path)) if os.path.exists(pycache): shutil.rmtree(pycache)
def test_functionality(self): '''Test basic functionality of stuff defined in an extension module''' with util.uncache(self.name): module = self.load_module() self.assertIsInstance(module, types.ModuleType) ex = module.Example() self.assertEqual(ex.demo('abcd'), 'abcd') self.assertEqual(ex.demo(), None) with self.assertRaises(AttributeError): ex.abc ex.abc = 0 self.assertEqual(ex.abc, 0) self.assertEqual(module.foo(9, 9), 18) self.assertIsInstance(module.Str(), str) self.assertEqual(module.Str(1) + '23', '123') with self.assertRaises(module.error): raise module.error() self.assertEqual(module.int_const, 1969) self.assertEqual(module.str_const, 'something different')
def test_file_from_empty_string_dir(self): # Loading a module found from an empty string entry on sys.path should # not only work, but keep all attributes relative. file_path = '_temp.py' with open(file_path, 'w') as file: file.write("# test file for importlib") try: with util.uncache('_temp'): loader = machinery.SourceFileLoader('_temp', file_path) mod = loader.load_module('_temp') self.assertEqual(file_path, mod.__file__) self.assertEqual(importlib.util.cache_from_source(file_path), mod.__cached__) finally: os.unlink(file_path) pycache = os.path.dirname( importlib.util.cache_from_source(file_path)) if os.path.exists(pycache): shutil.rmtree(pycache)
def test_reload(self): # Test that a module is reused if already in sys.modules. class FakeLoader: def is_package(self, name): return True @self.module_for_loader def load_module(self, module): return module name = 'a.b.c' module = types.ModuleType('a.b.c') module.__loader__ = 42 module.__package__ = 42 with util.uncache(name): sys.modules[name] = module loader = FakeLoader() returned_module = loader.load_module(name) self.assertIs(returned_module, sys.modules[name]) self.assertEqual(module.__loader__, loader) self.assertEqual(module.__package__, name)
def test_module_reuse(self): with util.uncache(util.EXTENSIONS.name): module1 = self.load_module(util.EXTENSIONS.name) module2 = self.load_module(util.EXTENSIONS.name) self.assertIs(module1, module2)
def test_success(self): name = 'some_mod' with util.uncache(name): with util.import_state(meta_path=[self.FakeMetaFinder]): self.assertEqual((name, None, None), self.util.find_spec(name))
def test_new_module_failure(self): name = 'a.b.c' with util.uncache(name): self.raise_exception(name) self.assertNotIn(name, sys.modules)