diff options
author | Dino Viehland <dinoviehland@gmail.com> | 2020-01-23 00:42:38 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-01-23 00:42:38 (GMT) |
commit | 9b6fec46513006d7b06fcb645cca6e4f5bf7c7b8 (patch) | |
tree | d159d657c67c4f4ef7a961a5e73a119644de8d3e /Lib/test/test_import | |
parent | d3ae95e1e945ed20297e1c38ba43a18b7a868ab6 (diff) | |
download | cpython-9b6fec46513006d7b06fcb645cca6e4f5bf7c7b8.zip cpython-9b6fec46513006d7b06fcb645cca6e4f5bf7c7b8.tar.gz cpython-9b6fec46513006d7b06fcb645cca6e4f5bf7c7b8.tar.bz2 |
bpo-39336: Allow packages to not let their child modules be set on them (#18006)
* bpo-39336: Allow setattr to fail on modules which aren't assignable
When attaching a child module to a package if the object in sys.modules raises an AttributeError (e.g. because it is immutable) it causes the whole import to fail. This now allows immutable packages to exist and an ImportWarning is reported and the AttributeError exception is ignored.
Diffstat (limited to 'Lib/test/test_import')
-rw-r--r-- | Lib/test/test_import/__init__.py | 14 | ||||
-rw-r--r-- | Lib/test/test_import/data/unwritable/__init__.py | 12 | ||||
-rw-r--r-- | Lib/test/test_import/data/unwritable/x.py | 0 |
3 files changed, 26 insertions, 0 deletions
diff --git a/Lib/test/test_import/__init__.py b/Lib/test/test_import/__init__.py index 13d0481..482fe6a 100644 --- a/Lib/test/test_import/__init__.py +++ b/Lib/test/test_import/__init__.py @@ -26,6 +26,7 @@ from test.support import ( 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( @@ -1339,6 +1340,19 @@ class CircularImportTests(unittest.TestCase): str(cm.exception), ) + def test_unwritable_module(self): + self.addCleanup(unload, "test.test_import.data.unwritable") + self.addCleanup(unload, "test.test_import.data.unwritable.x") + + import test.test_import.data.unwritable as unwritable + with self.assertWarns(ImportWarning): + from test.test_import.data.unwritable import x + + self.assertNotEqual(type(unwritable), ModuleType) + self.assertEqual(type(x), ModuleType) + with self.assertRaises(AttributeError): + unwritable.x = 42 + if __name__ == '__main__': # Test needs to be a package, so we can do relative imports. diff --git a/Lib/test/test_import/data/unwritable/__init__.py b/Lib/test/test_import/data/unwritable/__init__.py new file mode 100644 index 0000000..da4ddb3 --- /dev/null +++ b/Lib/test/test_import/data/unwritable/__init__.py @@ -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() diff --git a/Lib/test/test_import/data/unwritable/x.py b/Lib/test/test_import/data/unwritable/x.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/Lib/test/test_import/data/unwritable/x.py |