summaryrefslogtreecommitdiffstats
path: root/Lib/importlib/test/test_util.py
diff options
context:
space:
mode:
authorBrett Cannon <brett@python.org>2012-04-27 21:27:14 (GMT)
committerBrett Cannon <brett@python.org>2012-04-27 21:27:14 (GMT)
commitefad00d52041fedbff5d7cfadd163e228b4af519 (patch)
tree33c9cc54a62a114eeaffdcfe3e2b31fa6a0b1166 /Lib/importlib/test/test_util.py
parentfea73efc9ea2a65d73a55f8bab1adfbbca62e38b (diff)
downloadcpython-efad00d52041fedbff5d7cfadd163e228b4af519.zip
cpython-efad00d52041fedbff5d7cfadd163e228b4af519.tar.gz
cpython-efad00d52041fedbff5d7cfadd163e228b4af519.tar.bz2
Issue #14646: __import__() now sets __loader__ if need be.
importlib.util.module_for_loader also will set __loader__ along with __package__. This is in conjunction to a forthcoming update to PEP 302 which will make these two attributes required for loaders to set.
Diffstat (limited to 'Lib/importlib/test/test_util.py')
-rw-r--r--Lib/importlib/test/test_util.py28
1 files changed, 28 insertions, 0 deletions
diff --git a/Lib/importlib/test/test_util.py b/Lib/importlib/test/test_util.py
index b035d65..7963e4f 100644
--- a/Lib/importlib/test/test_util.py
+++ b/Lib/importlib/test/test_util.py
@@ -79,6 +79,34 @@ class ModuleForLoaderTests(unittest.TestCase):
given = self.return_module(name)
self.assertTrue(given is module)
+ def test_attributes_set(self):
+ # __name__, __loader__, and __package__ should be set (when
+ # is_package() is defined; undefined implicitly tested elsewhere).
+ class FakeLoader:
+ def __init__(self, is_package):
+ self._pkg = is_package
+ def is_package(self, name):
+ return self._pkg
+ @util.module_for_loader
+ def load_module(self, module):
+ return module
+
+ name = 'pkg.mod'
+ with test_util.uncache(name):
+ loader = FakeLoader(False)
+ module = loader.load_module(name)
+ self.assertEqual(module.__name__, name)
+ self.assertIs(module.__loader__, loader)
+ self.assertEqual(module.__package__, 'pkg')
+
+ name = 'pkg.sub'
+ with test_util.uncache(name):
+ loader = FakeLoader(True)
+ module = loader.load_module(name)
+ self.assertEqual(module.__name__, name)
+ self.assertIs(module.__loader__, loader)
+ self.assertEqual(module.__package__, name)
+
class SetPackageTests(unittest.TestCase):