def test_load_legacy_attributes(self): self.spec.loader = LegacyLoader() with CleanImport(self.spec.name): loaded = self.bootstrap._load(self.spec) self.assertIs(loaded.__loader__, self.spec.loader) self.assertEqual(loaded.__package__, self.spec.parent) self.assertIs(loaded.__spec__, self.spec)
def test_get_loader_handles_spec_attribute_none(self): name = 'spam' mod = type(sys)(name) mod.__spec__ = None with CleanImport(name): sys.modules[name] = mod loader = pkgutil.get_loader(name) self.assertIsNone(loader)
def test_load(self): self.spec.loader = NewLoader() with CleanImport(self.spec.name): loaded = self.bootstrap._load(self.spec) installed = sys.modules[self.spec.name] self.assertEqual(loaded.eggs, 1) self.assertIs(loaded, installed)
def test_reload_extra_attributes(self): self.spec.loader = NewLoader() with CleanImport(self.spec.name): loaded = self.bootstrap._load(self.spec) loaded.available = False reloaded = self.bootstrap._exec(self.spec, loaded) self.assertFalse(loaded.available) self.assertIs(reloaded, loaded)
def test_reload_modified(self): self.spec.loader = NewLoader() with CleanImport(self.spec.name): loaded = self.bootstrap._load(self.spec) loaded.eggs = 2 reloaded = self.bootstrap._exec(self.spec, loaded) self.assertEqual(loaded.eggs, 1) self.assertIs(reloaded, loaded)
def test_reload_legacy(self): self.spec.loader = LegacyLoader() with CleanImport(self.spec.name): loaded = self.bootstrap._load(self.spec) reloaded = self.bootstrap._exec(self.spec, loaded) installed = sys.modules[self.spec.name] self.assertEqual(loaded.ham, -1) self.assertIs(reloaded, loaded) self.assertIs(installed, loaded)
def test_load_failed(self): class FailedLoader(TestLoader): def exec_module(self, module): raise RuntimeError self.spec.loader = FailedLoader() with CleanImport(self.spec.name): with self.assertRaises(RuntimeError): loaded = self.bootstrap._load(self.spec) self.assertNotIn(self.spec.name, sys.modules)
def test_load_legacy_attributes_immutable(self): module = object() class ImmutableLoader(TestLoader): def load_module(self, name): sys.modules[name] = module return module self.spec.loader = ImmutableLoader() with CleanImport(self.spec.name): loaded = self.bootstrap._load(self.spec) self.assertIs(sys.modules[self.spec.name], module)
def test_load_replaced(self): replacement = object() class ReplacingLoader(TestLoader): def exec_module(self, module): sys.modules[module.__name__] = replacement self.spec.loader = ReplacingLoader() with CleanImport(self.spec.name): loaded = self.bootstrap._load(self.spec) installed = sys.modules[self.spec.name] self.assertIs(loaded, replacement) self.assertIs(installed, replacement)
def test_reload_init_module_attrs(self): self.spec.loader = NewLoader() with CleanImport(self.spec.name): loaded = self.bootstrap._load(self.spec) loaded.__name__ = 'ham' del loaded.__loader__ del loaded.__package__ del loaded.__spec__ self.bootstrap._exec(self.spec, loaded) self.assertEqual(loaded.__name__, self.spec.name) self.assertIs(loaded.__loader__, self.spec.loader) self.assertEqual(loaded.__package__, self.spec.parent) self.assertIs(loaded.__spec__, self.spec) self.assertFalse(hasattr(loaded, '__path__')) self.assertFalse(hasattr(loaded, '__file__')) self.assertFalse(hasattr(loaded, '__cached__'))
def test_load_legacy(self): self.spec.loader = LegacyLoader() with CleanImport(self.spec.name): loaded = self.bootstrap._load(self.spec) self.assertEqual(loaded.ham, -1)