Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ba5517d

Browse files
committed
Issue #12645: Clarify and reformat the documentation of import_fresh_module
1 parent b212291 commit ba5517d

2 files changed

Lines changed: 32 additions & 18 deletions

File tree

Doc/library/test.rst

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ The :mod:`test.support` module defines the following functions:
489489
*fresh* is an iterable of additional module names that are also removed
490490
from the ``sys.modules`` cache before doing the import.
491491

492-
*blocked* is an iterable of module names that are replaced with :const:`0`
492+
*blocked* is an iterable of module names that are replaced with ``None``
493493
in the module cache during the import to ensure that attempts to import
494494
them raise :exc:`ImportError`.
495495

@@ -500,15 +500,15 @@ The :mod:`test.support` module defines the following functions:
500500
Module and package deprecation messages are suppressed during this import
501501
if *deprecated* is ``True``.
502502

503-
This function will raise :exc:`unittest.SkipTest` if the named module
504-
cannot be imported.
503+
This function will raise :exc:`ImportError` if the named module cannot be
504+
imported.
505505

506506
Example use::
507507

508-
# Get copies of the warnings module for testing without
509-
# affecting the version being used by the rest of the test suite
510-
# One copy uses the C implementation, the other is forced to use
511-
# the pure Python fallback implementation
508+
# Get copies of the warnings module for testing without affecting the
509+
# version being used by the rest of the test suite. One copy uses the
510+
# C implementation, the other is forced to use the pure Python fallback
511+
# implementation
512512
py_warnings = import_fresh_module('warnings', blocked=['_warnings'])
513513
c_warnings = import_fresh_module('warnings', fresh=['_warnings'])
514514

Lib/test/support/__init__.py

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,8 @@ def import_module(name, deprecated=False):
124124
def _save_and_remove_module(name, orig_modules):
125125
"""Helper function to save and remove a module from sys.modules
126126
127-
Raise ImportError if the module can't be imported."""
127+
Raise ImportError if the module can't be imported.
128+
"""
128129
# try to import the module and raise an error if it can't be imported
129130
if name not in sys.modules:
130131
__import__(name)
@@ -137,7 +138,8 @@ def _save_and_remove_module(name, orig_modules):
137138
def _save_and_block_module(name, orig_modules):
138139
"""Helper function to save and block a module in sys.modules
139140
140-
Return True if the module was in sys.modules, False otherwise."""
141+
Return True if the module was in sys.modules, False otherwise.
142+
"""
141143
saved = True
142144
try:
143145
orig_modules[name] = sys.modules[name]
@@ -159,18 +161,30 @@ def anticipate_failure(condition):
159161

160162

161163
def import_fresh_module(name, fresh=(), blocked=(), deprecated=False):
162-
"""Imports and returns a module, deliberately bypassing the sys.modules cache
163-
and importing a fresh copy of the module. Once the import is complete,
164-
the sys.modules cache is restored to its original state.
164+
"""Import and return a module, deliberately bypassing sys.modules.
165165
166-
Modules named in fresh are also imported anew if needed by the import.
167-
If one of these modules can't be imported, None is returned.
166+
This function imports and returns a fresh copy of the named Python module
167+
by removing the named module from sys.modules before doing the import.
168+
Note that unlike reload, the original module is not affected by
169+
this operation.
168170
169-
Importing of modules named in blocked is prevented while the fresh import
170-
takes place.
171+
*fresh* is an iterable of additional module names that are also removed
172+
from the sys.modules cache before doing the import.
171173
172-
If deprecated is True, any module or package deprecation messages
173-
will be suppressed."""
174+
*blocked* is an iterable of module names that are replaced with None
175+
in the module cache during the import to ensure that attempts to import
176+
them raise ImportError.
177+
178+
The named module and any modules named in the *fresh* and *blocked*
179+
parameters are saved before starting the import and then reinserted into
180+
sys.modules when the fresh import is complete.
181+
182+
Module and package deprecation messages are suppressed during this import
183+
if *deprecated* is True.
184+
185+
This function will raise ImportError if the named module cannot be
186+
imported.
187+
"""
174188
# NOTE: test_heapq, test_json and test_warnings include extra sanity checks
175189
# to make sure that this utility function is working as expected
176190
with _ignore_deprecated_imports(deprecated):

0 commit comments

Comments
 (0)