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