diff options
Diffstat (limited to 'Lib/test/test_support.py')
-rw-r--r-- | Lib/test/test_support.py | 25 |
1 files changed, 24 insertions, 1 deletions
diff --git a/Lib/test/test_support.py b/Lib/test/test_support.py index 0bf22cf..abaf117 100644 --- a/Lib/test/test_support.py +++ b/Lib/test/test_support.py @@ -18,7 +18,7 @@ __all__ = ["Error", "TestFailed", "TestSkipped", "ResourceDenied", "import_modul "is_resource_enabled", "requires", "find_unused_port", "bind_port", "fcmp", "have_unicode", "is_jython", "TESTFN", "HOST", "FUZZ", "findfile", "verify", "vereq", "sortdict", "check_syntax_error", - "open_urlresource", "CleanImport", + "open_urlresource", "check_warnings", "CleanImport", "EnvironmentVarGuard", "captured_output", "captured_stdout", "TransientResource", "transient_internet", "run_with_locale", "set_memlimit", "bigmemtest", "bigaddrspacetest", @@ -381,6 +381,29 @@ def open_urlresource(url): return open(fn) +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 + + 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)) + + 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): """Context manager to force import to return a new module reference. |