summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_support.py
diff options
context:
space:
mode:
authorNick Coghlan <ncoghlan@gmail.com>2008-09-11 12:11:06 (GMT)
committerNick Coghlan <ncoghlan@gmail.com>2008-09-11 12:11:06 (GMT)
commitd2e09383624944add0e670b857c572129d56988e (patch)
treef77e6631636c1d33d9bd5cf1e3e905c571ac9cd9 /Lib/test/test_support.py
parent9fa5a2828c8007dfb678b883d65aecac93e995e4 (diff)
downloadcpython-d2e09383624944add0e670b857c572129d56988e.zip
cpython-d2e09383624944add0e670b857c572129d56988e.tar.gz
cpython-d2e09383624944add0e670b857c572129d56988e.tar.bz2
Issue #3781: Final cleanup of warnings.catch_warnings and its usage in the test suite. Closes issue w.r.t. 2.6 (R: Brett Cannon)
Diffstat (limited to 'Lib/test/test_support.py')
-rw-r--r--Lib/test/test_support.py25
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.