diff options
author | Brett Cannon <brett@python.org> | 2013-05-30 21:31:47 (GMT) |
---|---|---|
committer | Brett Cannon <brett@python.org> | 2013-05-30 21:31:47 (GMT) |
commit | 357c9fb0556e0ec9d440a4874b6af19d7b0bee7b (patch) | |
tree | 9eaf7e79f980770c75961a06ce92a8f647b10dad /Lib | |
parent | 335ab5b66f432ae3713840ed2403a11c368f5406 (diff) | |
download | cpython-357c9fb0556e0ec9d440a4874b6af19d7b0bee7b.zip cpython-357c9fb0556e0ec9d440a4874b6af19d7b0bee7b.tar.gz cpython-357c9fb0556e0ec9d440a4874b6af19d7b0bee7b.tar.bz2 |
Rename importlib.util.ModuleManager to module_to_load so that the name
explains better what the context manager is providing.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/importlib/_bootstrap.py | 11 | ||||
-rw-r--r-- | Lib/importlib/util.py | 2 | ||||
-rw-r--r-- | Lib/test/test_importlib/test_util.py | 14 |
3 files changed, 17 insertions, 10 deletions
diff --git a/Lib/importlib/_bootstrap.py b/Lib/importlib/_bootstrap.py index 739279f..f5b71ba 100644 --- a/Lib/importlib/_bootstrap.py +++ b/Lib/importlib/_bootstrap.py @@ -484,7 +484,8 @@ def _verbose_message(message, *args, verbosity=1): print(message.format(*args), file=sys.stderr) -class ModuleManager: +# Written as a class only because contextlib is not available. +class _ModuleManager: """Context manager which returns the module to be loaded. @@ -516,6 +517,12 @@ class ModuleManager: del sys.modules[self._name] +def module_to_load(name): + """Return a context manager which provides the module object to load.""" + # Hiding _ModuleManager behind a function for better naming. + return _ModuleManager(name) + + def set_package(fxn): """Set __package__ on the returned module.""" def set_package_wrapper(*args, **kwargs): @@ -559,7 +566,7 @@ def module_for_loader(fxn): """ def module_for_loader_wrapper(self, fullname, *args, **kwargs): - with ModuleManager(fullname) as module: + with module_to_load(fullname) as module: module.__loader__ = self try: is_package = self.is_package(fullname) diff --git a/Lib/importlib/util.py b/Lib/importlib/util.py index f817a40..74eef2a 100644 --- a/Lib/importlib/util.py +++ b/Lib/importlib/util.py @@ -1,6 +1,6 @@ """Utility code for constructing importers, etc.""" -from ._bootstrap import ModuleManager +from ._bootstrap import module_to_load from ._bootstrap import module_for_loader from ._bootstrap import set_loader from ._bootstrap import set_package diff --git a/Lib/test/test_importlib/test_util.py b/Lib/test/test_importlib/test_util.py index b3896f8..7646b34 100644 --- a/Lib/test/test_importlib/test_util.py +++ b/Lib/test/test_importlib/test_util.py @@ -7,7 +7,7 @@ import types import unittest -class ModuleManagerTests(unittest.TestCase): +class ModuleToLoadTests(unittest.TestCase): module_name = 'ModuleManagerTest_module' @@ -19,7 +19,7 @@ class ModuleManagerTests(unittest.TestCase): # 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: + with util.module_to_load(self.module_name) as module: self.assertIn(self.module_name, sys.modules) self.assertIs(sys.modules[self.module_name], module) self.assertTrue(module.__initializing__) @@ -28,19 +28,19 @@ class ModuleManagerTests(unittest.TestCase): def test_new_module_failed(self): # Test the module is removed from sys.modules. try: - with util.ModuleManager(self.module_name) as module: + with util.module_to_load(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') + self.fail('importlib.util.module_to_load 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: + with util.module_to_load(self.module_name) as module: self.assertIs(module, created_module) def test_reload_failed(self): @@ -48,12 +48,12 @@ class ModuleManagerTests(unittest.TestCase): created_module = imp.new_module(self.module_name) sys.modules[self.module_name] = created_module try: - with util.ModuleManager(self.module_name) as module: + with util.module_to_load(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') + self.fail('importlib.util.module_to_load swallowed an exception') class ModuleForLoaderTests(unittest.TestCase): |