diff options
Diffstat (limited to 'coverage/misc.py')
-rw-r--r-- | coverage/misc.py | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/coverage/misc.py b/coverage/misc.py index 11dad23e..d11b5ace 100644 --- a/coverage/misc.py +++ b/coverage/misc.py @@ -5,6 +5,7 @@ import errno import hashlib +import importlib import importlib.util import inspect import locale @@ -43,6 +44,24 @@ def isolate_module(mod): os = isolate_module(os) +def import_extra(modname): + """Import a third-party module we need, but might not be installed. + + This also cleans out the module after the import, so that coverage won't + appear to have imported it. This lets the extra package use coverage for + their own tests. + """ + try: + mod = importlib.import_module(modname) + except ImportError: + mod = None + else: + for name in list(sys.modules.keys()): + if name.startswith(modname): + del sys.modules[name] + return mod + + def dummy_decorator_with_args(*args_unused, **kwargs_unused): """Dummy no-op implementation of a decorator with arguments.""" def _decorator(func): |