diff options
author | Benjamin Peterson <benjamin@python.org> | 2008-10-16 23:24:44 (GMT) |
---|---|---|
committer | Benjamin Peterson <benjamin@python.org> | 2008-10-16 23:24:44 (GMT) |
commit | fcf5d639f508b5a7ebf42d858390a6bd3bbb2c61 (patch) | |
tree | 193a1dd1d41e2403c61cd021409c28b843760cc6 /Lib/test/support.py | |
parent | d31fdc547b1805516f6013af672e43cc66bd8c22 (diff) | |
download | cpython-fcf5d639f508b5a7ebf42d858390a6bd3bbb2c61.zip cpython-fcf5d639f508b5a7ebf42d858390a6bd3bbb2c61.tar.gz cpython-fcf5d639f508b5a7ebf42d858390a6bd3bbb2c61.tar.bz2 |
forward port r66386
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): |