2008-04-12 20:44:07 -03:00
|
|
|
from contextlib import contextmanager
|
2007-12-20 06:09:52 -04:00
|
|
|
import linecache
|
2003-07-13 03:15:11 -03:00
|
|
|
import os
|
2007-12-20 06:09:52 -04:00
|
|
|
import StringIO
|
2007-08-17 17:16:15 -03:00
|
|
|
import sys
|
2003-07-13 05:37:40 -03:00
|
|
|
import unittest
|
|
|
|
from test import test_support
|
2003-07-11 12:37:59 -03:00
|
|
|
|
2007-04-03 13:53:43 -03:00
|
|
|
import warning_tests
|
|
|
|
|
2008-04-12 23:42:36 -03:00
|
|
|
import warnings as original_warnings
|
|
|
|
|
2008-04-12 20:44:07 -03:00
|
|
|
sys.modules['_warnings'] = 0
|
2008-04-12 23:42:36 -03:00
|
|
|
del sys.modules['warnings']
|
2008-04-12 20:44:07 -03:00
|
|
|
|
|
|
|
import warnings as py_warnings
|
|
|
|
|
|
|
|
del sys.modules['_warnings']
|
|
|
|
del sys.modules['warnings']
|
|
|
|
|
|
|
|
import warnings as c_warnings
|
|
|
|
|
2008-04-12 23:42:36 -03:00
|
|
|
sys.modules['warnings'] = original_warnings
|
|
|
|
|
|
|
|
|
2008-04-12 20:44:07 -03:00
|
|
|
@contextmanager
|
|
|
|
def warnings_state(module):
|
|
|
|
"""Use a specific warnings implementation in warning_tests."""
|
|
|
|
global __warningregistry__
|
|
|
|
for to_clear in (sys, warning_tests):
|
|
|
|
try:
|
|
|
|
to_clear.__warningregistry__.clear()
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
try:
|
|
|
|
__warningregistry__.clear()
|
|
|
|
except NameError:
|
|
|
|
pass
|
|
|
|
original_warnings = warning_tests.warnings
|
|
|
|
try:
|
|
|
|
warning_tests.warnings = module
|
|
|
|
yield
|
|
|
|
finally:
|
|
|
|
warning_tests.warnings = original_warnings
|
|
|
|
|
|
|
|
|
2008-04-12 23:42:36 -03:00
|
|
|
class BaseTest(unittest.TestCase):
|
2008-04-12 20:44:07 -03:00
|
|
|
|
2008-04-12 23:42:36 -03:00
|
|
|
"""Basic bookkeeping required for testing."""
|
2008-04-12 20:44:07 -03:00
|
|
|
|
2003-07-13 05:37:40 -03:00
|
|
|
def setUp(self):
|
2008-04-12 23:42:36 -03:00
|
|
|
# The __warningregistry__ needs to be in a pristine state for tests
|
|
|
|
# to work properly.
|
|
|
|
if '__warningregistry__' in globals():
|
|
|
|
del globals()['__warningregistry__']
|
|
|
|
if hasattr(warning_tests, '__warningregistry__'):
|
|
|
|
del warning_tests.__warningregistry__
|
|
|
|
if hasattr(sys, '__warningregistry__'):
|
|
|
|
del sys.__warningregistry__
|
|
|
|
# The 'warnings' module must be explicitly set so that the proper
|
|
|
|
# interaction between _warnings and 'warnings' can be controlled.
|
|
|
|
sys.modules['warnings'] = self.module
|
|
|
|
super(BaseTest, self).setUp()
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
sys.modules['warnings'] = original_warnings
|
|
|
|
super(BaseTest, self).tearDown()
|
|
|
|
|
|
|
|
|
|
|
|
class FilterTests(object):
|
|
|
|
|
|
|
|
"""Testing the filtering functionality."""
|
2003-07-13 05:37:40 -03:00
|
|
|
|
2008-04-12 20:44:07 -03:00
|
|
|
def test_error(self):
|
2008-09-08 21:49:16 -03:00
|
|
|
with original_warnings.catch_warnings(module=self.module) as w:
|
2008-04-12 20:44:07 -03:00
|
|
|
self.module.resetwarnings()
|
|
|
|
self.module.filterwarnings("error", category=UserWarning)
|
|
|
|
self.assertRaises(UserWarning, self.module.warn,
|
|
|
|
"FilterTests.test_error")
|
|
|
|
|
|
|
|
def test_ignore(self):
|
2008-09-08 21:49:16 -03:00
|
|
|
with original_warnings.catch_warnings(record=True,
|
|
|
|
module=self.module) as w:
|
2008-04-12 20:44:07 -03:00
|
|
|
self.module.resetwarnings()
|
|
|
|
self.module.filterwarnings("ignore", category=UserWarning)
|
|
|
|
self.module.warn("FilterTests.test_ignore", UserWarning)
|
2008-09-01 22:25:16 -03:00
|
|
|
self.assertEquals(len(w), 0)
|
2008-04-12 20:44:07 -03:00
|
|
|
|
|
|
|
def test_always(self):
|
2008-09-08 21:49:16 -03:00
|
|
|
with original_warnings.catch_warnings(record=True,
|
|
|
|
module=self.module) as w:
|
2008-04-12 20:44:07 -03:00
|
|
|
self.module.resetwarnings()
|
|
|
|
self.module.filterwarnings("always", category=UserWarning)
|
|
|
|
message = "FilterTests.test_always"
|
|
|
|
self.module.warn(message, UserWarning)
|
2008-09-08 21:49:16 -03:00
|
|
|
self.assert_(message, w[-1].message)
|
2008-04-12 20:44:07 -03:00
|
|
|
self.module.warn(message, UserWarning)
|
2008-09-08 21:49:16 -03:00
|
|
|
self.assert_(w[-1].message, message)
|
2003-07-13 05:37:40 -03:00
|
|
|
|
2008-04-12 20:44:07 -03:00
|
|
|
def test_default(self):
|
2008-09-08 21:49:16 -03:00
|
|
|
with original_warnings.catch_warnings(record=True,
|
|
|
|
module=self.module) as w:
|
2008-04-12 20:44:07 -03:00
|
|
|
self.module.resetwarnings()
|
|
|
|
self.module.filterwarnings("default", category=UserWarning)
|
|
|
|
message = UserWarning("FilterTests.test_default")
|
|
|
|
for x in xrange(2):
|
|
|
|
self.module.warn(message, UserWarning)
|
|
|
|
if x == 0:
|
2008-09-08 21:49:16 -03:00
|
|
|
self.assertEquals(w[-1].message, message)
|
|
|
|
del w[:]
|
2008-04-12 20:44:07 -03:00
|
|
|
elif x == 1:
|
2008-09-08 21:49:16 -03:00
|
|
|
self.assertEquals(len(w), 0)
|
2007-04-03 15:33:29 -03:00
|
|
|
else:
|
2008-04-12 20:44:07 -03:00
|
|
|
raise ValueError("loop variant unhandled")
|
2003-07-13 05:37:40 -03:00
|
|
|
|
2008-04-12 20:44:07 -03:00
|
|
|
def test_module(self):
|
2008-09-08 21:49:16 -03:00
|
|
|
with original_warnings.catch_warnings(record=True,
|
|
|
|
module=self.module) as w:
|
2008-04-12 20:44:07 -03:00
|
|
|
self.module.resetwarnings()
|
|
|
|
self.module.filterwarnings("module", category=UserWarning)
|
|
|
|
message = UserWarning("FilterTests.test_module")
|
|
|
|
self.module.warn(message, UserWarning)
|
2008-09-08 21:49:16 -03:00
|
|
|
self.assertEquals(w[-1].message, message)
|
|
|
|
del w[:]
|
2008-04-12 20:44:07 -03:00
|
|
|
self.module.warn(message, UserWarning)
|
2008-09-08 21:49:16 -03:00
|
|
|
self.assertEquals(len(w), 0)
|
2008-04-12 20:44:07 -03:00
|
|
|
|
|
|
|
def test_once(self):
|
2008-09-08 21:49:16 -03:00
|
|
|
with original_warnings.catch_warnings(record=True,
|
|
|
|
module=self.module) as w:
|
2008-04-12 20:44:07 -03:00
|
|
|
self.module.resetwarnings()
|
|
|
|
self.module.filterwarnings("once", category=UserWarning)
|
|
|
|
message = UserWarning("FilterTests.test_once")
|
|
|
|
self.module.warn_explicit(message, UserWarning, "test_warnings.py",
|
|
|
|
42)
|
2008-09-08 21:49:16 -03:00
|
|
|
self.assertEquals(w[-1].message, message)
|
|
|
|
del w[:]
|
2008-04-12 20:44:07 -03:00
|
|
|
self.module.warn_explicit(message, UserWarning, "test_warnings.py",
|
|
|
|
13)
|
2008-09-01 22:25:16 -03:00
|
|
|
self.assertEquals(len(w), 0)
|
2008-04-12 20:44:07 -03:00
|
|
|
self.module.warn_explicit(message, UserWarning, "test_warnings2.py",
|
|
|
|
42)
|
2008-09-01 22:25:16 -03:00
|
|
|
self.assertEquals(len(w), 0)
|
2008-04-12 20:44:07 -03:00
|
|
|
|
|
|
|
def test_inheritance(self):
|
2008-09-08 21:49:16 -03:00
|
|
|
with original_warnings.catch_warnings(module=self.module) as w:
|
2008-04-12 20:44:07 -03:00
|
|
|
self.module.resetwarnings()
|
|
|
|
self.module.filterwarnings("error", category=Warning)
|
|
|
|
self.assertRaises(UserWarning, self.module.warn,
|
|
|
|
"FilterTests.test_inheritance", UserWarning)
|
|
|
|
|
|
|
|
def test_ordering(self):
|
2008-09-08 21:49:16 -03:00
|
|
|
with original_warnings.catch_warnings(record=True,
|
|
|
|
module=self.module) as w:
|
2008-04-12 20:44:07 -03:00
|
|
|
self.module.resetwarnings()
|
|
|
|
self.module.filterwarnings("ignore", category=UserWarning)
|
|
|
|
self.module.filterwarnings("error", category=UserWarning,
|
|
|
|
append=True)
|
2008-09-08 21:49:16 -03:00
|
|
|
del w[:]
|
2008-04-12 20:44:07 -03:00
|
|
|
try:
|
|
|
|
self.module.warn("FilterTests.test_ordering", UserWarning)
|
|
|
|
except UserWarning:
|
|
|
|
self.fail("order handling for actions failed")
|
2008-09-01 22:25:16 -03:00
|
|
|
self.assertEquals(len(w), 0)
|
2008-04-12 20:44:07 -03:00
|
|
|
|
|
|
|
def test_filterwarnings(self):
|
2007-12-20 06:09:52 -04:00
|
|
|
# Test filterwarnings().
|
|
|
|
# Implicitly also tests resetwarnings().
|
2008-09-08 21:49:16 -03:00
|
|
|
with original_warnings.catch_warnings(record=True,
|
|
|
|
module=self.module) as w:
|
2008-04-12 20:44:07 -03:00
|
|
|
self.module.filterwarnings("error", "", Warning, "", 0)
|
|
|
|
self.assertRaises(UserWarning, self.module.warn, 'convert to error')
|
2003-07-13 05:37:40 -03:00
|
|
|
|
2008-04-12 20:44:07 -03:00
|
|
|
self.module.resetwarnings()
|
2007-04-03 15:33:29 -03:00
|
|
|
text = 'handle normally'
|
2008-04-12 20:44:07 -03:00
|
|
|
self.module.warn(text)
|
2008-09-08 21:49:16 -03:00
|
|
|
self.assertEqual(str(w[-1].message), text)
|
|
|
|
self.assert_(w[-1].category is UserWarning)
|
2003-07-13 05:37:40 -03:00
|
|
|
|
2008-04-12 20:44:07 -03:00
|
|
|
self.module.filterwarnings("ignore", "", Warning, "", 0)
|
2007-04-03 15:33:29 -03:00
|
|
|
text = 'filtered out'
|
2008-04-12 20:44:07 -03:00
|
|
|
self.module.warn(text)
|
2008-09-08 21:49:16 -03:00
|
|
|
self.assertNotEqual(str(w[-1].message), text)
|
2003-07-13 05:37:40 -03:00
|
|
|
|
2008-04-12 20:44:07 -03:00
|
|
|
self.module.resetwarnings()
|
|
|
|
self.module.filterwarnings("error", "hex*", Warning, "", 0)
|
|
|
|
self.assertRaises(UserWarning, self.module.warn, 'hex/oct')
|
2007-04-03 15:33:29 -03:00
|
|
|
text = 'nonmatching text'
|
2008-04-12 20:44:07 -03:00
|
|
|
self.module.warn(text)
|
2008-09-08 21:49:16 -03:00
|
|
|
self.assertEqual(str(w[-1].message), text)
|
|
|
|
self.assert_(w[-1].category is UserWarning)
|
2003-07-13 05:37:40 -03:00
|
|
|
|
2008-04-12 23:42:36 -03:00
|
|
|
class CFilterTests(BaseTest, FilterTests):
|
2008-04-12 20:44:07 -03:00
|
|
|
module = c_warnings
|
|
|
|
|
2008-04-12 23:42:36 -03:00
|
|
|
class PyFilterTests(BaseTest, FilterTests):
|
2008-04-12 20:44:07 -03:00
|
|
|
module = py_warnings
|
|
|
|
|
|
|
|
|
|
|
|
class WarnTests(unittest.TestCase):
|
|
|
|
|
|
|
|
"""Test warnings.warn() and warnings.warn_explicit()."""
|
|
|
|
|
|
|
|
def test_message(self):
|
2008-09-08 21:49:16 -03:00
|
|
|
with original_warnings.catch_warnings(record=True,
|
|
|
|
module=self.module) as w:
|
2008-04-12 20:44:07 -03:00
|
|
|
for i in range(4):
|
|
|
|
text = 'multi %d' %i # Different text on each call.
|
|
|
|
self.module.warn(text)
|
2008-09-08 21:49:16 -03:00
|
|
|
self.assertEqual(str(w[-1].message), text)
|
|
|
|
self.assert_(w[-1].category is UserWarning)
|
2006-06-27 08:52:49 -03:00
|
|
|
|
2007-04-03 13:53:43 -03:00
|
|
|
def test_filename(self):
|
2008-04-12 20:44:07 -03:00
|
|
|
with warnings_state(self.module):
|
2008-09-08 21:49:16 -03:00
|
|
|
with original_warnings.catch_warnings(record=True,
|
|
|
|
module=self.module) as w:
|
2008-04-12 20:44:07 -03:00
|
|
|
warning_tests.inner("spam1")
|
2008-09-08 21:49:16 -03:00
|
|
|
self.assertEqual(os.path.basename(w[-1].filename),
|
|
|
|
"warning_tests.py")
|
2008-04-12 20:44:07 -03:00
|
|
|
warning_tests.outer("spam2")
|
2008-09-08 21:49:16 -03:00
|
|
|
self.assertEqual(os.path.basename(w[-1].filename),
|
|
|
|
"warning_tests.py")
|
2007-04-03 13:53:43 -03:00
|
|
|
|
|
|
|
def test_stacklevel(self):
|
|
|
|
# Test stacklevel argument
|
|
|
|
# make sure all messages are different, so the warning won't be skipped
|
2008-04-12 20:44:07 -03:00
|
|
|
with warnings_state(self.module):
|
2008-09-08 21:49:16 -03:00
|
|
|
with original_warnings.catch_warnings(record=True,
|
|
|
|
module=self.module) as w:
|
2008-04-12 20:44:07 -03:00
|
|
|
warning_tests.inner("spam3", stacklevel=1)
|
2008-09-08 21:49:16 -03:00
|
|
|
self.assertEqual(os.path.basename(w[-1].filename),
|
|
|
|
"warning_tests.py")
|
2008-04-12 20:44:07 -03:00
|
|
|
warning_tests.outer("spam4", stacklevel=1)
|
2008-09-08 21:49:16 -03:00
|
|
|
self.assertEqual(os.path.basename(w[-1].filename),
|
|
|
|
"warning_tests.py")
|
2008-04-12 20:44:07 -03:00
|
|
|
|
|
|
|
warning_tests.inner("spam5", stacklevel=2)
|
2008-09-08 21:49:16 -03:00
|
|
|
self.assertEqual(os.path.basename(w[-1].filename),
|
|
|
|
"test_warnings.py")
|
2008-04-12 20:44:07 -03:00
|
|
|
warning_tests.outer("spam6", stacklevel=2)
|
2008-09-08 21:49:16 -03:00
|
|
|
self.assertEqual(os.path.basename(w[-1].filename),
|
|
|
|
"warning_tests.py")
|
2008-05-06 01:37:31 -03:00
|
|
|
warning_tests.outer("spam6.5", stacklevel=3)
|
2008-09-08 21:49:16 -03:00
|
|
|
self.assertEqual(os.path.basename(w[-1].filename),
|
|
|
|
"test_warnings.py")
|
2008-04-12 20:44:07 -03:00
|
|
|
|
|
|
|
warning_tests.inner("spam7", stacklevel=9999)
|
2008-09-08 21:49:16 -03:00
|
|
|
self.assertEqual(os.path.basename(w[-1].filename),
|
|
|
|
"sys")
|
2008-04-12 20:44:07 -03:00
|
|
|
|
2008-05-03 00:19:39 -03:00
|
|
|
def test_missing_filename_not_main(self):
|
|
|
|
# If __file__ is not specified and __main__ is not the module name,
|
|
|
|
# then __file__ should be set to the module name.
|
|
|
|
filename = warning_tests.__file__
|
|
|
|
try:
|
|
|
|
del warning_tests.__file__
|
|
|
|
with warnings_state(self.module):
|
2008-09-08 21:49:16 -03:00
|
|
|
with original_warnings.catch_warnings(record=True,
|
|
|
|
module=self.module) as w:
|
2008-05-03 00:19:39 -03:00
|
|
|
warning_tests.inner("spam8", stacklevel=1)
|
2008-09-08 21:49:16 -03:00
|
|
|
self.assertEqual(w[-1].filename, warning_tests.__name__)
|
2008-05-03 00:19:39 -03:00
|
|
|
finally:
|
|
|
|
warning_tests.__file__ = filename
|
|
|
|
|
|
|
|
def test_missing_filename_main_with_argv(self):
|
|
|
|
# If __file__ is not specified and the caller is __main__ and sys.argv
|
|
|
|
# exists, then use sys.argv[0] as the file.
|
|
|
|
if not hasattr(sys, 'argv'):
|
|
|
|
return
|
|
|
|
filename = warning_tests.__file__
|
|
|
|
module_name = warning_tests.__name__
|
|
|
|
try:
|
|
|
|
del warning_tests.__file__
|
|
|
|
warning_tests.__name__ = '__main__'
|
|
|
|
with warnings_state(self.module):
|
2008-09-08 21:49:16 -03:00
|
|
|
with original_warnings.catch_warnings(record=True,
|
|
|
|
module=self.module) as w:
|
2008-05-03 00:19:39 -03:00
|
|
|
warning_tests.inner('spam9', stacklevel=1)
|
2008-09-08 21:49:16 -03:00
|
|
|
self.assertEqual(w[-1].filename, sys.argv[0])
|
2008-05-03 00:19:39 -03:00
|
|
|
finally:
|
|
|
|
warning_tests.__file__ = filename
|
|
|
|
warning_tests.__name__ = module_name
|
|
|
|
|
|
|
|
def test_missing_filename_main_without_argv(self):
|
|
|
|
# If __file__ is not specified, the caller is __main__, and sys.argv
|
|
|
|
# is not set, then '__main__' is the file name.
|
|
|
|
filename = warning_tests.__file__
|
|
|
|
module_name = warning_tests.__name__
|
|
|
|
argv = sys.argv
|
|
|
|
try:
|
|
|
|
del warning_tests.__file__
|
|
|
|
warning_tests.__name__ = '__main__'
|
|
|
|
del sys.argv
|
|
|
|
with warnings_state(self.module):
|
2008-09-08 21:49:16 -03:00
|
|
|
with original_warnings.catch_warnings(record=True,
|
|
|
|
module=self.module) as w:
|
2008-05-03 00:19:39 -03:00
|
|
|
warning_tests.inner('spam10', stacklevel=1)
|
2008-09-08 21:49:16 -03:00
|
|
|
self.assertEqual(w[-1].filename, '__main__')
|
2008-05-03 00:19:39 -03:00
|
|
|
finally:
|
|
|
|
warning_tests.__file__ = filename
|
|
|
|
warning_tests.__name__ = module_name
|
|
|
|
sys.argv = argv
|
|
|
|
|
|
|
|
def test_missing_filename_main_with_argv_empty_string(self):
|
|
|
|
# If __file__ is not specified, the caller is __main__, and sys.argv[0]
|
|
|
|
# is the empty string, then '__main__ is the file name.
|
|
|
|
# Tests issue 2743.
|
|
|
|
file_name = warning_tests.__file__
|
|
|
|
module_name = warning_tests.__name__
|
|
|
|
argv = sys.argv
|
|
|
|
try:
|
|
|
|
del warning_tests.__file__
|
|
|
|
warning_tests.__name__ = '__main__'
|
|
|
|
sys.argv = ['']
|
|
|
|
with warnings_state(self.module):
|
2008-09-08 21:49:16 -03:00
|
|
|
with original_warnings.catch_warnings(record=True,
|
|
|
|
module=self.module) as w:
|
2008-05-03 00:19:39 -03:00
|
|
|
warning_tests.inner('spam11', stacklevel=1)
|
2008-09-08 21:49:16 -03:00
|
|
|
self.assertEqual(w[-1].filename, '__main__')
|
2008-05-03 00:19:39 -03:00
|
|
|
finally:
|
|
|
|
warning_tests.__file__ = file_name
|
|
|
|
warning_tests.__name__ = module_name
|
|
|
|
sys.argv = argv
|
|
|
|
|
2008-06-26 21:31:13 -03:00
|
|
|
def test_warn_explicit_type_errors(self):
|
|
|
|
# warn_explicit() shoud error out gracefully if it is given objects
|
|
|
|
# of the wrong types.
|
|
|
|
# lineno is expected to be an integer.
|
|
|
|
self.assertRaises(TypeError, self.module.warn_explicit,
|
|
|
|
None, UserWarning, None, None)
|
|
|
|
# Either 'message' needs to be an instance of Warning or 'category'
|
|
|
|
# needs to be a subclass.
|
|
|
|
self.assertRaises(TypeError, self.module.warn_explicit,
|
|
|
|
None, None, None, 1)
|
|
|
|
# 'registry' must be a dict or None.
|
|
|
|
self.assertRaises((TypeError, AttributeError),
|
|
|
|
self.module.warn_explicit,
|
|
|
|
None, Warning, None, 1, registry=42)
|
|
|
|
|
2008-05-03 00:19:39 -03:00
|
|
|
|
2008-04-12 23:42:36 -03:00
|
|
|
class CWarnTests(BaseTest, WarnTests):
|
2008-04-12 20:44:07 -03:00
|
|
|
module = c_warnings
|
|
|
|
|
2008-04-12 23:42:36 -03:00
|
|
|
class PyWarnTests(BaseTest, WarnTests):
|
2008-04-12 20:44:07 -03:00
|
|
|
module = py_warnings
|
|
|
|
|
|
|
|
|
|
|
|
class WCmdLineTests(unittest.TestCase):
|
|
|
|
|
|
|
|
def test_improper_input(self):
|
|
|
|
# Uses the private _setoption() function to test the parsing
|
|
|
|
# of command-line warning arguments
|
2008-09-08 21:49:16 -03:00
|
|
|
with original_warnings.catch_warnings(module=self.module):
|
2008-04-12 20:44:07 -03:00
|
|
|
self.assertRaises(self.module._OptionError,
|
|
|
|
self.module._setoption, '1:2:3:4:5:6')
|
|
|
|
self.assertRaises(self.module._OptionError,
|
|
|
|
self.module._setoption, 'bogus::Warning')
|
|
|
|
self.assertRaises(self.module._OptionError,
|
|
|
|
self.module._setoption, 'ignore:2::4:-5')
|
|
|
|
self.module._setoption('error::Warning::0')
|
|
|
|
self.assertRaises(UserWarning, self.module.warn, 'convert to error')
|
|
|
|
|
2008-04-12 23:42:36 -03:00
|
|
|
class CWCmdLineTests(BaseTest, WCmdLineTests):
|
2008-04-12 20:44:07 -03:00
|
|
|
module = c_warnings
|
|
|
|
|
2008-04-12 23:42:36 -03:00
|
|
|
class PyWCmdLineTests(BaseTest, WCmdLineTests):
|
2008-04-12 20:44:07 -03:00
|
|
|
module = py_warnings
|
2007-04-03 15:33:29 -03:00
|
|
|
|
|
|
|
|
2008-04-12 23:42:36 -03:00
|
|
|
class _WarningsTests(BaseTest):
|
2008-04-12 20:44:07 -03:00
|
|
|
|
|
|
|
"""Tests specific to the _warnings module."""
|
|
|
|
|
|
|
|
module = c_warnings
|
|
|
|
|
|
|
|
def test_filter(self):
|
|
|
|
# Everything should function even if 'filters' is not in warnings.
|
2008-09-08 21:49:16 -03:00
|
|
|
with original_warnings.catch_warnings(module=self.module) as w:
|
2008-04-12 20:44:07 -03:00
|
|
|
self.module.filterwarnings("error", "", Warning, "", 0)
|
|
|
|
self.assertRaises(UserWarning, self.module.warn,
|
|
|
|
'convert to error')
|
|
|
|
del self.module.filters
|
|
|
|
self.assertRaises(UserWarning, self.module.warn,
|
|
|
|
'convert to error')
|
|
|
|
|
|
|
|
def test_onceregistry(self):
|
|
|
|
# Replacing or removing the onceregistry should be okay.
|
|
|
|
global __warningregistry__
|
|
|
|
message = UserWarning('onceregistry test')
|
|
|
|
try:
|
|
|
|
original_registry = self.module.onceregistry
|
|
|
|
__warningregistry__ = {}
|
2008-09-08 21:49:16 -03:00
|
|
|
with original_warnings.catch_warnings(record=True,
|
|
|
|
module=self.module) as w:
|
2008-04-12 20:44:07 -03:00
|
|
|
self.module.resetwarnings()
|
|
|
|
self.module.filterwarnings("once", category=UserWarning)
|
|
|
|
self.module.warn_explicit(message, UserWarning, "file", 42)
|
2008-09-08 21:49:16 -03:00
|
|
|
self.failUnlessEqual(w[-1].message, message)
|
|
|
|
del w[:]
|
2008-04-12 20:44:07 -03:00
|
|
|
self.module.warn_explicit(message, UserWarning, "file", 42)
|
2008-09-01 22:25:16 -03:00
|
|
|
self.assertEquals(len(w), 0)
|
2008-04-12 20:44:07 -03:00
|
|
|
# Test the resetting of onceregistry.
|
|
|
|
self.module.onceregistry = {}
|
|
|
|
__warningregistry__ = {}
|
|
|
|
self.module.warn('onceregistry test')
|
2008-09-08 21:49:16 -03:00
|
|
|
self.failUnlessEqual(w[-1].message.args, message.args)
|
2008-04-12 20:44:07 -03:00
|
|
|
# Removal of onceregistry is okay.
|
2008-09-08 21:49:16 -03:00
|
|
|
del w[:]
|
2008-04-12 20:44:07 -03:00
|
|
|
del self.module.onceregistry
|
|
|
|
__warningregistry__ = {}
|
|
|
|
self.module.warn_explicit(message, UserWarning, "file", 42)
|
2008-09-01 22:25:16 -03:00
|
|
|
self.assertEquals(len(w), 0)
|
2008-04-12 20:44:07 -03:00
|
|
|
finally:
|
|
|
|
self.module.onceregistry = original_registry
|
|
|
|
|
2009-04-01 15:03:59 -03:00
|
|
|
def test_default_action(self):
|
|
|
|
# Replacing or removing defaultaction should be okay.
|
|
|
|
message = UserWarning("defaultaction test")
|
|
|
|
original = self.module.defaultaction
|
|
|
|
try:
|
|
|
|
with original_warnings.catch_warnings(record=True,
|
|
|
|
module=self.module) as w:
|
|
|
|
self.module.resetwarnings()
|
|
|
|
registry = {}
|
|
|
|
self.module.warn_explicit(message, UserWarning, "<test>", 42,
|
|
|
|
registry=registry)
|
|
|
|
self.assertEqual(w[-1].message, message)
|
|
|
|
self.assertEqual(len(w), 1)
|
|
|
|
self.assertEqual(len(registry), 1)
|
|
|
|
del w[:]
|
|
|
|
# Test removal.
|
|
|
|
del self.module.defaultaction
|
|
|
|
__warningregistry__ = {}
|
|
|
|
registry = {}
|
|
|
|
self.module.warn_explicit(message, UserWarning, "<test>", 43,
|
|
|
|
registry=registry)
|
|
|
|
self.assertEqual(w[-1].message, message)
|
|
|
|
self.assertEqual(len(w), 1)
|
|
|
|
self.assertEqual(len(registry), 1)
|
|
|
|
del w[:]
|
|
|
|
# Test setting.
|
|
|
|
self.module.defaultaction = "ignore"
|
|
|
|
__warningregistry__ = {}
|
|
|
|
registry = {}
|
|
|
|
self.module.warn_explicit(message, UserWarning, "<test>", 44,
|
|
|
|
registry=registry)
|
|
|
|
self.assertEqual(len(w), 0)
|
|
|
|
finally:
|
|
|
|
self.module.defaultaction = original
|
|
|
|
|
2008-04-12 20:44:07 -03:00
|
|
|
def test_showwarning_missing(self):
|
|
|
|
# Test that showwarning() missing is okay.
|
|
|
|
text = 'del showwarning test'
|
2008-09-08 21:49:16 -03:00
|
|
|
with original_warnings.catch_warnings(module=self.module):
|
2008-04-12 20:44:07 -03:00
|
|
|
self.module.filterwarnings("always", category=UserWarning)
|
|
|
|
del self.module.showwarning
|
|
|
|
with test_support.captured_output('stderr') as stream:
|
|
|
|
self.module.warn(text)
|
|
|
|
result = stream.getvalue()
|
|
|
|
self.failUnless(text in result)
|
|
|
|
|
2008-05-06 19:18:11 -03:00
|
|
|
def test_showwarning_not_callable(self):
|
2009-04-01 17:25:48 -03:00
|
|
|
with original_warnings.catch_warnings(module=self.module):
|
|
|
|
self.module.filterwarnings("always", category=UserWarning)
|
|
|
|
old_showwarning = self.module.showwarning
|
|
|
|
self.module.showwarning = 23
|
|
|
|
try:
|
|
|
|
self.assertRaises(TypeError, self.module.warn, "Warning!")
|
|
|
|
finally:
|
|
|
|
self.module.showwarning = old_showwarning
|
2008-05-06 19:18:11 -03:00
|
|
|
|
2008-04-12 20:44:07 -03:00
|
|
|
def test_show_warning_output(self):
|
|
|
|
# With showarning() missing, make sure that output is okay.
|
|
|
|
text = 'test show_warning'
|
2008-09-08 21:49:16 -03:00
|
|
|
with original_warnings.catch_warnings(module=self.module):
|
2008-04-12 20:44:07 -03:00
|
|
|
self.module.filterwarnings("always", category=UserWarning)
|
|
|
|
del self.module.showwarning
|
|
|
|
with test_support.captured_output('stderr') as stream:
|
|
|
|
warning_tests.inner(text)
|
|
|
|
result = stream.getvalue()
|
|
|
|
self.failUnlessEqual(result.count('\n'), 2,
|
|
|
|
"Too many newlines in %r" % result)
|
|
|
|
first_line, second_line = result.split('\n', 1)
|
|
|
|
expected_file = os.path.splitext(warning_tests.__file__)[0] + '.py'
|
2008-04-13 14:41:31 -03:00
|
|
|
first_line_parts = first_line.rsplit(':', 3)
|
2008-04-13 14:09:43 -03:00
|
|
|
path, line, warning_class, message = first_line_parts
|
2008-04-12 20:44:07 -03:00
|
|
|
line = int(line)
|
|
|
|
self.failUnlessEqual(expected_file, path)
|
|
|
|
self.failUnlessEqual(warning_class, ' ' + UserWarning.__name__)
|
|
|
|
self.failUnlessEqual(message, ' ' + text)
|
|
|
|
expected_line = ' ' + linecache.getline(path, line).strip() + '\n'
|
|
|
|
assert expected_line
|
|
|
|
self.failUnlessEqual(second_line, expected_line)
|
2007-04-03 13:53:43 -03:00
|
|
|
|
2006-06-22 13:49:14 -03:00
|
|
|
|
2007-12-20 06:09:52 -04:00
|
|
|
class WarningsDisplayTests(unittest.TestCase):
|
|
|
|
|
2008-04-12 20:44:07 -03:00
|
|
|
"""Test the displaying of warnings and the ability to overload functions
|
|
|
|
related to displaying warnings."""
|
|
|
|
|
2007-12-20 06:09:52 -04:00
|
|
|
def test_formatwarning(self):
|
|
|
|
message = "msg"
|
|
|
|
category = Warning
|
|
|
|
file_name = os.path.splitext(warning_tests.__file__)[0] + '.py'
|
|
|
|
line_num = 3
|
|
|
|
file_line = linecache.getline(file_name, line_num).strip()
|
2008-04-12 20:44:07 -03:00
|
|
|
format = "%s:%s: %s: %s\n %s\n"
|
|
|
|
expect = format % (file_name, line_num, category.__name__, message,
|
|
|
|
file_line)
|
|
|
|
self.failUnlessEqual(expect, self.module.formatwarning(message,
|
|
|
|
category, file_name, line_num))
|
|
|
|
# Test the 'line' argument.
|
|
|
|
file_line += " for the win!"
|
|
|
|
expect = format % (file_name, line_num, category.__name__, message,
|
|
|
|
file_line)
|
|
|
|
self.failUnlessEqual(expect, self.module.formatwarning(message,
|
|
|
|
category, file_name, line_num, file_line))
|
2007-12-20 06:09:52 -04:00
|
|
|
|
|
|
|
def test_showwarning(self):
|
|
|
|
file_name = os.path.splitext(warning_tests.__file__)[0] + '.py'
|
|
|
|
line_num = 3
|
|
|
|
expected_file_line = linecache.getline(file_name, line_num).strip()
|
|
|
|
message = 'msg'
|
|
|
|
category = Warning
|
|
|
|
file_object = StringIO.StringIO()
|
2008-04-12 20:44:07 -03:00
|
|
|
expect = self.module.formatwarning(message, category, file_name,
|
|
|
|
line_num)
|
|
|
|
self.module.showwarning(message, category, file_name, line_num,
|
2007-12-20 06:09:52 -04:00
|
|
|
file_object)
|
|
|
|
self.failUnlessEqual(file_object.getvalue(), expect)
|
2008-04-12 20:44:07 -03:00
|
|
|
# Test 'line' argument.
|
|
|
|
expected_file_line += "for the win!"
|
|
|
|
expect = self.module.formatwarning(message, category, file_name,
|
|
|
|
line_num, expected_file_line)
|
|
|
|
file_object = StringIO.StringIO()
|
|
|
|
self.module.showwarning(message, category, file_name, line_num,
|
|
|
|
file_object, expected_file_line)
|
|
|
|
self.failUnlessEqual(expect, file_object.getvalue())
|
|
|
|
|
2008-04-12 23:42:36 -03:00
|
|
|
class CWarningsDisplayTests(BaseTest, WarningsDisplayTests):
|
2008-04-12 20:44:07 -03:00
|
|
|
module = c_warnings
|
2007-12-20 06:09:52 -04:00
|
|
|
|
2008-04-12 23:42:36 -03:00
|
|
|
class PyWarningsDisplayTests(BaseTest, WarningsDisplayTests):
|
2008-04-12 20:44:07 -03:00
|
|
|
module = py_warnings
|
2007-12-20 06:09:52 -04:00
|
|
|
|
2008-04-12 20:44:07 -03:00
|
|
|
|
2008-09-01 22:25:16 -03:00
|
|
|
class CatchWarningTests(BaseTest):
|
2008-07-13 09:23:47 -03:00
|
|
|
|
2008-09-01 22:25:16 -03:00
|
|
|
"""Test catch_warnings()."""
|
|
|
|
|
|
|
|
def test_catch_warnings_restore(self):
|
2008-07-13 09:23:47 -03:00
|
|
|
wmod = self.module
|
|
|
|
orig_filters = wmod.filters
|
|
|
|
orig_showwarning = wmod.showwarning
|
2008-09-11 09:11:06 -03:00
|
|
|
# Ensure both showwarning and filters are restored when recording
|
|
|
|
with wmod.catch_warnings(module=wmod, record=True):
|
2008-07-13 09:23:47 -03:00
|
|
|
wmod.filters = wmod.showwarning = object()
|
|
|
|
self.assert_(wmod.filters is orig_filters)
|
|
|
|
self.assert_(wmod.showwarning is orig_showwarning)
|
2008-09-11 09:11:06 -03:00
|
|
|
# Same test, but with recording disabled
|
2008-09-01 22:25:16 -03:00
|
|
|
with wmod.catch_warnings(module=wmod, record=False):
|
2008-07-13 09:23:47 -03:00
|
|
|
wmod.filters = wmod.showwarning = object()
|
|
|
|
self.assert_(wmod.filters is orig_filters)
|
|
|
|
self.assert_(wmod.showwarning is orig_showwarning)
|
|
|
|
|
2008-09-01 22:25:16 -03:00
|
|
|
def test_catch_warnings_recording(self):
|
2008-07-13 09:23:47 -03:00
|
|
|
wmod = self.module
|
2008-09-11 09:11:06 -03:00
|
|
|
# Ensure warnings are recorded when requested
|
2008-09-01 22:25:16 -03:00
|
|
|
with wmod.catch_warnings(module=wmod, record=True) as w:
|
|
|
|
self.assertEqual(w, [])
|
2008-09-11 09:11:06 -03:00
|
|
|
self.assert_(type(w) is list)
|
2008-07-13 09:23:47 -03:00
|
|
|
wmod.simplefilter("always")
|
|
|
|
wmod.warn("foo")
|
2008-09-08 21:49:16 -03:00
|
|
|
self.assertEqual(str(w[-1].message), "foo")
|
2008-07-13 09:23:47 -03:00
|
|
|
wmod.warn("bar")
|
2008-09-08 21:49:16 -03:00
|
|
|
self.assertEqual(str(w[-1].message), "bar")
|
2008-09-01 22:25:16 -03:00
|
|
|
self.assertEqual(str(w[0].message), "foo")
|
|
|
|
self.assertEqual(str(w[1].message), "bar")
|
2008-09-08 21:49:16 -03:00
|
|
|
del w[:]
|
2008-09-01 22:25:16 -03:00
|
|
|
self.assertEqual(w, [])
|
2008-09-11 09:11:06 -03:00
|
|
|
# Ensure warnings are not recorded when not requested
|
2008-07-13 09:23:47 -03:00
|
|
|
orig_showwarning = wmod.showwarning
|
2008-09-01 22:25:16 -03:00
|
|
|
with wmod.catch_warnings(module=wmod, record=False) as w:
|
2008-07-13 09:23:47 -03:00
|
|
|
self.assert_(w is None)
|
|
|
|
self.assert_(wmod.showwarning is orig_showwarning)
|
|
|
|
|
2008-09-11 09:11:06 -03:00
|
|
|
def test_catch_warnings_reentry_guard(self):
|
|
|
|
wmod = self.module
|
|
|
|
# Ensure catch_warnings is protected against incorrect usage
|
|
|
|
x = wmod.catch_warnings(module=wmod, record=True)
|
|
|
|
self.assertRaises(RuntimeError, x.__exit__)
|
|
|
|
with x:
|
|
|
|
self.assertRaises(RuntimeError, x.__enter__)
|
|
|
|
# Same test, but with recording disabled
|
|
|
|
x = wmod.catch_warnings(module=wmod, record=False)
|
|
|
|
self.assertRaises(RuntimeError, x.__exit__)
|
|
|
|
with x:
|
|
|
|
self.assertRaises(RuntimeError, x.__enter__)
|
|
|
|
|
|
|
|
def test_catch_warnings_defaults(self):
|
|
|
|
wmod = self.module
|
|
|
|
orig_filters = wmod.filters
|
|
|
|
orig_showwarning = wmod.showwarning
|
|
|
|
# Ensure default behaviour is not to record warnings
|
|
|
|
with wmod.catch_warnings(module=wmod) as w:
|
|
|
|
self.assert_(w is None)
|
|
|
|
self.assert_(wmod.showwarning is orig_showwarning)
|
|
|
|
self.assert_(wmod.filters is not orig_filters)
|
|
|
|
self.assert_(wmod.filters is orig_filters)
|
|
|
|
if wmod is sys.modules['warnings']:
|
|
|
|
# Ensure the default module is this one
|
|
|
|
with wmod.catch_warnings() as w:
|
|
|
|
self.assert_(w is None)
|
|
|
|
self.assert_(wmod.showwarning is orig_showwarning)
|
|
|
|
self.assert_(wmod.filters is not orig_filters)
|
|
|
|
self.assert_(wmod.filters is orig_filters)
|
|
|
|
|
|
|
|
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, [])
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-09-01 22:25:16 -03:00
|
|
|
class CCatchWarningTests(CatchWarningTests):
|
2008-07-13 09:23:47 -03:00
|
|
|
module = c_warnings
|
|
|
|
|
2008-09-01 22:25:16 -03:00
|
|
|
class PyCatchWarningTests(CatchWarningTests):
|
2008-07-13 09:23:47 -03:00
|
|
|
module = py_warnings
|
|
|
|
|
|
|
|
|
2008-04-12 20:44:07 -03:00
|
|
|
def test_main():
|
2008-04-18 20:31:33 -03:00
|
|
|
py_warnings.onceregistry.clear()
|
|
|
|
c_warnings.onceregistry.clear()
|
2008-09-01 22:25:16 -03:00
|
|
|
test_support.run_unittest(CFilterTests, PyFilterTests,
|
|
|
|
CWarnTests, PyWarnTests,
|
2008-04-12 20:44:07 -03:00
|
|
|
CWCmdLineTests, PyWCmdLineTests,
|
|
|
|
_WarningsTests,
|
|
|
|
CWarningsDisplayTests, PyWarningsDisplayTests,
|
2008-09-01 22:25:16 -03:00
|
|
|
CCatchWarningTests, PyCatchWarningTests,
|
2008-04-12 20:44:07 -03:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2003-07-13 05:37:40 -03:00
|
|
|
if __name__ == "__main__":
|
2008-04-12 20:44:07 -03:00
|
|
|
test_main()
|