2003-07-11 12:37:59 -03:00
|
|
|
import warnings
|
2003-07-13 03:15:11 -03:00
|
|
|
import os
|
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
|
|
|
|
|
2007-04-03 15:33:29 -03:00
|
|
|
class TestModule(unittest.TestCase):
|
2003-07-13 05:37:40 -03:00
|
|
|
def setUp(self):
|
2007-04-03 15:33:29 -03:00
|
|
|
self.ignored = [w[2].__name__ for w in warnings.filters
|
2003-07-13 05:37:40 -03:00
|
|
|
if w[0]=='ignore' and w[1] is None and w[3] is None]
|
|
|
|
|
|
|
|
def test_warn_default_category(self):
|
2007-04-03 15:33:29 -03:00
|
|
|
with test_support.catch_warning() as w:
|
|
|
|
for i in range(4):
|
|
|
|
text = 'multi %d' %i # Different text on each call
|
|
|
|
warnings.warn(text)
|
|
|
|
self.assertEqual(str(w.message), text)
|
|
|
|
self.assert_(w.category is UserWarning)
|
2003-07-13 05:37:40 -03:00
|
|
|
|
|
|
|
def test_warn_specific_category(self):
|
2007-04-03 15:33:29 -03:00
|
|
|
with test_support.catch_warning() as w:
|
|
|
|
text = 'None'
|
|
|
|
for category in [DeprecationWarning, FutureWarning,
|
|
|
|
PendingDeprecationWarning, RuntimeWarning,
|
|
|
|
SyntaxWarning, UserWarning, Warning]:
|
|
|
|
if category.__name__ in self.ignored:
|
|
|
|
text = 'filtered out' + category.__name__
|
|
|
|
warnings.warn(text, category)
|
|
|
|
self.assertNotEqual(w.message, text)
|
|
|
|
else:
|
|
|
|
text = 'unfiltered %s' % category.__name__
|
|
|
|
warnings.warn(text, category)
|
|
|
|
self.assertEqual(str(w.message), text)
|
|
|
|
self.assert_(w.category is category)
|
2003-07-13 05:37:40 -03:00
|
|
|
|
|
|
|
def test_filtering(self):
|
2007-04-03 15:33:29 -03:00
|
|
|
with test_support.catch_warning() as w:
|
|
|
|
warnings.filterwarnings("error", "", Warning, "", 0)
|
|
|
|
self.assertRaises(UserWarning, warnings.warn, 'convert to error')
|
2003-07-13 05:37:40 -03:00
|
|
|
|
2007-04-03 15:33:29 -03:00
|
|
|
warnings.resetwarnings()
|
|
|
|
text = 'handle normally'
|
|
|
|
warnings.warn(text)
|
|
|
|
self.assertEqual(str(w.message), text)
|
|
|
|
self.assert_(w.category is UserWarning)
|
2003-07-13 05:37:40 -03:00
|
|
|
|
2007-04-03 15:33:29 -03:00
|
|
|
warnings.filterwarnings("ignore", "", Warning, "", 0)
|
|
|
|
text = 'filtered out'
|
|
|
|
warnings.warn(text)
|
|
|
|
self.assertNotEqual(str(w.message), text)
|
2003-07-13 05:37:40 -03:00
|
|
|
|
2007-04-03 15:33:29 -03:00
|
|
|
warnings.resetwarnings()
|
|
|
|
warnings.filterwarnings("error", "hex*", Warning, "", 0)
|
|
|
|
self.assertRaises(UserWarning, warnings.warn, 'hex/oct')
|
|
|
|
text = 'nonmatching text'
|
|
|
|
warnings.warn(text)
|
|
|
|
self.assertEqual(str(w.message), text)
|
|
|
|
self.assert_(w.category is UserWarning)
|
2003-07-13 05:37:40 -03:00
|
|
|
|
2006-06-22 13:49:14 -03:00
|
|
|
def test_options(self):
|
|
|
|
# Uses the private _setoption() function to test the parsing
|
|
|
|
# of command-line warning arguments
|
2007-08-14 02:51:06 -03:00
|
|
|
with test_support.catch_warning():
|
2007-04-03 15:33:29 -03:00
|
|
|
self.assertRaises(warnings._OptionError,
|
|
|
|
warnings._setoption, '1:2:3:4:5:6')
|
|
|
|
self.assertRaises(warnings._OptionError,
|
|
|
|
warnings._setoption, 'bogus::Warning')
|
|
|
|
self.assertRaises(warnings._OptionError,
|
|
|
|
warnings._setoption, 'ignore:2::4:-5')
|
|
|
|
warnings._setoption('error::Warning::0')
|
|
|
|
self.assertRaises(UserWarning, warnings.warn, 'convert to error')
|
2006-06-27 08:52:49 -03:00
|
|
|
|
2007-04-03 13:53:43 -03:00
|
|
|
def test_filename(self):
|
2007-04-03 15:33:29 -03:00
|
|
|
with test_support.catch_warning() as w:
|
|
|
|
warning_tests.inner("spam1")
|
|
|
|
self.assertEqual(os.path.basename(w.filename), "warning_tests.py")
|
|
|
|
warning_tests.outer("spam2")
|
|
|
|
self.assertEqual(os.path.basename(w.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
|
2007-04-03 15:33:29 -03:00
|
|
|
with test_support.catch_warning() as w:
|
|
|
|
warning_tests.inner("spam3", stacklevel=1)
|
|
|
|
self.assertEqual(os.path.basename(w.filename), "warning_tests.py")
|
|
|
|
warning_tests.outer("spam4", stacklevel=1)
|
|
|
|
self.assertEqual(os.path.basename(w.filename), "warning_tests.py")
|
|
|
|
|
|
|
|
warning_tests.inner("spam5", stacklevel=2)
|
|
|
|
self.assertEqual(os.path.basename(w.filename), "test_warnings.py")
|
|
|
|
warning_tests.outer("spam6", stacklevel=2)
|
|
|
|
self.assertEqual(os.path.basename(w.filename), "warning_tests.py")
|
|
|
|
|
|
|
|
warning_tests.inner("spam7", stacklevel=9999)
|
|
|
|
self.assertEqual(os.path.basename(w.filename), "sys")
|
2007-04-03 13:53:43 -03:00
|
|
|
|
2006-06-22 13:49:14 -03:00
|
|
|
|
2003-07-13 05:37:40 -03:00
|
|
|
def test_main(verbose=None):
|
2006-04-16 12:43:39 -03:00
|
|
|
# Obscure hack so that this test passes after reloads or repeated calls
|
|
|
|
# to test_main (regrtest -R).
|
|
|
|
if '__warningregistry__' in globals():
|
|
|
|
del globals()['__warningregistry__']
|
2007-08-17 17:16:15 -03:00
|
|
|
if hasattr(warning_tests, '__warningregistry__'):
|
|
|
|
del warning_tests.__warningregistry__
|
|
|
|
if hasattr(sys, '__warningregistry__'):
|
|
|
|
del sys.__warningregistry__
|
2003-07-13 05:37:40 -03:00
|
|
|
test_support.run_unittest(TestModule)
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
test_main(verbose=True)
|