Debian Codegraph @

Lib/test/test_importlib/test_lazy.py

raw
1 import importlib
2 from importlib import abc
3 from importlib import util
4 import sys
5 import time
6 import threading
7 import types
8 import unittest
9
10 from test.support import threading_helper
11 from test.test_importlib import util as test_util
12
13
14 class CollectInit:
15
16 def __init__(self, *args, **kwargs):
17 self.args = args
18 self.kwargs = kwargs
19
20 def exec_module(self, module):
21 return self
22
23
24 class LazyLoaderFactoryTests(unittest.TestCase):
25
26 def test_init(self):
27 factory = util.LazyLoader.factory(CollectInit)
28 # E.g. what importlib.machinery.FileFinder instantiates loaders with
29 # plus keyword arguments.
30 lazy_loader = factory('module name', 'module path', kw='kw')
31 loader = lazy_loader.loader
32 self.assertEqual(('module name', 'module path'), loader.args)
33 self.assertEqual({'kw': 'kw'}, loader.kwargs)
34
35 def test_validation(self):
36 # No exec_module(), no lazy loading.
37 with self.assertRaises(TypeError):
38 util.LazyLoader.factory(object)
39
40
41 class TestingImporter(abc.MetaPathFinder, abc.Loader):
42
43 module_name = 'lazy_loader_test'
44 mutated_name = 'changed'
45 loaded = None
46 load_count = 0
47 source_code = 'attr = 42; __name__ = {!r}'.format(mutated_name)
48
49 def find_spec(self, name, path, target=None):
50 if name != self.module_name:
51 return None
52 return util.spec_from_loader(name, util.LazyLoader(self))
53
54 def exec_module(self, module):
55 time.sleep(0.01) # Simulate a slow load.
56 exec(self.source_code, module.__dict__)
57 self.loaded = module
58 self.load_count += 1
59
60
61 class LazyLoaderTests(unittest.TestCase):
62
63 def test_init(self):
64 with self.assertRaises(TypeError):
65 # Classes that don't define exec_module() trigger TypeError.
66 util.LazyLoader(object)
67
68 def new_module(self, source_code=None, loader=None):
69 if loader is None:
70 loader = TestingImporter()
71 if source_code is not None:
72 loader.source_code = source_code
73 spec = util.spec_from_loader(TestingImporter.module_name,
74 util.LazyLoader(loader))
75 module = spec.loader.create_module(spec)
76 if module is None:
77 module = types.ModuleType(TestingImporter.module_name)
78 module.__spec__ = spec
79 module.__loader__ = spec.loader
80 spec.loader.exec_module(module)
81 # Module is now lazy.
82 self.assertIsNone(loader.loaded)
83 return module
84
85 def test_e2e(self):
86 # End-to-end test to verify the load is in fact lazy.
87 importer = TestingImporter()
88 assert importer.loaded is None
89 with test_util.uncache(importer.module_name):
90 with test_util.import_state(meta_path=[importer]):
91 module = importlib.import_module(importer.module_name)
92 self.assertIsNone(importer.loaded)
93 # Trigger load.
94 self.assertEqual(module.__loader__, importer)
95 self.assertIsNotNone(importer.loaded)
96 self.assertEqual(module, importer.loaded)
97
98 def test_attr_unchanged(self):
99 # An attribute only mutated as a side-effect of import should not be
100 # changed needlessly.
101 module = self.new_module()
102 self.assertEqual(TestingImporter.mutated_name, module.__name__)
103
104 def test_new_attr(self):
105 # A new attribute should persist.
106 module = self.new_module()
107 module.new_attr = 42
108 self.assertEqual(42, module.new_attr)
109
110 def test_mutated_preexisting_attr(self):
111 # Changing an attribute that already existed on the module --
112 # e.g. __name__ -- should persist.
113 module = self.new_module()
114 module.__name__ = 'bogus'
115 self.assertEqual('bogus', module.__name__)
116
117 def test_mutated_attr(self):
118 # Changing an attribute that comes into existence after an import
119 # should persist.
120 module = self.new_module()
121 module.attr = 6
122 self.assertEqual(6, module.attr)
123
124 def test_delete_eventual_attr(self):
125 # Deleting an attribute should stay deleted.
126 module = self.new_module()
127 del module.attr
128 self.assertNotHasAttr(module, 'attr')
129
130 def test_delete_preexisting_attr(self):
131 module = self.new_module()
132 del module.__name__
133 self.assertNotHasAttr(module, '__name__')
134
135 def test_module_substitution_error(self):
136 with test_util.uncache(TestingImporter.module_name):
137 fresh_module = types.ModuleType(TestingImporter.module_name)
138 sys.modules[TestingImporter.module_name] = fresh_module
139 module = self.new_module()
140 with self.assertRaisesRegex(ValueError, "substituted"):
141 module.__name__
142
143 def test_module_already_in_sys(self):
144 with test_util.uncache(TestingImporter.module_name):
145 module = self.new_module()
146 sys.modules[TestingImporter.module_name] = module
147 # Force the load; just care that no exception is raised.
148 module.__name__
149
150 @threading_helper.requires_working_threading()
151 def test_module_load_race(self):
152 with test_util.uncache(TestingImporter.module_name):
153 loader = TestingImporter()
154 module = self.new_module(loader=loader)
155 self.assertEqual(loader.load_count, 0)
156
157 class RaisingThread(threading.Thread):
158 exc = None
159 def run(self):
160 try:
161 super().run()
162 except Exception as exc:
163 self.exc = exc
164
165 def access_module():
166 return module.attr
167
168 threads = []
169 for _ in range(2):
170 threads.append(thread := RaisingThread(target=access_module))
171 thread.start()
172
173 # Races could cause errors
174 for thread in threads:
175 thread.join()
176 self.assertIsNone(thread.exc)
177
178 # Or multiple load attempts
179 self.assertEqual(loader.load_count, 1)
180
181 def test_lazy_self_referential_modules(self):
182 # Directory modules with submodules that reference the parent can attempt to access
183 # the parent module during a load. Verify that this common pattern works with lazy loading.
184 # json is a good example in the stdlib.
185 json_modules = [name for name in sys.modules if name.startswith('json')]
186 with test_util.uncache(*json_modules):
187 # Standard lazy loading, unwrapped
188 spec = util.find_spec('json')
189 loader = util.LazyLoader(spec.loader)
190 spec.loader = loader
191 module = util.module_from_spec(spec)
192 sys.modules['json'] = module
193 loader.exec_module(module)
194
195 # Trigger load with attribute lookup, ensure expected behavior
196 test_load = module.loads('{}')
197 self.assertEqual(test_load, {})
198
199 def test_lazy_module_type_override(self):
200 # Verify that lazy loading works with a module that modifies
201 # its __class__ to be a custom type.
202
203 # Example module from PEP 726
204 module = self.new_module(source_code="""\
205 import sys
206 from types import ModuleType
207
208 CONSTANT = 3.14
209
210 class ImmutableModule(ModuleType):
211 def __setattr__(self, name, value):
212 raise AttributeError('Read-only attribute!')
213
214 def __delattr__(self, name):
215 raise AttributeError('Read-only attribute!')
216
217 sys.modules[__name__].__class__ = ImmutableModule
218 """)
219 sys.modules[TestingImporter.module_name] = module
220 self.assertIsInstance(module, util._LazyModule)
221 self.assertEqual(module.CONSTANT, 3.14)
222 with self.assertRaises(AttributeError):
223 module.CONSTANT = 2.71
224 with self.assertRaises(AttributeError):
225 del module.CONSTANT
226
227
228 if __name__ == '__main__':
229 unittest.main()