[Python-checkins] cpython (3.3): Issue #12645: Clarify and reformat the documentation of import_fresh_module
eli.bendersky
python-checkins at python.org
Mon Aug 12 00:44:12 CEST 2013
http://hg.python.org/cpython/rev/edaf44136d32
changeset: 85124:edaf44136d32
branch: 3.3
parent: 85122:dab790a17c4d
user: Eli Bendersky <eliben at gmail.com>
date: Sun Aug 11 15:38:08 2013 -0700
summary:
Issue #12645: Clarify and reformat the documentation of import_fresh_module
files:
Doc/library/test.rst | 14 ++++----
Lib/test/support/__init__.py | 36 ++++++++++++++++-------
2 files changed, 32 insertions(+), 18 deletions(-)
diff --git a/Doc/library/test.rst b/Doc/library/test.rst
--- a/Doc/library/test.rst
+++ b/Doc/library/test.rst
@@ -489,7 +489,7 @@
*fresh* is an iterable of additional module names that are also removed
from the ``sys.modules`` cache before doing the import.
- *blocked* is an iterable of module names that are replaced with :const:`0`
+ *blocked* is an iterable of module names that are replaced with ``None``
in the module cache during the import to ensure that attempts to import
them raise :exc:`ImportError`.
@@ -500,15 +500,15 @@
Module and package deprecation messages are suppressed during this import
if *deprecated* is ``True``.
- This function will raise :exc:`unittest.SkipTest` if the named module
- cannot be imported.
+ This function will raise :exc:`ImportError` if the named module cannot be
+ imported.
Example use::
- # Get copies of the warnings module for testing without
- # affecting the version being used by the rest of the test suite
- # One copy uses the C implementation, the other is forced to use
- # the pure Python fallback implementation
+ # Get copies of the warnings module for testing without affecting the
+ # version being used by the rest of the test suite. One copy uses the
+ # C implementation, the other is forced to use the pure Python fallback
+ # implementation
py_warnings = import_fresh_module('warnings', blocked=['_warnings'])
c_warnings = import_fresh_module('warnings', fresh=['_warnings'])
diff --git a/Lib/test/support/__init__.py b/Lib/test/support/__init__.py
--- a/Lib/test/support/__init__.py
+++ b/Lib/test/support/__init__.py
@@ -124,7 +124,8 @@
def _save_and_remove_module(name, orig_modules):
"""Helper function to save and remove a module from sys.modules
- Raise ImportError if the module can't be imported."""
+ Raise ImportError if the module can't be imported.
+ """
# try to import the module and raise an error if it can't be imported
if name not in sys.modules:
__import__(name)
@@ -137,7 +138,8 @@
def _save_and_block_module(name, orig_modules):
"""Helper function to save and block a module in sys.modules
- Return True if the module was in sys.modules, False otherwise."""
+ Return True if the module was in sys.modules, False otherwise.
+ """
saved = True
try:
orig_modules[name] = sys.modules[name]
@@ -159,18 +161,30 @@
def import_fresh_module(name, fresh=(), blocked=(), deprecated=False):
- """Imports and returns a module, deliberately bypassing the sys.modules cache
- and importing a fresh copy of the module. Once the import is complete,
- the sys.modules cache is restored to its original state.
+ """Import and return a module, deliberately bypassing sys.modules.
- Modules named in fresh are also imported anew if needed by the import.
- If one of these modules can't be imported, None is returned.
+ This function imports and returns a fresh copy of the named Python module
+ by removing the named module from sys.modules before doing the import.
+ Note that unlike reload, the original module is not affected by
+ this operation.
- Importing of modules named in blocked is prevented while the fresh import
- takes place.
+ *fresh* is an iterable of additional module names that are also removed
+ from the sys.modules cache before doing the import.
- If deprecated is True, any module or package deprecation messages
- will be suppressed."""
+ *blocked* is an iterable of module names that are replaced with None
+ in the module cache during the import to ensure that attempts to import
+ them raise ImportError.
+
+ The named module and any modules named in the *fresh* and *blocked*
+ parameters are saved before starting the import and then reinserted into
+ sys.modules when the fresh import is complete.
+
+ Module and package deprecation messages are suppressed during this import
+ if *deprecated* is True.
+
+ This function will raise ImportError if the named module cannot be
+ imported.
+ """
# NOTE: test_heapq, test_json and test_warnings include extra sanity checks
# to make sure that this utility function is working as expected
with _ignore_deprecated_imports(deprecated):
--
Repository URL: http://hg.python.org/cpython
More information about the Python-checkins
mailing list