Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
ea202a3f
Commit
ea202a3f
authored
Aug 11, 2013
by
Eli Bendersky
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue #12645: Clarify and reformat the documentation of import_fresh_module
parent
679ca6bc
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
32 additions
and
18 deletions
+32
-18
Doc/library/test.rst
Doc/library/test.rst
+7
-7
Lib/test/support/__init__.py
Lib/test/support/__init__.py
+25
-11
No files found.
Doc/library/test.rst
View file @
ea202a3f
...
@@ -489,7 +489,7 @@ The :mod:`test.support` module defines the following functions:
...
@@ -489,7 +489,7 @@ The :mod:`test.support` module defines the following functions:
*fresh* is an iterable of additional module names that are also removed
*fresh* is an iterable of additional module names that are also removed
from the ``sys.modules`` cache before doing the import.
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
in the module cache during the import to ensure that attempts to import
them raise :exc:`ImportError`.
them raise :exc:`ImportError`.
...
@@ -500,15 +500,15 @@ The :mod:`test.support` module defines the following functions:
...
@@ -500,15 +500,15 @@ The :mod:`test.support` module defines the following functions:
Module and package deprecation messages are suppressed during this import
Module and package deprecation messages are suppressed during this import
if *deprecated* is ``True``.
if *deprecated* is ``True``.
This function will raise :exc:`
unittest.SkipTest` if the named modul
e
This function will raise :exc:`
ImportError` if the named module cannot b
e
cannot be
imported.
imported.
Example use::
Example use::
# Get copies of the warnings module for testing without
# Get copies of the warnings module for testing without
affecting the
#
affecting the version being used by the rest of the test suit
e
#
version being used by the rest of the test suite. One copy uses th
e
#
One copy uses the C implementation, the other is forced to use
#
C implementation, the other is forced to use the pure Python fallback
#
the pure Python fallback
implementation
# implementation
py_warnings = import_fresh_module('warnings', blocked=['_warnings'])
py_warnings = import_fresh_module('warnings', blocked=['_warnings'])
c_warnings = import_fresh_module('warnings', fresh=['_warnings'])
c_warnings = import_fresh_module('warnings', fresh=['_warnings'])
...
...
Lib/test/support/__init__.py
View file @
ea202a3f
...
@@ -124,7 +124,8 @@ def import_module(name, deprecated=False):
...
@@ -124,7 +124,8 @@ def import_module(name, deprecated=False):
def
_save_and_remove_module
(
name
,
orig_modules
):
def
_save_and_remove_module
(
name
,
orig_modules
):
"""Helper function to save and remove a module from sys.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
# try to import the module and raise an error if it can't be imported
if
name
not
in
sys
.
modules
:
if
name
not
in
sys
.
modules
:
__import__
(
name
)
__import__
(
name
)
...
@@ -137,7 +138,8 @@ def _save_and_remove_module(name, orig_modules):
...
@@ -137,7 +138,8 @@ def _save_and_remove_module(name, orig_modules):
def
_save_and_block_module
(
name
,
orig_modules
):
def
_save_and_block_module
(
name
,
orig_modules
):
"""Helper function to save and block a module in sys.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
saved
=
True
try
:
try
:
orig_modules
[
name
]
=
sys
.
modules
[
name
]
orig_modules
[
name
]
=
sys
.
modules
[
name
]
...
@@ -159,18 +161,30 @@ def anticipate_failure(condition):
...
@@ -159,18 +161,30 @@ def anticipate_failure(condition):
def
import_fresh_module
(
name
,
fresh
=
(),
blocked
=
(),
deprecated
=
False
):
def
import_fresh_module
(
name
,
fresh
=
(),
blocked
=
(),
deprecated
=
False
):
"""Imports and returns a module, deliberately bypassing the sys.modules cache
"""Import and return a module, deliberately bypassing sys.modules.
and importing a fresh copy of the module. Once the import is complete,
the sys.modules cache is restored to its original state.
Modules named in fresh are also imported anew if needed by the import.
This function imports and returns a fresh copy of the named Python module
If one of these modules can't be imported, None is returned.
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
*fresh* is an iterable of additional module names that are also removed
takes place
.
from the sys.modules cache before doing the import
.
If deprecated is True, any module or package deprecation messages
*blocked* is an iterable of module names that are replaced with None
will be suppressed."""
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
# NOTE: test_heapq, test_json and test_warnings include extra sanity checks
# to make sure that this utility function is working as expected
# to make sure that this utility function is working as expected
with
_ignore_deprecated_imports
(
deprecated
):
with
_ignore_deprecated_imports
(
deprecated
):
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment