]> granicus.if.org Git - python/commitdiff
Merged revisions 78769,79049 via svnmerge from
authorEzio Melotti <ezio.melotti@gmail.com>
Mon, 2 Aug 2010 22:01:34 +0000 (22:01 +0000)
committerEzio Melotti <ezio.melotti@gmail.com>
Mon, 2 Aug 2010 22:01:34 +0000 (22:01 +0000)
svn+ssh://pythondev@svn.python.org/python/trunk

........
  r78769 | florent.xicluna | 2010-03-07 21:14:12 +0200 (Sun, 07 Mar 2010) | 2 lines

  Refresh the documentation for the test.test_support module.
........
  r79049 | florent.xicluna | 2010-03-18 21:51:47 +0200 (Thu, 18 Mar 2010) | 2 lines

  #8155: Preserve backward compatibility for test_support.check_warnings().  Add regression tests.
........

Lib/test/test_support.py
Lib/test/test_warnings.py

index 69b4d90a1cb3da186b6c0615c379f9c81d27458e..2212fce9912787c5e7b7d207c44d5d543d2d71a7 100644 (file)
@@ -457,11 +457,11 @@ def _filterwarnings(filters, quiet=False):
         if not seen and not quiet:
             # This filter caught nothing
             missing.append((msg, cat.__name__))
-    for exc in reraise:
-        raise AssertionError("unhandled warning %r" % exc)
-    for filter in missing:
-        raise AssertionError("filter (%r, %s) did not caught any warning" %
-                             filter)
+    if reraise:
+        raise AssertionError("unhandled warning %r" % reraise[0])
+    if missing:
+        raise AssertionError("filter (%r, %s) did not catch any warning" %
+                             missing[0])
 
 
 @contextlib.contextmanager
@@ -473,14 +473,19 @@ def check_warnings(*filters, **kwargs):
 
     Optional argument:
      - if 'quiet' is True, it does not fail if a filter catches nothing
-        (default False)
+        (default True without argument,
+         default False if some filters are defined)
 
     Without argument, it defaults to:
-        check_warnings(("", Warning), quiet=False)
+        check_warnings(("", Warning), quiet=True)
     """
+    quiet = kwargs.get('quiet')
     if not filters:
         filters = (("", Warning),)
-    return _filterwarnings(filters, kwargs.get('quiet'))
+        # Preserve backward compatibility
+        if quiet is None:
+            quiet = True
+    return _filterwarnings(filters, quiet)
 
 
 @contextlib.contextmanager
index fab8086700714384cb3594e3be5f1f6a6e75b460..b1e2010fbcd20661cbdeaa103bbe08b848aafe78 100644 (file)
@@ -600,19 +600,33 @@ class CatchWarningTests(BaseTest):
     def test_check_warnings(self):
         # Explicit tests for the test_support convenience wrapper
         wmod = self.module
-        if wmod is sys.modules['warnings']:
-            with test_support.check_warnings() as w:
-                self.assertEqual(w.warnings, [])
-                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")
-                w.reset()
-                self.assertEqual(w.warnings, [])
+        if wmod is not sys.modules['warnings']:
+            return
+        with test_support.check_warnings(quiet=False) as w:
+            self.assertEqual(w.warnings, [])
+            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")
+            w.reset()
+            self.assertEqual(w.warnings, [])
+
+        with test_support.check_warnings():
+            # defaults to quiet=True without argument
+            pass
+        with test_support.check_warnings(('foo', UserWarning)):
+            wmod.warn("foo")
 
+        with self.assertRaises(AssertionError):
+            with test_support.check_warnings(('', RuntimeWarning)):
+                # defaults to quiet=False with argument
+                pass
+        with self.assertRaises(AssertionError):
+            with test_support.check_warnings(('foo', RuntimeWarning)):
+                wmod.warn("foo")
 
 
 class CCatchWarningTests(CatchWarningTests):