summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_importlib/test_lazy.py
diff options
context:
space:
mode:
authorChris Markiewicz <effigies@gmail.com>2024-04-09 03:08:48 (GMT)
committerGitHub <noreply@github.com>2024-04-09 03:08:48 (GMT)
commit19a22020676a599e1c92a24f841196645ddd9895 (patch)
treefc0ba69973538310d5f80af21fcd72429de37e69 /Lib/test/test_importlib/test_lazy.py
parentac45766673b181ace8fbafe36c89bde910968f0e (diff)
downloadcpython-19a22020676a599e1c92a24f841196645ddd9895.zip
cpython-19a22020676a599e1c92a24f841196645ddd9895.tar.gz
cpython-19a22020676a599e1c92a24f841196645ddd9895.tar.bz2
gh-117182: Allow lazily loaded modules to modify their own __class__
Diffstat (limited to 'Lib/test/test_importlib/test_lazy.py')
-rw-r--r--Lib/test/test_importlib/test_lazy.py28
1 files changed, 28 insertions, 0 deletions
diff --git a/Lib/test/test_importlib/test_lazy.py b/Lib/test/test_importlib/test_lazy.py
index 4d2cc4e..5c6e030 100644
--- a/Lib/test/test_importlib/test_lazy.py
+++ b/Lib/test/test_importlib/test_lazy.py
@@ -196,6 +196,34 @@ class LazyLoaderTests(unittest.TestCase):
test_load = module.loads('{}')
self.assertEqual(test_load, {})
+ def test_lazy_module_type_override(self):
+ # Verify that lazy loading works with a module that modifies
+ # its __class__ to be a custom type.
+
+ # Example module from PEP 726
+ module = self.new_module(source_code="""\
+import sys
+from types import ModuleType
+
+CONSTANT = 3.14
+
+class ImmutableModule(ModuleType):
+ def __setattr__(self, name, value):
+ raise AttributeError('Read-only attribute!')
+
+ def __delattr__(self, name):
+ raise AttributeError('Read-only attribute!')
+
+sys.modules[__name__].__class__ = ImmutableModule
+""")
+ sys.modules[TestingImporter.module_name] = module
+ self.assertIsInstance(module, util._LazyModule)
+ self.assertEqual(module.CONSTANT, 3.14)
+ with self.assertRaises(AttributeError):
+ module.CONSTANT = 2.71
+ with self.assertRaises(AttributeError):
+ del module.CONSTANT
+
if __name__ == '__main__':
unittest.main()