Merged revisions 66321 via svnmerge from

svn+ssh://pythondev@svn.python.org/python/trunk

........
  r66321 | brett.cannon | 2008-09-08 17:49:16 -0700 (Mon, 08 Sep 2008) | 7 lines

  warnings.catch_warnings() now returns a list or None instead of the custom
  WarningsRecorder object. This makes the API simpler to use as no special object
  must be learned.

  Closes issue 3781.
  Review by Benjamin Peterson.
........
This commit is contained in:
Brett Cannon 2008-09-09 01:52:27 +00:00
parent 4c19e6e02d
commit 1cd0247a4d
10 changed files with 193 additions and 137 deletions

View file

@ -1,5 +1,5 @@
import unittest
from test.support import run_unittest, catch_warning
from test.support import run_unittest
import sys
import warnings
@ -8,7 +8,7 @@ class AllTest(unittest.TestCase):
def check_all(self, modname):
names = {}
with catch_warning():
with warnings.catch_warnings():
warnings.filterwarnings("ignore", ".* (module|package)",
DeprecationWarning)
try:

View file

@ -211,7 +211,7 @@ class TestVectorsTestCase(unittest.TestCase):
def digest(self):
return self._x.digest()
with support.catch_warning():
with warnings.catch_warnings():
warnings.simplefilter('error', RuntimeWarning)
try:
hmac.HMAC(b'a', b'b', digestmod=MockCrazyHash)

View file

@ -6,7 +6,7 @@ import sys
import py_compile
import warnings
import imp
from test.support import unlink, TESTFN, unload, run_unittest, catch_warning
from test.support import unlink, TESTFN, unload, run_unittest
def remove_files(name):
@ -153,7 +153,7 @@ class ImportTest(unittest.TestCase):
self.assert_(y is test.support, y.__name__)
def test_import_initless_directory_warning(self):
with catch_warning():
with warnings.catch_warnings():
# Just a random non-package directory we always expect to be
# somewhere in sys.path...
warnings.simplefilter('error', ImportWarning)

View file

@ -1,7 +1,7 @@
import sys
sys.path = ['.'] + sys.path
from test.support import verbose, run_unittest, catch_warning
from test.support import verbose, run_unittest
import re
from re import Scanner
import sys, os, traceback

View file

@ -4,7 +4,7 @@ import struct
import warnings
from functools import wraps
from test.support import TestFailed, verbose, run_unittest, catch_warning
from test.support import TestFailed, verbose, run_unittest
import sys
ISBIGENDIAN = sys.byteorder == "big"
@ -34,7 +34,7 @@ def bigendian_to_native(value):
def with_warning_restore(func):
@wraps(func)
def decorator(*args, **kw):
with catch_warning():
with warnings.catch_warnings():
# We need this function to warn every time, so stick an
# unqualifed 'always' at the head of the filter list
warnings.simplefilter("always")

View file

@ -66,35 +66,35 @@ class ReadWriteTests(unittest.TestCase):
class TestWarnings(unittest.TestCase):
def has_warned(self, w):
self.assertEqual(w.category, RuntimeWarning)
self.assertEqual(w[-1].category, RuntimeWarning)
def test_byte_max(self):
with support.catch_warning() as w:
with warnings.catch_warnings(record=True) as w:
ts.T_BYTE = CHAR_MAX+1
self.has_warned(w)
def test_byte_min(self):
with support.catch_warning() as w:
with warnings.catch_warnings(record=True) as w:
ts.T_BYTE = CHAR_MIN-1
self.has_warned(w)
def test_ubyte_max(self):
with support.catch_warning() as w:
with warnings.catch_warnings(record=True) as w:
ts.T_UBYTE = UCHAR_MAX+1
self.has_warned(w)
def test_short_max(self):
with support.catch_warning() as w:
with warnings.catch_warnings(record=True) as w:
ts.T_SHORT = SHRT_MAX+1
self.has_warned(w)
def test_short_min(self):
with support.catch_warning() as w:
with warnings.catch_warnings(record=True) as w:
ts.T_SHORT = SHRT_MIN-1
self.has_warned(w)
def test_ushort_max(self):
with support.catch_warning() as w:
with warnings.catch_warnings(record=True) as w:
ts.T_USHORT = USHRT_MAX+1
self.has_warned(w)

