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

Skip to content

Commit 01ea326

Browse files
committed
Close #12645: Clarify and reformat the documentation of import_fresh_module
2 parents 3ee1140 + ba5517d commit 01ea326

2 files changed

Lines changed: 32 additions & 19 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 & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,8 @@ def import_module(name, deprecated=False, *, required_on=()):
130130
def _save_and_remove_module(name, orig_modules):
131131
"""Helper function to save and remove a module from sys.modules
132132
133-
Raise ImportError if the module can't be imported.
134-
"""
133+
Raise ImportError if the module can't be imported.
134+
"""
135135
# try to import the module and raise an error if it can't be imported
136136
if name not in sys.modules:
137137
__import__(name)
@@ -144,7 +144,8 @@ def _save_and_remove_module(name, orig_modules):
144144
def _save_and_block_module(name, orig_modules):
145145
"""Helper function to save and block a module in sys.modules
146146
147-
Return True if the module was in sys.modules, False otherwise."""
147+
Return True if the module was in sys.modules, False otherwise.
148+
"""
148149
saved = True
149150
try:
150151
orig_modules[name] = sys.modules[name]
@@ -166,18 +167,30 @@ def anticipate_failure(condition):
166167

167168

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

0 commit comments

Comments
 (0)