| 1 | import warnings
|
|---|
| 2 | import os
|
|---|
| 3 | import unittest
|
|---|
| 4 | from test import test_support
|
|---|
| 5 |
|
|---|
| 6 | # The warnings module isn't easily tested, because it relies on module
|
|---|
| 7 | # globals to store configuration information. setUp() and tearDown()
|
|---|
| 8 | # preserve the current settings to avoid bashing them while running tests.
|
|---|
| 9 |
|
|---|
| 10 | # To capture the warning messages, a replacement for showwarning() is
|
|---|
| 11 | # used to save warning information in a global variable.
|
|---|
| 12 |
|
|---|
| 13 | class WarningMessage:
|
|---|
| 14 | "Holds results of latest showwarning() call"
|
|---|
| 15 | pass
|
|---|
| 16 |
|
|---|
| 17 | def showwarning(message, category, filename, lineno, file=None):
|
|---|
| 18 | msg.message = str(message)
|
|---|
| 19 | msg.category = category.__name__
|
|---|
| 20 | msg.filename = os.path.basename(filename)
|
|---|
| 21 | msg.lineno = lineno
|
|---|
| 22 |
|
|---|
| 23 | class TestModule(unittest.TestCase):
|
|---|
| 24 |
|
|---|
| 25 | def setUp(self):
|
|---|
| 26 | global msg
|
|---|
| 27 | msg = WarningMessage()
|
|---|
| 28 | self._filters = warnings.filters[:]
|
|---|
| 29 | self._showwarning = warnings.showwarning
|
|---|
| 30 | warnings.showwarning = showwarning
|
|---|
| 31 | self.ignored = [w[2].__name__ for w in self._filters
|
|---|
| 32 | if w[0]=='ignore' and w[1] is None and w[3] is None]
|
|---|
| 33 |
|
|---|
| 34 | def tearDown(self):
|
|---|
| 35 | warnings.filters = self._filters[:]
|
|---|
| 36 | warnings.showwarning = self._showwarning
|
|---|
| 37 |
|
|---|
| 38 | def test_warn_default_category(self):
|
|---|
| 39 | for i in range(4):
|
|---|
| 40 | text = 'multi %d' %i # Different text on each call
|
|---|
| 41 | warnings.warn(text)
|
|---|
| 42 | self.assertEqual(msg.message, text)
|
|---|
| 43 | self.assertEqual(msg.category, 'UserWarning')
|
|---|
| 44 |
|
|---|
| 45 | def test_warn_specific_category(self):
|
|---|
| 46 | text = 'None'
|
|---|
| 47 | for category in [DeprecationWarning, FutureWarning,
|
|---|
| 48 | PendingDeprecationWarning, RuntimeWarning,
|
|---|
| 49 | SyntaxWarning, UserWarning, Warning]:
|
|---|
| 50 | if category.__name__ in self.ignored:
|
|---|
| 51 | text = 'filtered out' + category.__name__
|
|---|
| 52 | warnings.warn(text, category)
|
|---|
| 53 | self.assertNotEqual(msg.message, text)
|
|---|
| 54 | else:
|
|---|
| 55 | text = 'unfiltered %s' % category.__name__
|
|---|
| 56 | warnings.warn(text, category)
|
|---|
| 57 | self.assertEqual(msg.message, text)
|
|---|
| 58 | self.assertEqual(msg.category, category.__name__)
|
|---|
| 59 |
|
|---|
| 60 | def test_filtering(self):
|
|---|
| 61 |
|
|---|
| 62 | warnings.filterwarnings("error", "", Warning, "", 0)
|
|---|
| 63 | self.assertRaises(UserWarning, warnings.warn, 'convert to error')
|
|---|
| 64 |
|
|---|
| 65 | warnings.resetwarnings()
|
|---|
| 66 | text = 'handle normally'
|
|---|
| 67 | warnings.warn(text)
|
|---|
| 68 | self.assertEqual(msg.message, text)
|
|---|
| 69 | self.assertEqual(msg.category, 'UserWarning')
|
|---|
| 70 |
|
|---|
| 71 | warnings.filterwarnings("ignore", "", Warning, "", 0)
|
|---|
| 72 | text = 'filtered out'
|
|---|
| 73 | warnings.warn(text)
|
|---|
| 74 | self.assertNotEqual(msg.message, text)
|
|---|
| 75 |
|
|---|
| 76 | warnings.resetwarnings()
|
|---|
| 77 | warnings.filterwarnings("error", "hex*", Warning, "", 0)
|
|---|
| 78 | self.assertRaises(UserWarning, warnings.warn, 'hex/oct')
|
|---|
| 79 | text = 'nonmatching text'
|
|---|
| 80 | warnings.warn(text)
|
|---|
| 81 | self.assertEqual(msg.message, text)
|
|---|
| 82 | self.assertEqual(msg.category, 'UserWarning')
|
|---|
| 83 |
|
|---|
| 84 | def test_options(self):
|
|---|
| 85 | # Uses the private _setoption() function to test the parsing
|
|---|
| 86 | # of command-line warning arguments
|
|---|
| 87 | self.assertRaises(warnings._OptionError,
|
|---|
| 88 | warnings._setoption, '1:2:3:4:5:6')
|
|---|
| 89 | self.assertRaises(warnings._OptionError,
|
|---|
| 90 | warnings._setoption, 'bogus::Warning')
|
|---|
| 91 | self.assertRaises(warnings._OptionError,
|
|---|
| 92 | warnings._setoption, 'ignore:2::4:-5')
|
|---|
| 93 | warnings._setoption('error::Warning::0')
|
|---|
| 94 | self.assertRaises(UserWarning, warnings.warn, 'convert to error')
|
|---|
| 95 |
|
|---|
| 96 |
|
|---|
| 97 | def test_main(verbose=None):
|
|---|
| 98 | # Obscure hack so that this test passes after reloads or repeated calls
|
|---|
| 99 | # to test_main (regrtest -R).
|
|---|
| 100 | if '__warningregistry__' in globals():
|
|---|
| 101 | del globals()['__warningregistry__']
|
|---|
| 102 | test_support.run_unittest(TestModule)
|
|---|
| 103 |
|
|---|
| 104 | if __name__ == "__main__":
|
|---|
| 105 | test_main(verbose=True)
|
|---|