| 1 |
import contextlib |
| 2 |
import importlib |
| 3 |
import sys |
| 4 |
import warnings |
| 5 |
|
| 6 |
|
| 7 |
@contextlib.contextmanager |
| 8 |
def _ignore_deprecated_imports(ignore=True): |
| 9 |
"""Context manager to suppress package and module deprecation |
| 10 |
warnings when importing them. |
| 11 |
|
| 12 |
If ignore is False, this context manager has no effect. |
| 13 |
""" |
| 14 |
if ignore: |
| 15 |
with warnings.catch_warnings(): |
| 16 |
warnings.filterwarnings("ignore", ".+ (module|package)", |
| 17 |
DeprecationWarning) |
| 18 |
yield |
| 19 |
else: |
| 20 |
yield |
| 21 |
|
| 22 |
|
| 23 |
def _save_and_remove_module(name, orig_modules): |
| 24 |
"""Helper function to save and remove a module from sys.modules |
| 25 |
|
| 26 |
Raise ImportError if the module can't be imported. |
| 27 |
""" |
| 28 |
|
| 29 |
if name not in sys.modules: |
| 30 |
__import__(name) |
| 31 |
del sys.modules[name] |
| 32 |
for modname in list(sys.modules): |
| 33 |
if modname == name or modname.startswith(name + '.'): |
| 34 |
orig_modules[modname] = sys.modules[modname] |
| 35 |
del sys.modules[modname] |
| 36 |
|
| 37 |
def _save_and_block_module(name, orig_modules): |
| 38 |
"""Helper function to save and block a module in sys.modules |
| 39 |
|
| 40 |
Return True if the module was in sys.modules, False otherwise. |
| 41 |
""" |
| 42 |
saved = True |
| 43 |
try: |
| 44 |
orig_modules[name] = sys.modules[name] |
| 45 |
except KeyError: |
| 46 |
saved = False |
| 47 |
sys.modules[name] = None |
| 48 |
return saved |
| 49 |
|
| 50 |
|
| 51 |
def import_fresh_module(name, fresh=(), blocked=(), deprecated=False): |
| 52 |
"""Import and return a module, deliberately bypassing sys.modules. |
| 53 |
|
| 54 |
This function imports and returns a fresh copy of the named Python module |
| 55 |
by removing the named module from sys.modules before doing the import. |
| 56 |
Note that unlike reload, the original module is not affected by |
| 57 |
this operation. |
| 58 |
|
| 59 |
*fresh* is an iterable of additional module names that are also removed |
| 60 |
from the sys.modules cache before doing the import. |
| 61 |
|
| 62 |
*blocked* is an iterable of module names that are replaced with None |
| 63 |
in the module cache during the import to ensure that attempts to import |
| 64 |
them raise ImportError. |
| 65 |
|
| 66 |
The named module and any modules named in the *fresh* and *blocked* |
| 67 |
parameters are saved before starting the import and then reinserted into |
| 68 |
sys.modules when the fresh import is complete. |
| 69 |
|
| 70 |
Module and package deprecation messages are suppressed during this import |
| 71 |
if *deprecated* is True. |
| 72 |
|
| 73 |
This function will raise ImportError if the named module cannot be |
| 74 |
imported. |
| 75 |
""" |
| 76 |
|
| 77 |
|
| 78 |
with _ignore_deprecated_imports(deprecated): |
| 79 |
|
| 80 |
|
| 81 |
orig_modules = {} |
| 82 |
names_to_remove = [] |
| 83 |
_save_and_remove_module(name, orig_modules) |
| 84 |
try: |
| 85 |
for fresh_name in fresh: |
| 86 |
_save_and_remove_module(fresh_name, orig_modules) |
| 87 |
for blocked_name in blocked: |
| 88 |
if not _save_and_block_module(blocked_name, orig_modules): |
| 89 |
names_to_remove.append(blocked_name) |
| 90 |
fresh_module = importlib.import_module(name) |
| 91 |
except ImportError: |
| 92 |
fresh_module = None |
| 93 |
finally: |
| 94 |
for orig_name, module in orig_modules.items(): |
| 95 |
sys.modules[orig_name] = module |
| 96 |
for name_to_remove in names_to_remove: |
| 97 |
del sys.modules[name_to_remove] |
| 98 |
return fresh_module |