Repository navigation
bpo-39336: Allow packages to not let their child modules be set on them #18006
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
Changes from 3 commits
78a9631
862b536
f2cfb2f
245fd87
d17e87a
98dba44
bbf8a94
1808b98
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ | |
| import errno | ||
| import glob | ||
| import importlib.util | ||
| import inspect | ||
| from importlib._bootstrap_external import _get_sourcefile | ||
| import marshal | ||
| import os | ||
|
|
@@ -26,7 +27,7 @@ | |
| temp_dir, DirsOnSysPath) | ||
| from test.support import script_helper | ||
| from test.test_importlib.util import uncache | ||
|
|
||
| from types import ModuleType | ||
|
|
||
| skip_if_dont_write_bytecode = unittest.skipIf( | ||
| sys.dont_write_bytecode, | ||
|
|
@@ -49,6 +50,8 @@ def _ready_to_import(name=None, source=""): | |
| # reverts or removes the module when cleaning up | ||
| name = name or "spam" | ||
| with temp_dir() as tempdir: | ||
| if os.sep in name: | ||
| os.makedirs(os.path.join(tempdir, name.rpartition(os.sep)[0])) | ||
| path = script_helper.make_script(tempdir, name, source) | ||
| old_module = sys.modules.pop(name, None) | ||
| try: | ||
|
|
@@ -1339,6 +1342,23 @@ def test_circular_from_import(self): | |
| str(cm.exception), | ||
| ) | ||
|
|
||
| def test_unwritable_module(self): | ||
| sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'data')) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why the path insertion instead of importing
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was actually just following the pattern I saw in test_concurrency but this is certainly much cleaner! |
||
| try: | ||
| try: | ||
| import unwritable | ||
| with self.assertWarns(ImportWarning): | ||
| from unwritable import x | ||
|
|
||
| self.assertNotEqual(type(unwritable), ModuleType) | ||
| self.assertEqual(type(x), ModuleType) | ||
| with self.assertRaises(AttributeError): | ||
| unwritable.x = 42 | ||
| finally: | ||
| sys.modules['unwritable.x'] | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If this is delete the module then you can use a helper in
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks, will do! |
||
| sys.modules['unwritable'] | ||
| finally: | ||
| del sys.path[0] | ||
|
|
||
| if __name__ == '__main__': | ||
| # Test needs to be a package, so we can do relative imports. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| import sys | ||
|
|
||
| class MyMod(object): | ||
| __slots__ = ['__builtins__', '__cached__', '__doc__', | ||
| '__file__', '__loader__', '__name__', | ||
| '__package__', '__path__', '__spec__'] | ||
| def __init__(self): | ||
| for attr in self.__slots__: | ||
| setattr(self, attr, globals()[attr]) | ||
|
|
||
|
|
||
| sys.modules[__name__] = MyMod() |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Import loaders which publish immutable module objects can now publish immutable packages in addition to individual modules | ||
|
DinoV marked this conversation as resolved.
Outdated
|
||
Uh oh!
There was an error while loading. Please reload this page.