8000 gh-117182: Test interaction between lazy modules and self-modifying m… · python/cpython@4fb1a22 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4fb1a22

Browse files
committed
gh-117182: Test interaction between lazy modules and self-modifying modules
1 parent 7da3f1b commit 4fb1a22

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

Lib/test/test_importlib/test_lazy.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,34 @@ def test_lazy_self_referential_modules(self):
196196
test_load = module.loads('{}')
197197
self.assertEqual(test_load, {})
198198

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+
199227

200228
if __name__ == '__main__':
201229
unittest.main()

0 commit comments

Comments
 (0)
0