mirror of
https://github.com/python/cpython.git
synced 2025-12-08 06:10:17 +00:00
gh-117178: Recover lazy loading of self-referential modules (#117179)
This commit is contained in:
parent
4c71d51a4b
commit
9a1e55b8c5
3 changed files with 25 additions and 6 deletions
|
|
@ -178,6 +178,24 @@ def access_module():
|
|||
# Or multiple load attempts
|
||||
self.assertEqual(loader.load_count, 1)
|
||||
|
||||
def test_lazy_self_referential_modules(self):
|
||||
# Directory modules with submodules that reference the parent can attempt to access
|
||||
# the parent module during a load. Verify that this common pattern works with lazy loading.
|
||||
# json is a good example in the stdlib.
|
||||
json_modules = [name for name in sys.modules if name.startswith('json')]
|
||||
with test_util.uncache(*json_modules):
|
||||
# Standard lazy loading, unwrapped
|
||||
spec = util.find_spec('json')
|
||||
loader = util.LazyLoader(spec.loader)
|
||||
spec.loader = loader
|
||||
module = util.module_from_spec(spec)
|
||||
sys.modules['json'] = module
|
||||
loader.exec_module(module)
|
||||
|
||||
# Trigger load with attribute lookup, ensure expected behavior
|
||||
test_load = module.loads('{}')
|
||||
self.assertEqual(test_load, {})
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue