diff options
author | Florent Xicluna <florent.xicluna@gmail.com> | 2010-03-13 15:26:44 (GMT) |
---|---|---|
committer | Florent Xicluna <florent.xicluna@gmail.com> | 2010-03-13 15:26:44 (GMT) |
commit | b14930cd93e74cae3b7370262c6dcc7c28e0e712 (patch) | |
tree | daca91cb5ed347957f3ad629ff9be93f0f1af4a4 /Lib/test | |
parent | 8142d8d7515b10cfc10fc332fcce34bac1e6d52e (diff) | |
download | cpython-b14930cd93e74cae3b7370262c6dcc7c28e0e712.zip cpython-b14930cd93e74cae3b7370262c6dcc7c28e0e712.tar.gz cpython-b14930cd93e74cae3b7370262c6dcc7c28e0e712.tar.bz2 |
Only the parts which are relevant for 3.x branch.
Merged revisions 78757-78758,78769,78815 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r78757 | florent.xicluna | 2010-03-07 13:14:25 +0100 (dim, 07 mar 2010) | 2 lines
Fix some py3k warnings in the standard library.
........
r78758 | florent.xicluna | 2010-03-07 13:18:33 +0100 (dim, 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.
........
r78769 | florent.xicluna | 2010-03-07 20:14:12 +0100 (dim, 07 mar 2010) | 2 lines
Refresh the documentation for the test.test_support module.
........
r78815 | florent.xicluna | 2010-03-09 20:57:01 +0100 (mar, 09 mar 2010) | 2 lines
#7772: Fix test_py3kwarn. Now the test suite could pass with "-3" flag.
........
Diffstat (limited to 'Lib/test')
-rw-r--r-- | Lib/test/support.py | 72 | ||||
-rw-r--r-- | Lib/test/test_fileio.py | 2 |
2 files changed, 66 insertions, 8 deletions
diff --git a/Lib/test/support.py b/Lib/test/support.py index bf49bab..769f94a 100644 --- a/Lib/test/support.py +++ b/Lib/test/support.py @@ -16,6 +16,7 @@ import warnings import unittest import importlib import collections +import re __all__ = ["Error", "TestFailed", "ResourceDenied", "import_module", "verbose", "use_resources", "max_memuse", "record_original_stdout", @@ -464,22 +465,80 @@ class WarningsRecorder(object): 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) -@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: + # Set filter "always" to record all warnings. Because + # test_warnings swap the module, we need to look up in + # the sys.modules dictionary. + sys.modules['warnings'].simplefilter("always") 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__)) + 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 +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')) class CleanImport(object): @@ -714,7 +773,6 @@ _4G = 4 * _1G MAX_Py_ssize_t = sys.maxsize def set_memlimit(limit): - import re global max_memuse global real_max_memuse sizes = { diff --git a/Lib/test/test_fileio.py b/Lib/test/test_fileio.py index 8348f6e..fd27512 100644 --- a/Lib/test/test_fileio.py +++ b/Lib/test/test_fileio.py @@ -390,7 +390,7 @@ class OtherFileTests(unittest.TestCase): self.assertRaises(TypeError, _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, []) self.assertEqual(w.warnings, []) |