|
| 1 | +import importlib |
| 2 | +from importlib import abc |
| 3 | +from importlib import util |
| 4 | +import unittest |
| 5 | + |
| 6 | +from . import util as test_util |
| 7 | + |
| 8 | + |
| 9 | +class CollectInit: |
| 10 | + |
| 11 | + def __init__(self, *args, **kwargs): |
| 12 | + self.args = args |
| 13 | + self.kwargs = kwargs |
| 14 | + |
| 15 | + def exec_module(self, module): |
| 16 | + return self |
| 17 | + |
| 18 | + |
| 19 | +class LazyLoaderFactoryTests(unittest.TestCase): |
| 20 | + |
| 21 | + def test_init(self): |
| 22 | + factory = util.LazyLoader.factory(CollectInit) |
| 23 | + # E.g. what importlib.machinery.FileFinder instantiates loaders with |
| 24 | + # plus keyword arguments. |
| 25 | + lazy_loader = factory('module name', 'module path', kw='kw') |
| 26 | + loader = lazy_loader.loader |
| 27 | + self.assertEqual(('module name', 'module path'), loader.args) |
| 28 | + self.assertEqual({'kw': 'kw'}, loader.kwargs) |
| 29 | + |
| 30 | + def test_validation(self): |
| 31 | + # No exec_module(), no lazy loading. |
| 32 | + with self.assertRaises(TypeError): |
| 33 | + util.LazyLoader.factory(object) |
| 34 | + |
| 35 | + |
| 36 | +class TestingImporter(abc.MetaPathFinder, abc.Loader): |
| 37 | + |
| 38 | + module_name = 'lazy_loader_test' |
| 39 | + mutated_name = 'changed' |
| 40 | + loaded = None |
| 41 | + source_code = 'attr = 42; __name__ = {!r}'.format(mutated_name) |
| 42 | + |
| 43 | + def find_spec(self, name, path, target=None): |
| 44 | + if name != self.module_name: |
| 45 | + return None |
| 46 | + return util.spec_from_loader(name, util.LazyLoader(self)) |
| 47 | + |
| 48 | + def exec_module(self, module): |
| 49 | + exec(self.source_code, module.__dict__) |
| 50 | + self.loaded = module |
| 51 | + |
| 52 | + |
| 53 | +class LazyLoaderTests(unittest.TestCase): |
| 54 | + |
| 55 | + def test_init(self): |
| 56 | + with self.assertRaises(TypeError): |
| 57 | + util.LazyLoader(object) |
| 58 | + |
| 59 | + def new_module(self, source_code=None): |
| 60 | + loader = TestingImporter() |
| 61 | + if source_code is not None: |
| 62 | + loader.source_code = source_code |
| 63 | + spec = util.spec_from_loader(TestingImporter.module_name, |
| 64 | + util.LazyLoader(loader)) |
| 65 | + module = spec.loader.create_module(spec) |
| 66 | + module.__spec__ = spec |
| 67 | + module.__loader__ = spec.loader |
| 68 | + spec.loader.exec_module(module) |
| 69 | + # Module is now lazy. |
| 70 | + self.assertIsNone(loader.loaded) |
| 71 | + return module |
| 72 | + |
| 73 | + def test_e2e(self): |
| 74 | + # End-to-end test to verify the load is in fact lazy. |
| 75 | + importer = TestingImporter() |
| 76 | + assert importer.loaded is None |
| 77 | + with test_util.uncache(importer.module_name): |
| 78 | + with test_util.import_state(meta_path=[importer]): |
| 79 | + module = importlib.import_module(importer.module_name) |
| 80 | + self.assertIsNone(importer.loaded) |
| 81 | + # Trigger load. |
| 82 | + self.assertEqual(module.__loader__, importer) |
| 83 | + self.assertIsNotNone(importer.loaded) |
| 84 | + self.assertEqual(module, importer.loaded) |
| 85 | + |
| 86 | + def test_attr_unchanged(self): |
| 87 | + # An attribute only mutated as a side-effect of import should not be |
| 88 | + # changed needlessly. |
| 89 | + module = self.new_module() |
| 90 | + self.assertEqual(TestingImporter.mutated_name, module.__name__) |
| 91 | + |
| 92 | + def test_new_attr(self): |
| 93 | + # A new attribute should persist. |
| 94 | + module = self.new_module() |
| 95 | + module.new_attr = 42 |
| 96 | + self.assertEqual(42, module.new_attr) |
| 97 | + |
| 98 | + def test_mutated_preexisting_attr(self): |
| 99 | + # Changing an attribute that already existed on the module -- |
| 100 | + # e.g. __name__ -- should persist. |
| 101 | + module = self.new_module() |
| 102 | + module.__name__ = 'bogus' |
| 103 | + self.assertEqual('bogus', module.__name__) |
| 104 | + |
| 105 | + def test_mutated_attr(self): |
| 106 | + # Changing an attribute that comes into existence after an import |
| 107 | + # should persist. |
| 108 | + module = self.new_module() |
| 109 | + module.attr = 6 |
| 110 | + self.assertEqual(6, module.attr) |
| 111 | + |
| 112 | + def test_delete_eventual_attr(self): |
| 113 | + # Deleting an attribute should stay deleted. |
| 114 | + module = self.new_module() |
| 115 | + del module.attr |
| 116 | + self.assertFalse(hasattr(module, 'attr')) |
| 117 | + |
| 118 | + def test_delete_preexisting_attr(self): |
| 119 | + module = self.new_module() |
| 120 | + del module.__name__ |
| 121 | + self.assertFalse(hasattr(module, '__name__')) |
| 122 | + |
| 123 | + def test_module_substitution_error(self): |
| 124 | + source_code = 'import sys; sys.modules[__name__] = 42' |
| 125 | + module = self.new_module(source_code) |
| 126 | + with test_util.uncache(TestingImporter.module_name): |
| 127 | + with self.assertRaises(ValueError): |
| 128 | + module.__name__ |
| 129 | + |
| 130 | + |
| 131 | +if __name__ == '__main__': |
| 132 | + unittest.main() |
0 commit comments