diff options
author | Brett Cannon <bcannon@gmail.com> | 2008-09-02 02:46:59 (GMT) |
---|---|---|
committer | Brett Cannon <bcannon@gmail.com> | 2008-09-02 02:46:59 (GMT) |
commit | ec92e181fb912e9dc0935fc10bd61bfd4fb237d7 (patch) | |
tree | 8999cbe9dd96050511ae253f6bfc3f14183c734f /Lib/test | |
parent | 3a2bd7d5c5c4af6e7916823d107e524c7f5aaa5a (diff) | |
download | cpython-ec92e181fb912e9dc0935fc10bd61bfd4fb237d7.zip cpython-ec92e181fb912e9dc0935fc10bd61bfd4fb237d7.tar.gz cpython-ec92e181fb912e9dc0935fc10bd61bfd4fb237d7.tar.bz2 |
Merge in r66135. Doing also required removing a stale DeprecationWarning along
with moving warnings.catch_warnings() over to keyword-only parameters for its
constructor (as documented in the 2.6 docs).
Diffstat (limited to 'Lib/test')
-rw-r--r-- | Lib/test/support.py | 58 | ||||
-rw-r--r-- | Lib/test/test_warnings.py | 54 |
2 files changed, 28 insertions, 84 deletions
diff --git a/Lib/test/support.py b/Lib/test/support.py index 24aff5e..a23c99b 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", - "WarningMessage", "catch_warning", "CleanImport", "EnvironmentVarGuard", + "catch_warning", "CleanImport", "EnvironmentVarGuard", "TransientResource", "captured_output", "captured_stdout", "TransientResource", "transient_internet", "run_with_locale", "set_memlimit", "bigmemtest", "bigaddrspacetest", "BasicTestRunner", @@ -368,47 +368,6 @@ def open_urlresource(url, *args, **kw): return open(fn, *args, **kw) -class WarningMessage(object): - "Holds the result of a single showwarning() call" - _WARNING_DETAILS = "message category filename lineno line".split() - def __init__(self, message, category, filename, lineno, line=None): - for attr in self._WARNING_DETAILS: - setattr(self, attr, locals()[attr]) - self._category_name = category.__name__ if category else None - - def __str__(self): - return ("{message : %r, category : %r, filename : %r, lineno : %s, " - "line : %r}" % (self.message, self._category_name, - self.filename, self.lineno, self.line)) - -class WarningRecorder(object): - "Records the result of any showwarning calls" - def __init__(self): - self.warnings = [] - self._set_last(None) - - def _showwarning(self, message, category, filename, lineno, - file=None, line=None): - wm = WarningMessage(message, category, filename, lineno, line) - self.warnings.append(wm) - self._set_last(wm) - - def _set_last(self, last_warning): - if last_warning is None: - for attr in WarningMessage._WARNING_DETAILS: - setattr(self, attr, None) - else: - for attr in WarningMessage._WARNING_DETAILS: - setattr(self, attr, getattr(last_warning, attr)) - - def reset(self): - self.warnings = [] - self._set_last(None) - - def __str__(self): - return '[%s]' % (', '.join(map(str, self.warnings))) - -@contextlib.contextmanager 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. @@ -419,20 +378,7 @@ def catch_warning(module=warnings, record=True): warnings.warn("foo") assert str(w.message) == "foo" """ - original_filters = module.filters - original_showwarning = module.showwarning - if record: - recorder = WarningRecorder() - module.showwarning = recorder._showwarning - else: - recorder = None - try: - # Replace the filters with a copy of the original - module.filters = module.filters[:] - yield recorder - finally: - module.showwarning = original_showwarning - module.filters = original_filters + return warnings.catch_warnings(record=record, module=module) class CleanImport(object): diff --git a/Lib/test/test_warnings.py b/Lib/test/test_warnings.py index b6634c0..cd408cf 100644 --- a/Lib/test/test_warnings.py +++ b/Lib/test/test_warnings.py @@ -79,20 +79,19 @@ class FilterTests(object): "FilterTests.test_error") def test_ignore(self): - with support.catch_warning(self.module) as w: + with support.catch_warning(module=self.module) as w: self.module.resetwarnings() self.module.filterwarnings("ignore", category=UserWarning) self.module.warn("FilterTests.test_ignore", UserWarning) - self.assert_(not w.message) + self.assertEquals(len(w), 0) def test_always(self): - with support.catch_warning(self.module) as w: + with support.catch_warning(module=self.module) as w: self.module.resetwarnings() self.module.filterwarnings("always", category=UserWarning) message = "FilterTests.test_always" self.module.warn(message, UserWarning) self.assert_(message, w.message) - w.message = None # Reset. self.module.warn(message, UserWarning) self.assert_(w.message, message) @@ -107,7 +106,7 @@ class FilterTests(object): self.assertEquals(w.message, message) w.reset() elif x == 1: - self.assert_(not w.message, "unexpected warning: " + str(w)) + self.assert_(not len(w), "unexpected warning: " + str(w)) else: raise ValueError("loop variant unhandled") @@ -120,7 +119,7 @@ class FilterTests(object): self.assertEquals(w.message, message) w.reset() self.module.warn(message, UserWarning) - self.assert_(not w.message, "unexpected message: " + str(w)) + self.assert_(not len(w), "unexpected message: " + str(w)) def test_once(self): with support.catch_warning(self.module) as w: @@ -133,10 +132,10 @@ class FilterTests(object): w.reset() self.module.warn_explicit(message, UserWarning, "test_warnings.py", 13) - self.assert_(not w.message) + self.assertEquals(len(w), 0) self.module.warn_explicit(message, UserWarning, "test_warnings2.py", 42) - self.assert_(not w.message) + self.assertEquals(len(w), 0) def test_inheritance(self): with support.catch_warning(self.module) as w: @@ -156,7 +155,7 @@ class FilterTests(object): self.module.warn("FilterTests.test_ordering", UserWarning) except UserWarning: self.fail("order handling for actions failed") - self.assert_(not w.message) + self.assertEquals(len(w), 0) def test_filterwarnings(self): # Test filterwarnings(). @@ -317,7 +316,6 @@ class WarnTests(unittest.TestCase): None, Warning, None, 1, registry=42) - class CWarnTests(BaseTest, WarnTests): module = c_warnings @@ -377,7 +375,7 @@ class _WarningsTests(BaseTest): self.failUnlessEqual(w.message, message) w.reset() self.module.warn_explicit(message, UserWarning, "file", 42) - self.assert_(not w.message) + self.assertEquals(len(w), 0) # Test the resetting of onceregistry. self.module.onceregistry = {} __warningregistry__ = {} @@ -388,7 +386,7 @@ class _WarningsTests(BaseTest): del self.module.onceregistry __warningregistry__ = {} self.module.warn_explicit(message, UserWarning, "file", 42) - self.failUnless(not w.message) + self.assertEquals(len(w), 0) finally: self.module.onceregistry = original_registry @@ -487,45 +485,45 @@ class CWarningsDisplayTests(BaseTest, WarningsDisplayTests): class PyWarningsDisplayTests(BaseTest, WarningsDisplayTests): module = py_warnings -class WarningsSupportTests(object): - """Test the warning tools from test support module""" +class CatchWarningTests(BaseTest): - def test_catch_warning_restore(self): + """Test catch_warnings().""" + + def test_catch_warnings_restore(self): wmod = self.module orig_filters = wmod.filters orig_showwarning = wmod.showwarning - with support.catch_warning(wmod): + with support.catch_warning(module=wmod): wmod.filters = wmod.showwarning = object() self.assert_(wmod.filters is orig_filters) self.assert_(wmod.showwarning is orig_showwarning) - with support.catch_warning(wmod, record=False): + with support.catch_warning(module=wmod, record=False): wmod.filters = wmod.showwarning = object() self.assert_(wmod.filters is orig_filters) self.assert_(wmod.showwarning is orig_showwarning) - def test_catch_warning_recording(self): + def test_catch_warnings_recording(self): wmod = self.module - with support.catch_warning(wmod) as w: - self.assertEqual(w.warnings, []) + with support.catch_warning(module=wmod) as w: + self.assertEqual(w, []) wmod.simplefilter("always") wmod.warn("foo") self.assertEqual(str(w.message), "foo") wmod.warn("bar") self.assertEqual(str(w.message), "bar") - self.assertEqual(str(w.warnings[0].message), "foo") - self.assertEqual(str(w.warnings[1].message), "bar") + self.assertEqual(str(w[0].message), "foo") + self.assertEqual(str(w[1].message), "bar") w.reset() - self.assertEqual(w.warnings, []) + self.assertEqual(w, []) orig_showwarning = wmod.showwarning - with support.catch_warning(wmod, record=False) as w: + with support.catch_warning(module=wmod, record=False) as w: self.assert_(w is None) self.assert_(wmod.showwarning is orig_showwarning) - -class CWarningsSupportTests(BaseTest, WarningsSupportTests): +class CCatchWarningTests(CatchWarningTests): module = c_warnings -class PyWarningsSupportTests(BaseTest, WarningsSupportTests): +class PyCatchWarningTests(CatchWarningTests): module = py_warnings @@ -539,7 +537,7 @@ def test_main(): CWCmdLineTests, PyWCmdLineTests, _WarningsTests, CWarningsDisplayTests, PyWarningsDisplayTests, - CWarningsSupportTests, PyWarningsSupportTests, + CCatchWarningTests, PyCatchWarningTests, ) |