[Python-checkins] r83537 - in python/branches/release26-maint: Lib/test/string_tests.py Lib/test/test_fileio.py Lib/test/test_index.py Lib/test/test_random.py Lib/test/test_support.py Lib/test/test_unicode.py Misc/NEWS

ezio.melotti python-checkins at python.org
Mon Aug 2 20:10:10 CEST 2010


Author: ezio.melotti
Date: Mon Aug  2 20:10:09 2010
New Revision: 83537

Log:
Merged revisions 78758 via svnmerge from 
svn+ssh://pythondev@svn.python.org/python/trunk

........
  r78758 | florent.xicluna | 2010-03-07 14:18:33 +0200 (Sun, 07 Mar 2010) | 4 lines
  
  Issue #7849: Now the utility ``check_warnings`` verifies if the warnings are
  effectively raised.  A new utility ``check_py3k_warnings`` deals with py3k warnings.
........


Modified:
   python/branches/release26-maint/   (props changed)
   python/branches/release26-maint/Lib/test/string_tests.py
   python/branches/release26-maint/Lib/test/test_fileio.py
   python/branches/release26-maint/Lib/test/test_index.py
   python/branches/release26-maint/Lib/test/test_random.py
   python/branches/release26-maint/Lib/test/test_support.py
   python/branches/release26-maint/Lib/test/test_unicode.py
   python/branches/release26-maint/Misc/NEWS

Modified: python/branches/release26-maint/Lib/test/string_tests.py
==============================================================================
--- python/branches/release26-maint/Lib/test/string_tests.py	(original)
+++ python/branches/release26-maint/Lib/test/string_tests.py	Mon Aug  2 20:10:09 2010
@@ -686,8 +686,9 @@
         EQ("bobobXbobob", "bobobobXbobobob", "replace", "bobob", "bob")
         EQ("BOBOBOB", "BOBOBOB", "replace", "bob", "bobby")
 
-        ba = buffer('a')
-        bb = buffer('b')
+        with test_support._check_py3k_warnings():
+            ba = buffer('a')
+            bb = buffer('b')
         EQ("bbc", "abc", "replace", ba, bb)
         EQ("aac", "abc", "replace", bb, ba)
 

Modified: python/branches/release26-maint/Lib/test/test_fileio.py
==============================================================================
--- python/branches/release26-maint/Lib/test/test_fileio.py	(original)
+++ python/branches/release26-maint/Lib/test/test_fileio.py	Mon Aug  2 20:10:09 2010
@@ -259,7 +259,7 @@
         self.assertRaises(TypeError, _fileio._FileIO, "1", 0, 0)
 
     def testWarnings(self):
-        with check_warnings() as w:
+        with check_warnings(quiet=True) as w:
             self.assertEqual(w.warnings, [])
             self.assertRaises(TypeError, _fileio._FileIO, [])
             self.assertEqual(w.warnings, [])

Modified: python/branches/release26-maint/Lib/test/test_index.py
==============================================================================
--- python/branches/release26-maint/Lib/test/test_index.py	(original)
+++ python/branches/release26-maint/Lib/test/test_index.py	Mon Aug  2 20:10:09 2010
@@ -195,8 +195,9 @@
         x = GetItem()
         self.assertEqual(x[self.pos], self.pos)
         self.assertEqual(x[self.neg], self.neg)
-        self.assertEqual(x[self.neg:self.pos], (maxint+minsize, maxsize))
-        self.assertEqual(x[self.neg:self.pos:1].indices(maxsize), (0, maxsize, 1))
+        with test_support._check_py3k_warnings():
+            self.assertEqual(x[self.neg:self.pos], (maxint+minsize, maxsize))
+            self.assertEqual(x[self.neg:self.pos:1].indices(maxsize), (0, maxsize, 1))
 
     def test_getitem(self):
         self._getitem_helper(object)

Modified: python/branches/release26-maint/Lib/test/test_random.py
==============================================================================
--- python/branches/release26-maint/Lib/test/test_random.py	(original)
+++ python/branches/release26-maint/Lib/test/test_random.py	Mon Aug  2 20:10:09 2010
@@ -52,10 +52,11 @@
         state3 = self.gen.getstate()    # s/b distinct from state2
         self.assertNotEqual(state2, state3)
 
-        self.assertRaises(TypeError, self.gen.jumpahead)  # needs an arg
-        self.assertRaises(TypeError, self.gen.jumpahead, "ick")  # wrong type
-        self.assertRaises(TypeError, self.gen.jumpahead, 2.3)  # wrong type
-        self.assertRaises(TypeError, self.gen.jumpahead, 2, 3)  # too many
+        with test_support._check_py3k_warnings(quiet=True):
+            self.assertRaises(TypeError, self.gen.jumpahead)  # needs an arg
+            self.assertRaises(TypeError, self.gen.jumpahead, "ick")  # wrong type
+            self.assertRaises(TypeError, self.gen.jumpahead, 2.3)  # wrong type
+            self.assertRaises(TypeError, self.gen.jumpahead, 2, 3)  # too many
 
     def test_sample(self):
         # For the entire allowable range of 0 <= k <= N, validate that

Modified: python/branches/release26-maint/Lib/test/test_support.py
==============================================================================
--- python/branches/release26-maint/Lib/test/test_support.py	(original)
+++ python/branches/release26-maint/Lib/test/test_support.py	Mon Aug  2 20:10:09 2010
@@ -11,6 +11,7 @@
 import shutil
 import warnings
 import unittest
