Skip to content

Commit 367cdc1

Browse files
committed
Add test for gh-117178
1 parent 9967b56 commit 367cdc1

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

Lib/test/test_importlib/test_lazy.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,23 @@ def access_module():
178178
# Or multiple load attempts
179179
self.assertEqual(loader.load_count, 1)
180180

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
196+
module.loads
197+
181198

182199
if __name__ == '__main__':
183200
unittest.main()

0 commit comments

Comments
 (0)