diff options
Diffstat (limited to 'Lib/test/test_support.py')
-rw-r--r-- | Lib/test/test_support.py | 22 |
1 files changed, 18 insertions, 4 deletions
diff --git a/Lib/test/test_support.py b/Lib/test/test_support.py index 431b66b..92592eb 100644 --- a/Lib/test/test_support.py +++ b/Lib/test/test_support.py @@ -37,6 +37,19 @@ class ResourceDenied(TestSkipped): and unexpected skips. """ +def import_module(name, deprecated=False): + """Import the module to be tested, raising TestSkipped if it is not + available.""" + with catch_warning(record=False): + if deprecated: + warnings.filterwarnings("ignore", ".+ module", DeprecationWarning) + try: + module = __import__(name, level=0) + except ImportError: + raise TestSkipped("No module named " + name) + else: + return module + verbose = 1 # Flag set to 0 by regrtest.py use_resources = None # Flag set to [] by regrtest.py max_memuse = 0 # Disable bigmem tests (they will still be run with @@ -373,7 +386,7 @@ class WarningMessage(object): @contextlib.contextmanager -def catch_warning(module=warnings): +def catch_warning(module=warnings, record=True): """ Guard the warnings filter from being permanently changed and record the data of the last warning that has been issued. @@ -384,12 +397,13 @@ def catch_warning(module=warnings): warnings.warn("foo") assert str(w.message) == "foo" """ - warning_obj = WarningMessage() original_filters = module.filters[:] original_showwarning = module.showwarning - module.showwarning = warning_obj._showwarning + if record: + warning_obj = WarningMessage() + module.showwarning = warning_obj._showwarning try: - yield warning_obj + yield warning_obj if record else None finally: module.showwarning = original_showwarning module.filters = original_filters |