8000 gh-117182: Allow lazily loaded modules to modify their own __class__ by effigies · Pull Request #117185 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-117182: Allow lazily loaded modules to modify their own __class__ #117185

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 9, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
gh-117182: Test interaction between lazy modules and self-modifying m…
…odules
  • Loading branch information
effigies committed Mar 29, 2024
commit 933c6467148dbd73d3a6d93760c82dfa71cfa4dc
28 changes: 28 additions & 0 deletions Lib/test/test_importlib/test_lazy.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,34 @@ def test_lazy_self_referential_modules(self):
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()
0