View file

@ -7,7 +7,7 @@ import warnings
class TestUntestedModules(unittest.TestCase):
def test_at_least_import_untested_modules(self):
with support.catch_warning():
with warnings.catch_warnings(record=True):
import aifc
import bdb
import cgitb

View file

@ -72,64 +72,69 @@ class FilterTests(object):
"""Testing the filtering functionality."""
def test_error(self):
with support.catch_warning(self.module) as w:
with original_warnings.catch_warnings(module=self.module) as w:
self.module.resetwarnings()
self.module.filterwarnings("error", category=UserWarning)
self.assertRaises(UserWarning, self.module.warn,
"FilterTests.test_error")
def test_ignore(self):
with support.catch_warning(module=self.module) as w:
with original_warnings.catch_warnings(record=True,
module=self.module) as w:
self.module.resetwarnings()
self.module.filterwarnings("ignore", category=UserWarning)
self.module.warn("FilterTests.test_ignore", UserWarning)
self.assertEquals(len(w), 0)
def test_always(self):
with support.catch_warning(module=self.module) as w:
with original_warnings.catch_warnings(record=True,
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)
self.assert_(message, w[-1].message)
self.module.warn(message, UserWarning)
self.assert_(w.message, message)
self.assert_(w[-1].message, message)
def test_default(self):
with support.catch_warning(self.module) as w:
with original_warnings.catch_warnings(record=True,
module=self.module) as w:
self.module.resetwarnings()
self.module.filterwarnings("default", category=UserWarning)
message = UserWarning("FilterTests.test_default")
for x in range(2):
self.module.warn(message, UserWarning)
if x == 0:
self.assertEquals(w.message, message)
w.reset()
self.assertEquals(w[-1].message, message)
del w[:]
elif x == 1:
self.assert_(not len(w), "unexpected warning: " + str(w))
self.assertEquals(len(w), 0)
else:
raise ValueError("loop variant unhandled")
def test_module(self):
with support.catch_warning(self.module) as w:
with original_warnings.catch_warnings(record=True,
module=self.module) as w:
self.module.resetwarnings()
self.module.filterwarnings("module", category=UserWarning)
message = UserWarning("FilterTests.test_module")
self.module.warn(message, UserWarning)
self.assertEquals(w.message, message)
w.reset()
self.assertEquals(w[-1].message, message)
del w[:]
self.module.warn(message, UserWarning)
self.assert_(not len(w), "unexpected message: " + str(w))
self.assertEquals(len(w), 0)
def test_once(self):
with support.catch_warning(self.module) as w:
with original_warnings.catch_warnings(record=True,
module=self.module) as w:
self.module.resetwarnings()
self.module.filterwarnings("once", category=UserWarning)
message = UserWarning("FilterTests.test_once")
self.module.warn_explicit(message, UserWarning, "test_warnings.py",
42)
self.assertEquals(w.message, message)
w.reset()
self.assertEquals(w[-1].message, message)
del w[:]
self.module.warn_explicit(message, UserWarning, "test_warnings.py",
13)
self.assertEquals(len(w), 0)
@ -138,19 +143,20 @@ class FilterTests(object):
self.assertEquals(len(w), 0)
def test_inheritance(self):
with support.catch_warning(self.module) as w:
with original_warnings.catch_warnings(module=self.module) as w:
self.module.resetwarnings()
self.module.filterwarnings("error", category=Warning)
self.assertRaises(UserWarning, self.module.warn,
"FilterTests.test_inheritance", UserWarning)
def test_ordering(self):
with support.catch_warning(self.module) as w:
with original_warnings.catch_warnings(record=True,
module=self.module) as w:
self.module.resetwarnings()
self.module.filterwarnings("ignore", category=UserWarning)
self.module.filterwarnings("error", category=UserWarning,
append=True)
w.reset()
del w[:]
try:
self.module.warn("FilterTests.test_ordering", UserWarning)
except UserWarning:
@ -160,28 +166,29 @@ class FilterTests(object):
def test_filterwarnings(self):
# Test filterwarnings().
# Implicitly also tests resetwarnings().
with support.catch_warning(self.module) as w:
with original_warnings.catch_warnings(record=True,
module=self.module) as w:
self.module.filterwarnings("error", "", Warning, "", 0)
self.assertRaises(UserWarning, self.module.warn, 'convert to error')
self.module.resetwarnings()
text = 'handle normally'
self.module.warn(text)
self.assertEqual(str(w.message), text)
self.assert_(w.category is UserWarning)
self.assertEqual(str(w[-1].message), text)
self.assert_(w[-1].category is UserWarning)
self.module.filterwarnings("ignore", "", Warning, "", 0)
text = 'filtered out'
self.module.warn(text)
self.assertNotEqual(str(w.message), text)
self.assertNotEqual(str(w[-1].message), text)
self.module.resetwarnings()
self.module.filterwarnings("error", "hex*", Warning, "", 0)
self.assertRaises(UserWarning, self.module.warn, 'hex/oct')
text = 'nonmatching text'
self.module.warn(text)
self.assertEqual(str(w.message), text)
self.assert_(w.category is UserWarning)
self.assertEqual(str(w[-1].message), text)
self.assert_(w[-1].category is UserWarning)
class CFilterTests(BaseTest, FilterTests):
module = c_warnings
@ -195,12 +202,13 @@ class WarnTests(unittest.TestCase):
"""Test warnings.warn() and warnings.warn_explicit()."""
def test_message(self):
with support.catch_warning(self.module) as w:
with original_warnings.catch_warnings(record=True,
module=self.module) as w:
for i in range(4):
text = 'multi %d' %i # Different text on each call.
self.module.warn(text)
self.assertEqual(str(w.message), text)
self.assert_(w.category is UserWarning)
self.assertEqual(str(w[-1].message), text)
self.assert_(w[-1].category is UserWarning)
# Issue 3639
def test_warn_nonstandard_types(self):
@ -210,35 +218,45 @@ class WarnTests(unittest.TestCase):
self.module.warn(ob)
# Don't directly compare objects since
# ``Warning() != Warning()``.
self.assertEquals(str(w.message), str(UserWarning(ob)))
self.assertEquals(str(w[-1].message), str(UserWarning(ob)))
def test_filename(self):
with warnings_state(self.module):
with support.catch_warning(self.module) as w:
with original_warnings.catch_warnings(record=True,
module=self.module) as w:
warning_tests.inner("spam1")
self.assertEqual(os.path.basename(w.filename), "warning_tests.py")
self.assertEqual(os.path.basename(w[-1].filename),
"warning_tests.py")
warning_tests.outer("spam2")
self.assertEqual(os.path.basename(w.filename), "warning_tests.py")
self.assertEqual(os.path.basename(w[-1].filename),
"warning_tests.py")
def test_stacklevel(self):
# Test stacklevel argument
# make sure all messages are different, so the warning won't be skipped
with warnings_state(self.module):
with support.catch_warning(self.module) as w:
with original_warnings.catch_warnings(record=True,
module=self.module) as w:
warning_tests.inner("spam3", stacklevel=1)
self.assertEqual(os.path.basename(w.filename), "warning_tests.py")
self.assertEqual(os.path.basename(w[-1].filename),
"warning_tests.py")
warning_tests.outer("spam4", stacklevel=1)
self.assertEqual(os.path.basename(w.filename), "warning_tests.py")
self.assertEqual(os.path.basename(w[-1].filename),
"warning_tests.py")
warning_tests.inner("spam5", stacklevel=2)
self.assertEqual(os.path.basename(w.filename), "test_warnings.py")
self.assertEqual(os.path.basename(w[-1].filename),
"test_warnings.py")
warning_tests.outer("spam6", stacklevel=2)
self.assertEqual(os.path.basename(w.filename), "warning_tests.py")
self.assertEqual(os.path.basename(w[-1].filename),
"warning_tests.py")
warning_tests.outer("spam6.5", stacklevel=3)
self.assertEqual(os.path.basename(w.filename), "test_warnings.py")
self.assertEqual(os.path.basename(w[-1].filename),
"test_warnings.py")
warning_tests.inner("spam7", stacklevel=9999)
self.assertEqual(os.path.basename(w.filename), "sys")
self.assertEqual(os.path.basename(w[-1].filename),
"sys")
def test_missing_filename_not_main(self):
# If __file__ is not specified and __main__ is not the module name,
@ -247,9 +265,10 @@ class WarnTests(unittest.TestCase):
try:
del warning_tests.__file__
with warnings_state(self.module):
with support.catch_warning(self.module) as w:
with original_warnings.catch_warnings(record=True,
module=self.module) as w:
warning_tests.inner("spam8", stacklevel=1)
self.assertEqual(w.filename, warning_tests.__name__)
self.assertEqual(w[-1].filename, warning_tests.__name__)
finally:
warning_tests.__file__ = filename
@ -264,9 +283,10 @@ class WarnTests(unittest.TestCase):
del warning_tests.__file__
warning_tests.__name__ = '__main__'
with warnings_state(self.module):
with support.catch_warning(self.module) as w:
with original_warnings.catch_warnings(record=True,
module=self.module) as w:
warning_tests.inner('spam9', stacklevel=1)
self.assertEqual(w.filename, sys.argv[0])
self.assertEqual(w[-1].filename, sys.argv[0])
finally:
warning_tests.__file__ = filename
warning_tests.__name__ = module_name
@ -282,9 +302,10 @@ class WarnTests(unittest.TestCase):
warning_tests.__name__ = '__main__'
del sys.argv
with warnings_state(self.module):
with support.catch_warning(self.module) as w:
with original_warnings.catch_warnings(record=True,
module=self.module) as w:
warning_tests.inner('spam10', stacklevel=1)
self.assertEqual(w.filename, '__main__')
self.assertEqual(w[-1].filename, '__main__')
finally:
warning_tests.__file__ = filename
warning_tests.__name__ = module_name
@ -302,9 +323,10 @@ class WarnTests(unittest.TestCase):
warning_tests.__name__ = '__main__'
sys.argv = ['']
with warnings_state(self.module):
with support.catch_warning(self.module) as w:
with original_warnings.catch_warnings(record=True,
module=self.module) as w:
warning_tests.inner('spam11', stacklevel=1)
self.assertEqual(w.filename, '__main__')
self.assertEqual(w[-1].filename, '__main__')
finally:
warning_tests.__file__ = file_name
warning_tests.__name__ = module_name
@ -337,7 +359,7 @@ class WCmdLineTests(unittest.TestCase):
def test_improper_input(self):
# Uses the private _setoption() function to test the parsing
# of command-line warning arguments
with support.catch_warning(self.module):
with original_warnings.catch_warnings(module=self.module):
self.assertRaises(self.module._OptionError,
self.module._setoption, '1:2:3:4:5:6')
self.assertRaises(self.module._OptionError,
@ -362,7 +384,7 @@ class _WarningsTests(BaseTest):
def test_filter(self):
# Everything should function even if 'filters' is not in warnings.
with support.catch_warning(self.module) as w:
with original_warnings.catch_warnings(module=self.module) as w:
self.module.filterwarnings("error", "", Warning, "", 0)
self.assertRaises(UserWarning, self.module.warn,
'convert to error')
@ -377,21 +399,22 @@ class _WarningsTests(BaseTest):
try:
original_registry = self.module.onceregistry
__warningregistry__ = {}
with support.catch_warning(self.module) as w:
with original_warnings.catch_warnings(record=True,
module=self.module) as w:
self.module.resetwarnings()
self.module.filterwarnings("once", category=UserWarning)
self.module.warn_explicit(message, UserWarning, "file", 42)
self.failUnlessEqual(w.message, message)
w.reset()
self.failUnlessEqual(w[-1].message, message)
del w[:]
self.module.warn_explicit(message, UserWarning, "file", 42)
self.assertEquals(len(w), 0)
# Test the resetting of onceregistry.
self.module.onceregistry = {}
__warningregistry__ = {}
self.module.warn('onceregistry test')
self.failUnlessEqual(w.message.args, message.args)
self.failUnlessEqual(w[-1].message.args, message.args)
# Removal of onceregistry is okay.
w.reset()
del w[:]
del self.module.onceregistry
__warningregistry__ = {}
self.module.warn_explicit(message, UserWarning, "file", 42)
@ -402,7 +425,7 @@ class _WarningsTests(BaseTest):
def test_showwarning_missing(self):
# Test that showwarning() missing is okay.
text = 'del showwarning test'
with support.catch_warning(self.module):
with original_warnings.catch_warnings(module=self.module):
self.module.filterwarnings("always", category=UserWarning)
del self.module.showwarning
with support.captured_output('stderr') as stream:
@ -423,7 +446,7 @@ class _WarningsTests(BaseTest):
def test_show_warning_output(self):
# With showarning() missing, make sure that output is okay.
text = 'test show_warning'
with support.catch_warning(self.module):
with original_warnings.catch_warnings(module=self.module):
self.module.filterwarnings("always", category=UserWarning)
del self.module.showwarning
with support.captured_output('stderr') as stream:
@ -494,6 +517,7 @@ class CWarningsDisplayTests(BaseTest, WarningsDisplayTests):
class PyWarningsDisplayTests(BaseTest, WarningsDisplayTests):
module = py_warnings
class CatchWarningTests(BaseTest):
"""Test catch_warnings()."""
@ -517,12 +541,12 @@ class CatchWarningTests(BaseTest):
self.assertEqual(w, [])
wmod.simplefilter("always")
wmod.warn("foo")
self.assertEqual(str(w.message), "foo")
self.assertEqual(str(w[-1].message), "foo")
wmod.warn("bar")
self.assertEqual(str(w.message), "bar")
self.assertEqual(str(w[-1].message), "bar")
self.assertEqual(str(w[0].message), "foo")
self.assertEqual(str(w[1].message), "bar")
w.reset()
del w[:]
self.assertEqual(w, [])
orig_showwarning = wmod.showwarning
with support.catch_warning(module=wmod, record=False) as w:

View file

@ -7,7 +7,7 @@ import linecache
import sys
__all__ = ["warn", "showwarning", "formatwarning", "filterwarnings",
"resetwarnings"]
"resetwarnings", "catch_warnings"]
def showwarning(message, category, filename, lineno, file=None, line=None):
@ -274,28 +274,20 @@ class WarningMessage(object):
self.filename, self.lineno, self.line))
class WarningsRecorder(list):
"""Record the result of various showwarning() calls."""
def showwarning(self, *args, **kwargs):
self.append(WarningMessage(*args, **kwargs))
def __getattr__(self, attr):
return getattr(self[-1], attr)
def reset(self):
del self[:]
class catch_warnings(object):
"""Guard the warnings filter from being permanently changed and optionally
record the details of any warnings that are issued.
"""A context manager that copies and restores the warnings filter upon
exiting the context.
Context manager returns an instance of warnings.WarningRecorder which is a
list of WarningMessage instances. Attributes on WarningRecorder are
redirected to the last created WarningMessage instance.
The 'record' argument specifies whether warnings should be captured by a
custom implementation of warnings.showwarning() and be appended to a list
returned by the context manager. Otherwise None is returned by the context
manager. The objects appended to the list are arguments whose attributes
mirror the arguments to showwarning().
The 'module' argument is to specify an alternative module to the module
named 'warnings' and imported under that name. This argument is only useful
when testing the warnings module itself.
"""
@ -307,17 +299,21 @@ class catch_warnings(object):
keyword-only.
"""
self._recorder = WarningsRecorder() if record else None
self._record = record
self._module = sys.modules['warnings'] if module is None else module
def __enter__(self):
self._filters = self._module.filters
self._module.filters = self._filters[:]
self._showwarning = self._module.showwarning
if self._recorder is not None:
self._recorder.reset() # In case the instance is being reused.
self._module.showwarning = self._recorder.showwarning
return self._recorder
if self._record:
log = []
def showwarning(*args, **kwargs):
log.append(WarningMessage(*args, **kwargs))
self._module.showwarning = showwarning
return log
else:
return None
def __exit__(self, *exc_info):
self._module.filters = self._filters