File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -90,6 +90,11 @@ def test_e2e(self):
9090 with test_util .import_state (meta_path = [importer ]):
9191 module = importlib .import_module (importer .module_name )
9292 self .assertIsNone (importer .loaded )
93+
94+ # Check that getting __spec__ doesn't trigger the load.
95+ module .__spec__
96+ self .assertIsNone (importer .loaded )
97+
9398 # Trigger load.
9499 self .assertEqual (module .__loader__ , importer )
95100 self .assertIsNotNone (importer .loaded )
@@ -224,6 +229,21 @@ def __delattr__(self, name):
224229 with self .assertRaises (AttributeError ):
225230 del module .CONSTANT
226231
232+ def test_spec_passthrough (self ):
233+ # Verify that a lazily loaded module can have its __spec__ retrieved without triggering
234+ # a full load. That way, importlib internal machinery will not cause a load when pulling
235+ # a lazily loaded module out of sys.modules.
236+ loader = TestingImporter ()
237+ module = self .new_module (loader = loader )
238+
239+ # Access __spec__ without triggering the load.
240+ _ = module .__spec__
241+ self .assertIsNone (loader .loaded )
242+
243+ # Trigger the full load.
244+ _ = module .attr
245+ self .assertIsNotNone (loader .loaded )
246+
227247
228248if __name__ == '__main__' :
229249 unittest .main ()
You can’t perform that action at this time.
0 commit comments