mirror of
https://github.com/python/cpython.git
synced 2025-09-27 02:39:58 +00:00
Rename importlib.util.ModuleManager to module_to_load so that the name
explains better what the context manager is providing.
This commit is contained in:
parent
335ab5b66f
commit
357c9fb055
6 changed files with 3568 additions and 3547 deletions
|
@ -789,12 +789,13 @@ an :term:`importer`.
|
||||||
|
|
||||||
.. versionadded:: 3.3
|
.. versionadded:: 3.3
|
||||||
|
|
||||||
.. class:: ModuleManager(name)
|
.. function:: module_to_load(name)
|
||||||
|
|
||||||
A :term:`context manager` which provides the module to load. The module will
|
Returns a :term:`context manager` which provides the module to load. The
|
||||||
either come from :attr:`sys.modules` in the case of reloading or a fresh
|
module will either come from :attr:`sys.modules` in the case of reloading or
|
||||||
module if loading a new module. Proper cleanup of :attr:`sys.modules` occurs
|
a fresh module if loading a new module. Proper cleanup of
|
||||||
if the module was new and an exception was raised.
|
:attr:`sys.modules` occurs if the module was new and an exception was
|
||||||
|
raised.
|
||||||
|
|
||||||
.. versionadded:: 3.4
|
.. versionadded:: 3.4
|
||||||
|
|
||||||
|
@ -823,7 +824,7 @@ an :term:`importer`.
|
||||||
in :data:`sys.modules` then it is left alone.
|
in :data:`sys.modules` then it is left alone.
|
||||||
|
|
||||||
.. note::
|
.. note::
|
||||||
:class:`ModuleManager` subsumes the module management aspect of this
|
:func:`module_to_load` subsumes the module management aspect of this
|
||||||
decorator.
|
decorator.
|
||||||
|
|
||||||
.. versionchanged:: 3.3
|
.. versionchanged:: 3.3
|
||||||
|
|
|
@ -484,7 +484,8 @@ def _verbose_message(message, *args, verbosity=1):
|
||||||
print(message.format(*args), file=sys.stderr)
|
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.
|
"""Context manager which returns the module to be loaded.
|
||||||
|
|
||||||
|
@ -516,6 +517,12 @@ class ModuleManager:
|
||||||
del sys.modules[self._name]
|
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):
|
def set_package(fxn):
|
||||||
"""Set __package__ on the returned module."""
|
"""Set __package__ on the returned module."""
|
||||||
def set_package_wrapper(*args, **kwargs):
|
def set_package_wrapper(*args, **kwargs):
|
||||||
|
@ -559,7 +566,7 @@ def module_for_loader(fxn):
|
||||||
|
|
||||||
"""
|
"""
|
||||||
def module_for_loader_wrapper(self, fullname, *args, **kwargs):
|
def module_for_loader_wrapper(self, fullname, *args, **kwargs):
|
||||||
with ModuleManager(fullname) as module:
|
with module_to_load(fullname) as module:
|
||||||
module.__loader__ = self
|
module.__loader__ = self
|
||||||
try:
|
try:
|
||||||
is_package = self.is_package(fullname)
|
is_package = self.is_package(fullname)
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
"""Utility code for constructing importers, etc."""
|
"""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 module_for_loader
|
||||||
from ._bootstrap import set_loader
|
from ._bootstrap import set_loader
|
||||||
from ._bootstrap import set_package
|
from ._bootstrap import set_package
|
||||||
|
|
|
@ -7,7 +7,7 @@ import types
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
|
|
||||||
class ModuleManagerTests(unittest.TestCase):
|
class ModuleToLoadTests(unittest.TestCase):
|
||||||
|
|
||||||
module_name = 'ModuleManagerTest_module'
|
module_name = 'ModuleManagerTest_module'
|
||||||
|
|
||||||
|
@ -19,7 +19,7 @@ class ModuleManagerTests(unittest.TestCase):
|
||||||
# Test a new module is created, inserted into sys.modules, has
|
# Test a new module is created, inserted into sys.modules, has
|
||||||
# __initializing__ set to True after entering the context manager,
|
# __initializing__ set to True after entering the context manager,
|
||||||
# and __initializing__ set to False after exiting.
|
# 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.assertIn(self.module_name, sys.modules)
|
||||||
self.assertIs(sys.modules[self.module_name], module)
|
self.assertIs(sys.modules[self.module_name], module)
|
||||||
self.assertTrue(module.__initializing__)
|
self.assertTrue(module.__initializing__)
|
||||||
|
@ -28,19 +28,19 @@ class ModuleManagerTests(unittest.TestCase):
|
||||||
def test_new_module_failed(self):
|
def test_new_module_failed(self):
|
||||||
# Test the module is removed from sys.modules.
|
# Test the module is removed from sys.modules.
|
||||||
try:
|
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)
|
self.assertIn(self.module_name, sys.modules)
|
||||||
raise exception
|
raise exception
|
||||||
except Exception:
|
except Exception:
|
||||||
self.assertNotIn(self.module_name, sys.modules)
|
self.assertNotIn(self.module_name, sys.modules)
|
||||||
else:
|
else:
|
||||||
self.fail('importlib.util.ModuleManager swallowed an exception')
|
self.fail('importlib.util.module_to_load swallowed an exception')
|
||||||
|
|
||||||
def test_reload(self):
|
def test_reload(self):
|
||||||
# Test that the same module is in sys.modules.
|
# Test that the same module is in sys.modules.
|
||||||
created_module = imp.new_module(self.module_name)
|
created_module = imp.new_module(self.module_name)
|
||||||
sys.modules[self.module_name] = created_module
|
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)
|
self.assertIs(module, created_module)
|
||||||
|
|
||||||
def test_reload_failed(self):
|
def test_reload_failed(self):
|
||||||
|
@ -48,12 +48,12 @@ class ModuleManagerTests(unittest.TestCase):
|
||||||
created_module = imp.new_module(self.module_name)
|
created_module = imp.new_module(self.module_name)
|
||||||
sys.modules[self.module_name] = created_module
|
sys.modules[self.module_name] = created_module
|
||||||
try:
|
try:
|
||||||
with util.ModuleManager(self.module_name) as module:
|
with util.module_to_load(self.module_name) as module:
|
||||||
raise Exception
|
raise Exception
|
||||||
except Exception:
|
except Exception:
|
||||||
self.assertIn(self.module_name, sys.modules)
|
self.assertIn(self.module_name, sys.modules)
|
||||||
else:
|
else:
|
||||||
self.fail('importlib.util.ModuleManager swallowed an exception')
|
self.fail('importlib.util.module_to_load swallowed an exception')
|
||||||
|
|
||||||
|
|
||||||
class ModuleForLoaderTests(unittest.TestCase):
|
class ModuleForLoaderTests(unittest.TestCase):
|
||||||
|
|
|
@ -103,8 +103,8 @@ Library
|
||||||
- Issue #18070: Have importlib.util.module_for_loader() set attributes
|
- Issue #18070: Have importlib.util.module_for_loader() set attributes
|
||||||
unconditionally in order to properly support reloading.
|
unconditionally in order to properly support reloading.
|
||||||
|
|
||||||
- Add importlib.util.ModuleManager as a context manager to provide the proper
|
- Add importlib.util.module_to_load to return a context manager to provide the
|
||||||
module object to load.
|
proper module object to load.
|
||||||
|
|
||||||
- Issue #18025: Fixed a segfault in io.BufferedIOBase.readinto() when raw
|
- Issue #18025: Fixed a segfault in io.BufferedIOBase.readinto() when raw
|
||||||
stream's read() returns more bytes than requested.
|
stream's read() returns more bytes than requested.
|
||||||
|
|
7071
Python/importlib.h
7071
Python/importlib.h
File diff suppressed because it is too large
Load diff
Loading…
Add table
Add a link
Reference in a new issue