Jeremy Hylton | 8501466 | 2003-07-11 15:37:59 +0000 | [diff] [blame] | 1 | import warnings |
Raymond Hettinger | dc9dcf1 | 2003-07-13 06:15:11 +0000 | [diff] [blame] | 2 | import os |
Guido van Rossum | 61e21b5 | 2007-08-20 19:06:03 +0000 | [diff] [blame] | 3 | import sys |
Raymond Hettinger | d6f6e50 | 2003-07-13 08:37:40 +0000 | [diff] [blame] | 4 | import unittest |
| 5 | from test import test_support |
Jeremy Hylton | 8501466 | 2003-07-11 15:37:59 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 7 | from test import warning_tests |
Jeremy Hylton | 8501466 | 2003-07-11 15:37:59 +0000 | [diff] [blame] | 8 | |
Raymond Hettinger | d6f6e50 | 2003-07-13 08:37:40 +0000 | [diff] [blame] | 9 | class TestModule(unittest.TestCase): |
Raymond Hettinger | d6f6e50 | 2003-07-13 08:37:40 +0000 | [diff] [blame] | 10 | def setUp(self): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 11 | self.ignored = [w[2].__name__ for w in warnings.filters |
Raymond Hettinger | d6f6e50 | 2003-07-13 08:37:40 +0000 | [diff] [blame] | 12 | if w[0]=='ignore' and w[1] is None and w[3] is None] |
Jeremy Hylton | 8501466 | 2003-07-11 15:37:59 +0000 | [diff] [blame] | 13 | |
Raymond Hettinger | d6f6e50 | 2003-07-13 08:37:40 +0000 | [diff] [blame] | 14 | def test_warn_default_category(self): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 15 | with test_support.catch_warning() as w: |
| 16 | for i in range(4): |
| 17 | text = 'multi %d' %i # Different text on each call |
| 18 | warnings.warn(text) |
| 19 | self.assertEqual(str(w.message), text) |
| 20 | self.assert_(w.category is UserWarning) |
Raymond Hettinger | d6f6e50 | 2003-07-13 08:37:40 +0000 | [diff] [blame] | 21 | |
| 22 | def test_warn_specific_category(self): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 23 | with test_support.catch_warning() as w: |
| 24 | text = 'None' |
| 25 | for category in [DeprecationWarning, FutureWarning, |
| 26 | PendingDeprecationWarning, RuntimeWarning, |
| 27 | SyntaxWarning, UserWarning, Warning]: |
| 28 | if category.__name__ in self.ignored: |
| 29 | text = 'filtered out' + category.__name__ |
| 30 | warnings.warn(text, category) |
| 31 | self.assertNotEqual(w.message, text) |
| 32 | else: |
| 33 | text = 'unfiltered %s' % category.__name__ |
| 34 | warnings.warn(text, category) |
| 35 | self.assertEqual(str(w.message), text) |
| 36 | self.assert_(w.category is category) |
Raymond Hettinger | d6f6e50 | 2003-07-13 08:37:40 +0000 | [diff] [blame] | 37 | |
| 38 | def test_filtering(self): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 39 | with test_support.catch_warning() as w: |
| 40 | warnings.filterwarnings("error", "", Warning, "", 0) |
| 41 | self.assertRaises(UserWarning, warnings.warn, 'convert to error') |
Raymond Hettinger | d6f6e50 | 2003-07-13 08:37:40 +0000 | [diff] [blame] | 42 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 43 | warnings.resetwarnings() |
| 44 | text = 'handle normally' |
| 45 | warnings.warn(text) |
| 46 | self.assertEqual(str(w.message), text) |
| 47 | self.assert_(w.category is UserWarning) |
Raymond Hettinger | d6f6e50 | 2003-07-13 08:37:40 +0000 | [diff] [blame] | 48 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 49 | warnings.filterwarnings("ignore", "", Warning, "", 0) |
| 50 | text = 'filtered out' |
| 51 | warnings.warn(text) |
| 52 | self.assertNotEqual(str(w.message), text) |
Raymond Hettinger | d6f6e50 | 2003-07-13 08:37:40 +0000 | [diff] [blame] | 53 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 54 | warnings.resetwarnings() |
| 55 | warnings.filterwarnings("error", "hex*", Warning, "", 0) |
| 56 | self.assertRaises(UserWarning, warnings.warn, 'hex/oct') |
| 57 | text = 'nonmatching text' |
| 58 | warnings.warn(text) |
| 59 | self.assertEqual(str(w.message), text) |
| 60 | self.assert_(w.category is UserWarning) |
Raymond Hettinger | d6f6e50 | 2003-07-13 08:37:40 +0000 | [diff] [blame] | 61 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 62 | def test_options(self): |
| 63 | # Uses the private _setoption() function to test the parsing |
| 64 | # of command-line warning arguments |
Guido van Rossum | af554a0 | 2007-08-16 23:48:43 +0000 | [diff] [blame] | 65 | with test_support.catch_warning(): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 66 | self.assertRaises(warnings._OptionError, |
| 67 | warnings._setoption, '1:2:3:4:5:6') |
| 68 | self.assertRaises(warnings._OptionError, |
| 69 | warnings._setoption, 'bogus::Warning') |
| 70 | self.assertRaises(warnings._OptionError, |
| 71 | warnings._setoption, 'ignore:2::4:-5') |
| 72 | warnings._setoption('error::Warning::0') |
| 73 | self.assertRaises(UserWarning, warnings.warn, 'convert to error') |
| 74 | |
| 75 | def test_filename(self): |
| 76 | with test_support.catch_warning() as w: |
| 77 | warning_tests.inner("spam1") |
| 78 | self.assertEqual(os.path.basename(w.filename), "warning_tests.py") |
| 79 | warning_tests.outer("spam2") |
| 80 | self.assertEqual(os.path.basename(w.filename), "warning_tests.py") |
| 81 | |
| 82 | def test_stacklevel(self): |
| 83 | # Test stacklevel argument |
| 84 | # make sure all messages are different, so the warning won't be skipped |
| 85 | with test_support.catch_warning() as w: |
| 86 | warning_tests.inner("spam3", stacklevel=1) |
| 87 | self.assertEqual(os.path.basename(w.filename), "warning_tests.py") |
| 88 | warning_tests.outer("spam4", stacklevel=1) |
| 89 | self.assertEqual(os.path.basename(w.filename), "warning_tests.py") |
| 90 | |
| 91 | warning_tests.inner("spam5", stacklevel=2) |
| 92 | self.assertEqual(os.path.basename(w.filename), "test_warnings.py") |
| 93 | warning_tests.outer("spam6", stacklevel=2) |
| 94 | self.assertEqual(os.path.basename(w.filename), "warning_tests.py") |
| 95 | |
| 96 | warning_tests.inner("spam7", stacklevel=9999) |
| 97 | self.assertEqual(os.path.basename(w.filename), "sys") |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 98 | |
| 99 | |
Raymond Hettinger | d6f6e50 | 2003-07-13 08:37:40 +0000 | [diff] [blame] | 100 | def test_main(verbose=None): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 101 | # Obscure hack so that this test passes after reloads or repeated calls |
| 102 | # to test_main (regrtest -R). |
| 103 | if '__warningregistry__' in globals(): |
| 104 | del globals()['__warningregistry__'] |
Guido van Rossum | 61e21b5 | 2007-08-20 19:06:03 +0000 | [diff] [blame] | 105 | if hasattr(warning_tests, '__warningregistry__'): |
| 106 | del warning_tests.__warningregistry__ |
| 107 | if hasattr(sys, '__warningregistry__'): |
| 108 | del sys.__warningregistry__ |
Raymond Hettinger | d6f6e50 | 2003-07-13 08:37:40 +0000 | [diff] [blame] | 109 | test_support.run_unittest(TestModule) |
| 110 | |
| 111 | if __name__ == "__main__": |
| 112 | test_main(verbose=True) |