diff options
author | Brett Cannon <brett@python.org> | 2013-05-28 17:29:34 -0400 |
---|---|---|
committer | Brett Cannon <brett@python.org> | 2013-05-28 17:29:34 -0400 |
commit | a3687f0d6896673689d8dd5c13e113947f66e921 (patch) | |
tree | 6d14bef5ece4b63451cdf2ca59a81fd2cca5de58 /Lib/test/test_importlib | |
parent | 4dbae881311073eedd6ef290ec206978f530ef98 (diff) | |
download | cpython-git-a3687f0d6896673689d8dd5c13e113947f66e921.tar.gz |
Introduce importlib.util.ModuleManager which is a context manager to
handle providing (and cleaning up if needed) the module to be loaded.
A future commit will use the context manager in
Lib/importlib/_bootstrap.py and thus why the code is placed there
instead of in Lib/importlib/util.py.
Diffstat (limited to 'Lib/test/test_importlib')
-rw-r--r-- | Lib/test/test_importlib/test_util.py | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/Lib/test/test_importlib/test_util.py b/Lib/test/test_importlib/test_util.py index 5f0871997b..b986efd47b 100644 --- a/Lib/test/test_importlib/test_util.py +++ b/Lib/test/test_importlib/test_util.py @@ -2,10 +2,60 @@ from importlib import util from . import util as test_util import imp import sys +from test import support import types import unittest +class ModuleManagerTests(unittest.TestCase): + + module_name = 'ModuleManagerTest_module' + + def setUp(self): + support.unload(self.module_name) + self.addCleanup(support.unload, self.module_name) + + def test_new_module(self): + # Test a new module is created, inserted into sys.modules, has + # __initializing__ set to True after entering the context manager, + # and __initializing__ set to False after exiting. + with util.ModuleManager(self.module_name) as module: + self.assertIn(self.module_name, sys.modules) + self.assertIs(sys.modules[self.module_name], module) + self.assertTrue(module.__initializing__) + self.assertFalse(module.__initializing__) + + def test_new_module_failed(self): + # Test the module is removed from sys.modules. + try: + with util.ModuleManager(self.module_name) as module: + self.assertIn(self.module_name, sys.modules) + raise exception + except Exception: + self.assertNotIn(self.module_name, sys.modules) + else: + self.fail('importlib.util.ModuleManager swallowed an exception') + + def test_reload(self): + # Test that the same module is in sys.modules. + created_module = imp.new_module(self.module_name) + sys.modules[self.module_name] = created_module + with util.ModuleManager(self.module_name) as module: + self.assertIs(module, created_module) + + def test_reload_failed(self): + # Test that the module was left in sys.modules. + created_module = imp.new_module(self.module_name) + sys.modules[self.module_name] = created_module + try: + with util.ModuleManager(self.module_name) as module: + raise Exception + except Exception: + self.assertIn(self.module_name, sys.modules) + else: + self.fail('importlib.util.ModuleManager swallowed an exception') + + class ModuleForLoaderTests(unittest.TestCase): """Tests for importlib.util.module_for_loader.""" |