diff options
Diffstat (limited to 'Lib/test/support.py')
-rw-r--r-- | Lib/test/support.py | 32 |
1 files changed, 21 insertions, 11 deletions
diff --git a/Lib/test/support.py b/Lib/test/support.py index a23c99b..5f8b1ba 100644 --- a/Lib/test/support.py +++ b/Lib/test/support.py @@ -19,7 +19,7 @@ __all__ = ["Error", "TestFailed", "TestSkipped", "ResourceDenied", "import_modul "is_resource_enabled", "requires", "find_unused_port", "bind_port", "fcmp", "is_jython", "TESTFN", "HOST", "FUZZ", "findfile", "verify", "vereq", "sortdict", "check_syntax_error", "open_urlresource", - "catch_warning", "CleanImport", "EnvironmentVarGuard", + "check_warnings", "CleanImport", "EnvironmentVarGuard", "TransientResource", "captured_output", "captured_stdout", "TransientResource", "transient_internet", "run_with_locale", "set_memlimit", "bigmemtest", "bigaddrspacetest", "BasicTestRunner", @@ -53,7 +53,7 @@ class ResourceDenied(TestSkipped): def import_module(name, deprecated=False): """Import the module to be tested, raising TestSkipped if it is not available.""" - with catch_warning(record=False): + with warnings.catch_warnings(): if deprecated: warnings.filterwarnings("ignore", ".+ (module|package)", DeprecationWarning) @@ -368,17 +368,27 @@ def open_urlresource(url, *args, **kw): return open(fn, *args, **kw) -def catch_warning(module=warnings, record=True): - """Guard the warnings filter from being permanently changed and - optionally record the details of any warnings that are issued. +class WarningsRecorder(object): + """Convenience wrapper for the warnings list returned on + entry to the warnings.catch_warnings() context manager. + """ + def __init__(self, warnings_list): + self.warnings = warnings_list - Use like this: + def __getattr__(self, attr): + if self.warnings: + return getattr(self.warnings[-1], attr) + elif attr in warnings.WarningMessage._WARNING_DETAILS: + return None + raise AttributeError("%r has no attribute %r" % (self, attr)) - with catch_warning() as w: - warnings.warn("foo") - assert str(w.message) == "foo" - """ - return warnings.catch_warnings(record=record, module=module) + def reset(self): + del self.warnings[:] + +@contextlib.contextmanager +def check_warnings(): + with warnings.catch_warnings(record=True) as w: + yield WarningsRecorder(w) class CleanImport(object): |