+import re
 
 __all__ = ["Error", "TestFailed", "TestSkipped", "ResourceDenied", "import_module",
            "verbose", "use_resources", "max_memuse", "record_original_stdout",
@@ -18,8 +19,8 @@
            "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", "check_warnings", "CleanImport",
-           "EnvironmentVarGuard", "captured_output",
+           "open_urlresource", "check_warnings", "_check_py3k_warnings",
+           "CleanImport", "EnvironmentVarGuard", "captured_output",
            "captured_stdout", "TransientResource", "transient_internet",
            "run_with_locale", "set_memlimit", "bigmemtest", "bigaddrspacetest",
            "BasicTestRunner", "run_unittest", "run_doctest", "threading_setup",
@@ -406,22 +407,103 @@
        entry to the warnings.catch_warnings() context manager.
     """
     def __init__(self, warnings_list):
-        self.warnings = warnings_list
+        self._warnings = warnings_list
+        self._last = 0
 
     def __getattr__(self, attr):
-        if self.warnings:
-            return getattr(self.warnings[-1], attr)
+        if len(self._warnings) > self._last:
+            return getattr(self._warnings[-1], attr)
         elif attr in warnings.WarningMessage._WARNING_DETAILS:
             return None
         raise AttributeError("%r has no attribute %r" % (self, attr))
 
+    @property
+    def warnings(self):
+        return self._warnings[self._last:]
+
     def reset(self):
-        del self.warnings[:]
+        self._last = len(self._warnings)
 
- at contextlib.contextmanager
-def check_warnings():
+
+def _filterwarnings(filters, quiet=False):
+    """Catch the warnings, then check if all the expected
+    warnings have been raised and re-raise unexpected warnings.
+    If 'quiet' is True, only re-raise the unexpected warnings.
+    """
+    # Clear the warning registry of the calling module
+    # in order to re-raise the warnings.
+    frame = sys._getframe(2)
+    registry = frame.f_globals.get('__warningregistry__')
+    if registry:
+        registry.clear()
     with warnings.catch_warnings(record=True) as w:
+        # Disable filters, to record all warnings.  Because
+        # test_warnings swap the module, we need to look up
+        # in the sys.modules dictionary.
+        sys.modules['warnings'].resetwarnings()
         yield WarningsRecorder(w)
+    # Filter the recorded warnings
+    reraise = [warning.message for warning in w]
+    missing = []
+    for msg, cat in filters:
+        seen = False
+        for exc in reraise[:]:
+            message = str(exc)
+            # Filter out the matching messages
+            if (re.match(msg, message, re.I) and
+                issubclass(exc.__class__, cat)):
+                seen = True
+                reraise.remove(exc)
+        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)
+
+
+ at contextlib.contextmanager
+def check_warnings(*filters, **kwargs):
+    """Context manager to silence warnings.
+
+    Accept 2-tuples as positional arguments:
+        ("message regexp", WarningCategory)
+
+    Optional argument:
+     - if 'quiet' is True, it does not fail if a filter catches nothing
+        (default False)
+
+    Without argument, it defaults to:
+        check_warnings(("", Warning), quiet=False)
+    """
+    if not filters:
+        filters = (("", Warning),)
+    return _filterwarnings(filters, kwargs.get('quiet'))
+
+
+ at contextlib.contextmanager
+def _check_py3k_warnings(*filters, **kwargs):
+    """Context manager to silence py3k warnings.
+
+    Accept 2-tuples as positional arguments:
+        ("message regexp", WarningCategory)
+
+    Optional argument:
+     - if 'quiet' is True, it does not fail if a filter catches nothing
+        (default False)
+
+    Without argument, it defaults to:
+        _check_py3k_warnings(("", DeprecationWarning), quiet=False)
+    """
+    if sys.py3kwarning:
+        if not filters:
+            filters = (("", DeprecationWarning),)
+    else:
+        # It should not raise any py3k warning
+        filters = ()
+    return _filterwarnings(filters, kwargs.get('quiet'))
 
 
 class CleanImport(object):
@@ -595,7 +677,6 @@
 MAX_Py_ssize_t = sys.maxsize
 
 def set_memlimit(limit):
-    import re
     global max_memuse
     global real_max_memuse
     sizes = {

Modified: python/branches/release26-maint/Lib/test/test_unicode.py
==============================================================================
--- python/branches/release26-maint/Lib/test/test_unicode.py	(original)
+++ python/branches/release26-maint/Lib/test/test_unicode.py	Mon Aug  2 20:10:09 2010
@@ -511,9 +511,11 @@
         )
 
         if not sys.platform.startswith('java'):
+            with test_support._check_py3k_warnings():
+                buf = buffer('character buffers are decoded to unicode')
             self.assertEqual(
                 unicode(
-                    buffer('character buffers are decoded to unicode'),
+                    buf,
                     'utf-8',
                     'strict'
                 ),

Modified: python/branches/release26-maint/Misc/NEWS
==============================================================================
--- python/branches/release26-maint/Misc/NEWS	(original)
+++ python/branches/release26-maint/Misc/NEWS	Mon Aug  2 20:10:09 2010
@@ -35,9 +35,6 @@
   when turned into an exception: in this case the exception simply
   gets ignored.
 
-- Issue #7372: Fix pstats regression when stripping paths from profile
-  data generated with the profile module.
-
 - Issue #4108: In urllib.robotparser, if there are multiple 'User-agent: *'
   entries, consider the first one.
 
@@ -422,6 +419,10 @@
 Tests
 -----
 
+- Issue #7849: Now the utility ``check_warnings`` verifies if the warnings are
+  effectively raised.  A new private utility ``_check_py3k_warnings`` has been
+  backported to help silencing py3k warnings.
+
 - Issue #8672: Add a zlib test ensuring that an incomplete stream can be
   handled by a decompressor object without errors (it returns incomplete
   uncompressed data).


More information about the Python-checkins mailing list