Brett Cannon | e974689 | 2008-04-12 23:44:07 +0000 | [diff] [blame] | 1 | from contextlib import contextmanager |
Brett Cannon | 905c31c | 2007-12-20 10:09:52 +0000 | [diff] [blame] | 2 | import linecache |
Raymond Hettinger | dc9dcf1 | 2003-07-13 06:15:11 +0000 | [diff] [blame] | 3 | import os |
Brett Cannon | 905c31c | 2007-12-20 10:09:52 +0000 | [diff] [blame] | 4 | import StringIO |
Brett Cannon | 855da6c | 2007-08-17 20:16:15 +0000 | [diff] [blame] | 5 | import sys |
Raymond Hettinger | d6f6e50 | 2003-07-13 08:37:40 +0000 | [diff] [blame] | 6 | import unittest |
| 7 | from test import test_support |
Jeremy Hylton | 8501466 | 2003-07-11 15:37:59 +0000 | [diff] [blame] | 8 | |
Walter Dörwald | e1a9b42 | 2007-04-03 16:53:43 +0000 | [diff] [blame] | 9 | import warning_tests |
| 10 | |
Brett Cannon | 667bb4f | 2008-04-13 02:42:36 +0000 | [diff] [blame] | 11 | import warnings as original_warnings |
| 12 | |
Nick Coghlan | 5533ff6 | 2009-04-22 15:26:04 +0000 | [diff] [blame] | 13 | py_warnings = test_support.import_fresh_module('warnings', blocked=['_warnings']) |
| 14 | c_warnings = test_support.import_fresh_module('warnings', fresh=['_warnings']) |
Brett Cannon | 667bb4f | 2008-04-13 02:42:36 +0000 | [diff] [blame] | 15 | |
Brett Cannon | e974689 | 2008-04-12 23:44:07 +0000 | [diff] [blame] | 16 | @contextmanager |
| 17 | def warnings_state(module): |
| 18 | """Use a specific warnings implementation in warning_tests.""" |
| 19 | global __warningregistry__ |
| 20 | for to_clear in (sys, warning_tests): |
| 21 | try: |
| 22 | to_clear.__warningregistry__.clear() |
| 23 | except AttributeError: |
| 24 | pass |
| 25 | try: |
| 26 | __warningregistry__.clear() |
| 27 | except NameError: |
| 28 | pass |
| 29 | original_warnings = warning_tests.warnings |
| 30 | try: |
| 31 | warning_tests.warnings = module |
| 32 | yield |
| 33 | finally: |
| 34 | warning_tests.warnings = original_warnings |
| 35 | |
| 36 | |
Brett Cannon | 667bb4f | 2008-04-13 02:42:36 +0000 | [diff] [blame] | 37 | class BaseTest(unittest.TestCase): |
Brett Cannon | e974689 | 2008-04-12 23:44:07 +0000 | [diff] [blame] | 38 | |
Brett Cannon | 667bb4f | 2008-04-13 02:42:36 +0000 | [diff] [blame] | 39 | """Basic bookkeeping required for testing.""" |
Brett Cannon | e974689 | 2008-04-12 23:44:07 +0000 | [diff] [blame] | 40 | |
| 41 | def setUp(self): |
Brett Cannon | 667bb4f | 2008-04-13 02:42:36 +0000 | [diff] [blame] | 42 | # The __warningregistry__ needs to be in a pristine state for tests |
| 43 | # to work properly. |
| 44 | if '__warningregistry__' in globals(): |
| 45 | del globals()['__warningregistry__'] |
| 46 | if hasattr(warning_tests, '__warningregistry__'): |
| 47 | del warning_tests.__warningregistry__ |
| 48 | if hasattr(sys, '__warningregistry__'): |
| 49 | del sys.__warningregistry__ |
| 50 | # The 'warnings' module must be explicitly set so that the proper |
| 51 | # interaction between _warnings and 'warnings' can be controlled. |
| 52 | sys.modules['warnings'] = self.module |
| 53 | super(BaseTest, self).setUp() |
| 54 | |
| 55 | def tearDown(self): |
| 56 | sys.modules['warnings'] = original_warnings |
| 57 | super(BaseTest, self).tearDown() |
| 58 | |
| 59 | |
| 60 | class FilterTests(object): |
| 61 | |
| 62 | """Testing the filtering functionality.""" |
Brett Cannon | e974689 | 2008-04-12 23:44:07 +0000 | [diff] [blame] | 63 | |
| 64 | def test_error(self): |
Brett Cannon | 672237d | 2008-09-09 00:49:16 +0000 | [diff] [blame] | 65 | with original_warnings.catch_warnings(module=self.module) as w: |
Brett Cannon | e974689 | 2008-04-12 23:44:07 +0000 | [diff] [blame] | 66 | self.module.resetwarnings() |
| 67 | self.module.filterwarnings("error", category=UserWarning) |
| 68 | self.assertRaises(UserWarning, self.module.warn, |
| 69 | "FilterTests.test_error") |
| 70 | |
| 71 | def test_ignore(self): |
Brett Cannon | 672237d | 2008-09-09 00:49:16 +0000 | [diff] [blame] | 72 | with original_warnings.catch_warnings(record=True, |
| 73 | module=self.module) as w: |
Brett Cannon | e974689 | 2008-04-12 23:44:07 +0000 | [diff] [blame] | 74 | self.module.resetwarnings() |
| 75 | self.module.filterwarnings("ignore", category=UserWarning) |
| 76 | self.module.warn("FilterTests.test_ignore", UserWarning) |
Brett Cannon | 1eaf074 | 2008-09-02 01:25:16 +0000 | [diff] [blame] | 77 | self.assertEquals(len(w), 0) |
Brett Cannon | e974689 | 2008-04-12 23:44:07 +0000 | [diff] [blame] | 78 | |
| 79 | def test_always(self): |
Brett Cannon | 672237d | 2008-09-09 00:49:16 +0000 | [diff] [blame] | 80 | with original_warnings.catch_warnings(record=True, |
| 81 | module=self.module) as w: |
Brett Cannon | e974689 | 2008-04-12 23:44:07 +0000 | [diff] [blame] | 82 | self.module.resetwarnings() |
| 83 | self.module.filterwarnings("always", category=UserWarning) |
| 84 | message = "FilterTests.test_always" |
| 85 | self.module.warn(message, UserWarning) |
Brett Cannon | 672237d | 2008-09-09 00:49:16 +0000 | [diff] [blame] | 86 | self.assert_(message, w[-1].message) |
Brett Cannon | e974689 | 2008-04-12 23:44:07 +0000 | [diff] [blame] | 87 | self.module.warn(message, UserWarning) |
Brett Cannon | 672237d | 2008-09-09 00:49:16 +0000 | [diff] [blame] | 88 | self.assert_(w[-1].message, message) |
Brett Cannon | e974689 | 2008-04-12 23:44:07 +0000 | [diff] [blame] | 89 | |
| 90 | def test_default(self): |
Brett Cannon | 672237d | 2008-09-09 00:49:16 +0000 | [diff] [blame] | 91 | with original_warnings.catch_warnings(record=True, |
| 92 | module=self.module) as w: |
Brett Cannon | e974689 | 2008-04-12 23:44:07 +0000 | [diff] [blame] | 93 | self.module.resetwarnings() |
| 94 | self.module.filterwarnings("default", category=UserWarning) |
| 95 | message = UserWarning("FilterTests.test_default") |
| 96 | for x in xrange(2): |
| 97 | self.module.warn(message, UserWarning) |
| 98 | if x == 0: |
Brett Cannon | 672237d | 2008-09-09 00:49:16 +0000 | [diff] [blame] | 99 | self.assertEquals(w[-1].message, message) |
| 100 | del w[:] |
Brett Cannon | e974689 | 2008-04-12 23:44:07 +0000 | [diff] [blame] | 101 | elif x == 1: |
Brett Cannon | 672237d | 2008-09-09 00:49:16 +0000 | [diff] [blame] | 102 | self.assertEquals(len(w), 0) |
Brett Cannon | e974689 | 2008-04-12 23:44:07 +0000 | [diff] [blame] | 103 | else: |
| 104 | raise ValueError("loop variant unhandled") |
| 105 | |
| 106 | def test_module(self): |
Brett Cannon | 672237d | 2008-09-09 00:49:16 +0000 | [diff] [blame] | 107 | with original_warnings.catch_warnings(record=True, |
| 108 | module=self.module) as w: |
Brett Cannon | e974689 | 2008-04-12 23:44:07 +0000 | [diff] [blame] | 109 | self.module.resetwarnings() |
| 110 | self.module.filterwarnings("module", category=UserWarning) |
| 111 | message = UserWarning("FilterTests.test_module") |
| 112 | self.module.warn(message, UserWarning) |
Brett Cannon | 672237d | 2008-09-09 00:49:16 +0000 | [diff] [blame] | 113 | self.assertEquals(w[-1].message, message) |
| 114 | del w[:] |
Brett Cannon | e974689 | 2008-04-12 23:44:07 +0000 | [diff] [blame] | 115 | self.module.warn(message, UserWarning) |
Brett Cannon | 672237d | 2008-09-09 00:49:16 +0000 | [diff] [blame] | 116 | self.assertEquals(len(w), 0) |
Brett Cannon | e974689 | 2008-04-12 23:44:07 +0000 | [diff] [blame] | 117 | |
| 118 | def test_once(self): |
Brett Cannon | 672237d | 2008-09-09 00:49:16 +0000 | [diff] [blame] | 119 | with original_warnings.catch_warnings(record=True, |
| 120 | module=self.module) as w: |
Brett Cannon | e974689 | 2008-04-12 23:44:07 +0000 | [diff] [blame] | 121 | self.module.resetwarnings() |
| 122 | self.module.filterwarnings("once", category=UserWarning) |
| 123 | message = UserWarning("FilterTests.test_once") |
| 124 | self.module.warn_explicit(message, UserWarning, "test_warnings.py", |
| 125 | 42) |
Brett Cannon | 672237d | 2008-09-09 00:49:16 +0000 | [diff] [blame] | 126 | self.assertEquals(w[-1].message, message) |
| 127 | del w[:] |
Brett Cannon | e974689 | 2008-04-12 23:44:07 +0000 | [diff] [blame] | 128 | self.module.warn_explicit(message, UserWarning, "test_warnings.py", |
| 129 | 13) |
Brett Cannon | 1eaf074 | 2008-09-02 01:25:16 +0000 | [diff] [blame] | 130 | self.assertEquals(len(w), 0) |
Brett Cannon | e974689 | 2008-04-12 23:44:07 +0000 | [diff] [blame] | 131 | self.module.warn_explicit(message, UserWarning, "test_warnings2.py", |
| 132 | 42) |
Brett Cannon | 1eaf074 | 2008-09-02 01:25:16 +0000 | [diff] [blame] | 133 | self.assertEquals(len(w), 0) |
Brett Cannon | e974689 | 2008-04-12 23:44:07 +0000 | [diff] [blame] | 134 | |
| 135 | def test_inheritance(self): |
Brett Cannon | 672237d | 2008-09-09 00:49:16 +0000 | [diff] [blame] | 136 | with original_warnings.catch_warnings(module=self.module) as w: |
Brett Cannon | e974689 | 2008-04-12 23:44:07 +0000 | [diff] [blame] | 137 | self.module.resetwarnings() |
| 138 | self.module.filterwarnings("error", category=Warning) |
| 139 | self.assertRaises(UserWarning, self.module.warn, |
| 140 | "FilterTests.test_inheritance", UserWarning) |
| 141 | |
| 142 | def test_ordering(self): |
Brett Cannon | 672237d | 2008-09-09 00:49:16 +0000 | [diff] [blame] | 143 | with original_warnings.catch_warnings(record=True, |
| 144 | module=self.module) as w: |
Brett Cannon | e974689 | 2008-04-12 23:44:07 +0000 | [diff] [blame] | 145 | self.module.resetwarnings() |
| 146 | self.module.filterwarnings("ignore", category=UserWarning) |
| 147 | self.module.filterwarnings("error", category=UserWarning, |
| 148 | append=True) |
Brett Cannon | 672237d | 2008-09-09 00:49:16 +0000 | [diff] [blame] | 149 | del w[:] |
Brett Cannon | e974689 | 2008-04-12 23:44:07 +0000 | [diff] [blame] | 150 | try: |
| 151 | self.module.warn("FilterTests.test_ordering", UserWarning) |
| 152 | except UserWarning: |
| 153 | self.fail("order handling for actions failed") |
Brett Cannon | 1eaf074 | 2008-09-02 01:25:16 +0000 | [diff] [blame] | 154 | self.assertEquals(len(w), 0) |
Brett Cannon | e974689 | 2008-04-12 23:44:07 +0000 | [diff] [blame] | 155 | |
| 156 | def test_filterwarnings(self): |
| 157 | # Test filterwarnings(). |
| 158 | # Implicitly also tests resetwarnings(). |
Brett Cannon | 672237d | 2008-09-09 00:49:16 +0000 | [diff] [blame] | 159 | with original_warnings.catch_warnings(record=True, |
| 160 | module=self.module) as w: |
Brett Cannon | e974689 | 2008-04-12 23:44:07 +0000 | [diff] [blame] | 161 | self.module.filterwarnings("error", "", Warning, "", 0) |
| 162 | self.assertRaises(UserWarning, self.module.warn, 'convert to error') |
| 163 | |
| 164 | self.module.resetwarnings() |
| 165 | text = 'handle normally' |
| 166 | self.module.warn(text) |
Brett Cannon | 672237d | 2008-09-09 00:49:16 +0000 | [diff] [blame] | 167 | self.assertEqual(str(w[-1].message), text) |
| 168 | self.assert_(w[-1].category is UserWarning) |
Brett Cannon | e974689 | 2008-04-12 23:44:07 +0000 | [diff] [blame] | 169 | |
| 170 | self.module.filterwarnings("ignore", "", Warning, "", 0) |
| 171 | text = 'filtered out' |
| 172 | self.module.warn(text) |
Brett Cannon | 672237d | 2008-09-09 00:49:16 +0000 | [diff] [blame] | 173 | self.assertNotEqual(str(w[-1].message), text) |
Brett Cannon | e974689 | 2008-04-12 23:44:07 +0000 | [diff] [blame] | 174 | |
| 175 | self.module.resetwarnings() |
| 176 | self.module.filterwarnings("error", "hex*", Warning, "", 0) |
| 177 | self.assertRaises(UserWarning, self.module.warn, 'hex/oct') |
| 178 | text = 'nonmatching text' |
| 179 | self.module.warn(text) |
Brett Cannon | 672237d | 2008-09-09 00:49:16 +0000 | [diff] [blame] | 180 | self.assertEqual(str(w[-1].message), text) |
| 181 | self.assert_(w[-1].category is UserWarning) |
Brett Cannon | e974689 | 2008-04-12 23:44:07 +0000 | [diff] [blame] | 182 | |
Brett Cannon | 667bb4f | 2008-04-13 02:42:36 +0000 | [diff] [blame] | 183 | class CFilterTests(BaseTest, FilterTests): |
Brett Cannon | e974689 | 2008-04-12 23:44:07 +0000 | [diff] [blame] | 184 | module = c_warnings |
| 185 | |
Brett Cannon | 667bb4f | 2008-04-13 02:42:36 +0000 | [diff] [blame] | 186 | class PyFilterTests(BaseTest, FilterTests): |
Brett Cannon | e974689 | 2008-04-12 23:44:07 +0000 | [diff] [blame] | 187 | module = py_warnings |
| 188 | |
| 189 | |
| 190 | class WarnTests(unittest.TestCase): |
| 191 | |
| 192 | """Test warnings.warn() and warnings.warn_explicit().""" |
| 193 | |
| 194 | def test_message(self): |
Brett Cannon | 672237d | 2008-09-09 00:49:16 +0000 | [diff] [blame] | 195 | with original_warnings.catch_warnings(record=True, |
| 196 | module=self.module) as w: |
Walter Dörwald | e6dae6c | 2007-04-03 18:33:29 +0000 | [diff] [blame] | 197 | for i in range(4): |
Brett Cannon | e974689 | 2008-04-12 23:44:07 +0000 | [diff] [blame] | 198 | text = 'multi %d' %i # Different text on each call. |
| 199 | self.module.warn(text) |
Brett Cannon | 672237d | 2008-09-09 00:49:16 +0000 | [diff] [blame] | 200 | self.assertEqual(str(w[-1].message), text) |
| 201 | self.assert_(w[-1].category is UserWarning) |
Raymond Hettinger | d6f6e50 | 2003-07-13 08:37:40 +0000 | [diff] [blame] | 202 | |
Walter Dörwald | e1a9b42 | 2007-04-03 16:53:43 +0000 | [diff] [blame] | 203 | def test_filename(self): |
Brett Cannon | e974689 | 2008-04-12 23:44:07 +0000 | [diff] [blame] | 204 | with warnings_state(self.module): |
Brett Cannon | 672237d | 2008-09-09 00:49:16 +0000 | [diff] [blame] | 205 | with original_warnings.catch_warnings(record=True, |
| 206 | module=self.module) as w: |
Brett Cannon | e974689 | 2008-04-12 23:44:07 +0000 | [diff] [blame] | 207 | warning_tests.inner("spam1") |
Brett Cannon | 672237d | 2008-09-09 00:49:16 +0000 | [diff] [blame] | 208 | self.assertEqual(os.path.basename(w[-1].filename), |
| 209 | "warning_tests.py") |
Brett Cannon | e974689 | 2008-04-12 23:44:07 +0000 | [diff] [blame] | 210 | warning_tests.outer("spam2") |
Brett Cannon | 672237d | 2008-09-09 00:49:16 +0000 | [diff] [blame] | 211 | self.assertEqual(os.path.basename(w[-1].filename), |
| 212 | "warning_tests.py") |
Walter Dörwald | e1a9b42 | 2007-04-03 16:53:43 +0000 | [diff] [blame] | 213 | |
| 214 | def test_stacklevel(self): |
| 215 | # Test stacklevel argument |
| 216 | # make sure all messages are different, so the warning won't be skipped |
Brett Cannon | e974689 | 2008-04-12 23:44:07 +0000 | [diff] [blame] | 217 | with warnings_state(self.module): |
Brett Cannon | 672237d | 2008-09-09 00:49:16 +0000 | [diff] [blame] | 218 | with original_warnings.catch_warnings(record=True, |
| 219 | module=self.module) as w: |
Brett Cannon | e974689 | 2008-04-12 23:44:07 +0000 | [diff] [blame] | 220 | warning_tests.inner("spam3", stacklevel=1) |
Brett Cannon | 672237d | 2008-09-09 00:49:16 +0000 | [diff] [blame] | 221 | self.assertEqual(os.path.basename(w[-1].filename), |
| 222 | "warning_tests.py") |
Brett Cannon | e974689 | 2008-04-12 23:44:07 +0000 | [diff] [blame] | 223 | warning_tests.outer("spam4", stacklevel=1) |
Brett Cannon | 672237d | 2008-09-09 00:49:16 +0000 | [diff] [blame] | 224 | self.assertEqual(os.path.basename(w[-1].filename), |
| 225 | "warning_tests.py") |
Walter Dörwald | e1a9b42 | 2007-04-03 16:53:43 +0000 | [diff] [blame] | 226 | |
Brett Cannon | e974689 | 2008-04-12 23:44:07 +0000 | [diff] [blame] | 227 | warning_tests.inner("spam5", stacklevel=2) |
Brett Cannon | 672237d | 2008-09-09 00:49:16 +0000 | [diff] [blame] | 228 | self.assertEqual(os.path.basename(w[-1].filename), |
| 229 | "test_warnings.py") |
Brett Cannon | e974689 | 2008-04-12 23:44:07 +0000 | [diff] [blame] | 230 | warning_tests.outer("spam6", stacklevel=2) |
Brett Cannon | 672237d | 2008-09-09 00:49:16 +0000 | [diff] [blame] | 231 | self.assertEqual(os.path.basename(w[-1].filename), |
| 232 | "warning_tests.py") |
Brett Cannon | e3dcb01 | 2008-05-06 04:37:31 +0000 | [diff] [blame] | 233 | warning_tests.outer("spam6.5", stacklevel=3) |
Brett Cannon | 672237d | 2008-09-09 00:49:16 +0000 | [diff] [blame] | 234 | self.assertEqual(os.path.basename(w[-1].filename), |
| 235 | "test_warnings.py") |
Walter Dörwald | e1a9b42 | 2007-04-03 16:53:43 +0000 | [diff] [blame] | 236 | |
Brett Cannon | e974689 | 2008-04-12 23:44:07 +0000 | [diff] [blame] | 237 | warning_tests.inner("spam7", stacklevel=9999) |
Brett Cannon | 672237d | 2008-09-09 00:49:16 +0000 | [diff] [blame] | 238 | self.assertEqual(os.path.basename(w[-1].filename), |
| 239 | "sys") |
Brett Cannon | e974689 | 2008-04-12 23:44:07 +0000 | [diff] [blame] | 240 | |
Brett Cannon | 64a4bbe | 2008-05-03 03:19:39 +0000 | [diff] [blame] | 241 | def test_missing_filename_not_main(self): |
| 242 | # If __file__ is not specified and __main__ is not the module name, |
| 243 | # then __file__ should be set to the module name. |
| 244 | filename = warning_tests.__file__ |
| 245 | try: |
| 246 | del warning_tests.__file__ |
| 247 | with warnings_state(self.module): |
Brett Cannon | 672237d | 2008-09-09 00:49:16 +0000 | [diff] [blame] | 248 | with original_warnings.catch_warnings(record=True, |
| 249 | module=self.module) as w: |
Brett Cannon | 64a4bbe | 2008-05-03 03:19:39 +0000 | [diff] [blame] | 250 | warning_tests.inner("spam8", stacklevel=1) |
Brett Cannon | 672237d | 2008-09-09 00:49:16 +0000 | [diff] [blame] | 251 | self.assertEqual(w[-1].filename, warning_tests.__name__) |
Brett Cannon | 64a4bbe | 2008-05-03 03:19:39 +0000 | [diff] [blame] | 252 | finally: |
| 253 | warning_tests.__file__ = filename |
| 254 | |
| 255 | def test_missing_filename_main_with_argv(self): |
| 256 | # If __file__ is not specified and the caller is __main__ and sys.argv |
| 257 | # exists, then use sys.argv[0] as the file. |
| 258 | if not hasattr(sys, 'argv'): |
| 259 | return |
| 260 | filename = warning_tests.__file__ |
| 261 | module_name = warning_tests.__name__ |
| 262 | try: |
| 263 | del warning_tests.__file__ |
| 264 | warning_tests.__name__ = '__main__' |
| 265 | with warnings_state(self.module): |
Brett Cannon | 672237d | 2008-09-09 00:49:16 +0000 | [diff] [blame] | 266 | with original_warnings.catch_warnings(record=True, |
| 267 | module=self.module) as w: |
Brett Cannon | 64a4bbe | 2008-05-03 03:19:39 +0000 | [diff] [blame] | 268 | warning_tests.inner('spam9', stacklevel=1) |
Brett Cannon | 672237d | 2008-09-09 00:49:16 +0000 | [diff] [blame] | 269 | self.assertEqual(w[-1].filename, sys.argv[0]) |
Brett Cannon | 64a4bbe | 2008-05-03 03:19:39 +0000 | [diff] [blame] | 270 | finally: |
| 271 | warning_tests.__file__ = filename |
| 272 | warning_tests.__name__ = module_name |
| 273 | |
| 274 | def test_missing_filename_main_without_argv(self): |
| 275 | # If __file__ is not specified, the caller is __main__, and sys.argv |
| 276 | # is not set, then '__main__' is the file name. |
| 277 | filename = warning_tests.__file__ |
| 278 | module_name = warning_tests.__name__ |
| 279 | argv = sys.argv |
| 280 | try: |
| 281 | del warning_tests.__file__ |
| 282 | warning_tests.__name__ = '__main__' |
| 283 | del sys.argv |
| 284 | with warnings_state(self.module): |
Brett Cannon | 672237d | 2008-09-09 00:49:16 +0000 | [diff] [blame] | 285 | with original_warnings.catch_warnings(record=True, |
| 286 | module=self.module) as w: |
Brett Cannon | 64a4bbe | 2008-05-03 03:19:39 +0000 | [diff] [blame] | 287 | warning_tests.inner('spam10', stacklevel=1) |
Brett Cannon | 672237d | 2008-09-09 00:49:16 +0000 | [diff] [blame] | 288 | self.assertEqual(w[-1].filename, '__main__') |
Brett Cannon | 64a4bbe | 2008-05-03 03:19:39 +0000 | [diff] [blame] | 289 | finally: |
| 290 | warning_tests.__file__ = filename |
| 291 | warning_tests.__name__ = module_name |
| 292 | sys.argv = argv |
| 293 | |
| 294 | def test_missing_filename_main_with_argv_empty_string(self): |
| 295 | # If __file__ is not specified, the caller is __main__, and sys.argv[0] |
| 296 | # is the empty string, then '__main__ is the file name. |
| 297 | # Tests issue 2743. |
| 298 | file_name = warning_tests.__file__ |
| 299 | module_name = warning_tests.__name__ |
| 300 | argv = sys.argv |
| 301 | try: |
| 302 | del warning_tests.__file__ |
| 303 | warning_tests.__name__ = '__main__' |
| 304 | sys.argv = [''] |
| 305 | with warnings_state(self.module): |
Brett Cannon | 672237d | 2008-09-09 00:49:16 +0000 | [diff] [blame] | 306 | with original_warnings.catch_warnings(record=True, |
| 307 | module=self.module) as w: |
Brett Cannon | 64a4bbe | 2008-05-03 03:19:39 +0000 | [diff] [blame] | 308 | warning_tests.inner('spam11', stacklevel=1) |
Brett Cannon | 672237d | 2008-09-09 00:49:16 +0000 | [diff] [blame] | 309 | self.assertEqual(w[-1].filename, '__main__') |
Brett Cannon | 64a4bbe | 2008-05-03 03:19:39 +0000 | [diff] [blame] | 310 | finally: |
| 311 | warning_tests.__file__ = file_name |
| 312 | warning_tests.__name__ = module_name |
| 313 | sys.argv = argv |
| 314 | |
Brett Cannon | dea1b56 | 2008-06-27 00:31:13 +0000 | [diff] [blame] | 315 | def test_warn_explicit_type_errors(self): |
| 316 | # warn_explicit() shoud error out gracefully if it is given objects |
| 317 | # of the wrong types. |
| 318 | # lineno is expected to be an integer. |
| 319 | self.assertRaises(TypeError, self.module.warn_explicit, |
| 320 | None, UserWarning, None, None) |
| 321 | # Either 'message' needs to be an instance of Warning or 'category' |
| 322 | # needs to be a subclass. |
| 323 | self.assertRaises(TypeError, self.module.warn_explicit, |
| 324 | None, None, None, 1) |
| 325 | # 'registry' must be a dict or None. |
| 326 | self.assertRaises((TypeError, AttributeError), |
| 327 | self.module.warn_explicit, |
| 328 | None, Warning, None, 1, registry=42) |
| 329 | |
Brett Cannon | 64a4bbe | 2008-05-03 03:19:39 +0000 | [diff] [blame] | 330 | |
Brett Cannon | 667bb4f | 2008-04-13 02:42:36 +0000 | [diff] [blame] | 331 | class CWarnTests(BaseTest, WarnTests): |
Brett Cannon | e974689 | 2008-04-12 23:44:07 +0000 | [diff] [blame] | 332 | module = c_warnings |
| 333 | |
Nick Coghlan | cd2e704 | 2009-04-11 13:31:31 +0000 | [diff] [blame] | 334 | # As an early adopter, we sanity check the |
| 335 | # test_support.import_fresh_module utility function |
| 336 | def test_accelerated(self): |
| 337 | self.assertFalse(original_warnings is self.module) |
| 338 | self.assertFalse(hasattr(self.module.warn, 'func_code')) |
| 339 | |
Brett Cannon | 667bb4f | 2008-04-13 02:42:36 +0000 | [diff] [blame] | 340 | class PyWarnTests(BaseTest, WarnTests): |
Brett Cannon | e974689 | 2008-04-12 23:44:07 +0000 | [diff] [blame] | 341 | module = py_warnings |
| 342 | |
Nick Coghlan | cd2e704 | 2009-04-11 13:31:31 +0000 | [diff] [blame] | 343 | # As an early adopter, we sanity check the |
| 344 | # test_support.import_fresh_module utility function |
| 345 | def test_pure_python(self): |
| 346 | self.assertFalse(original_warnings is self.module) |
| 347 | self.assertTrue(hasattr(self.module.warn, 'func_code')) |
| 348 | |
Brett Cannon | e974689 | 2008-04-12 23:44:07 +0000 | [diff] [blame] | 349 | |
| 350 | class WCmdLineTests(unittest.TestCase): |
| 351 | |
| 352 | def test_improper_input(self): |
| 353 | # Uses the private _setoption() function to test the parsing |
| 354 | # of command-line warning arguments |
Brett Cannon | 672237d | 2008-09-09 00:49:16 +0000 | [diff] [blame] | 355 | with original_warnings.catch_warnings(module=self.module): |
Brett Cannon | e974689 | 2008-04-12 23:44:07 +0000 | [diff] [blame] | 356 | self.assertRaises(self.module._OptionError, |
| 357 | self.module._setoption, '1:2:3:4:5:6') |
| 358 | self.assertRaises(self.module._OptionError, |
| 359 | self.module._setoption, 'bogus::Warning') |
| 360 | self.assertRaises(self.module._OptionError, |
| 361 | self.module._setoption, 'ignore:2::4:-5') |
| 362 | self.module._setoption('error::Warning::0') |
| 363 | self.assertRaises(UserWarning, self.module.warn, 'convert to error') |
| 364 | |
Brett Cannon | 667bb4f | 2008-04-13 02:42:36 +0000 | [diff] [blame] | 365 | class CWCmdLineTests(BaseTest, WCmdLineTests): |
Brett Cannon | e974689 | 2008-04-12 23:44:07 +0000 | [diff] [blame] | 366 | module = c_warnings |
| 367 | |
Brett Cannon | 667bb4f | 2008-04-13 02:42:36 +0000 | [diff] [blame] | 368 | class PyWCmdLineTests(BaseTest, WCmdLineTests): |
Brett Cannon | e974689 | 2008-04-12 23:44:07 +0000 | [diff] [blame] | 369 | module = py_warnings |
| 370 | |
| 371 | |
Brett Cannon | 667bb4f | 2008-04-13 02:42:36 +0000 | [diff] [blame] | 372 | class _WarningsTests(BaseTest): |
Brett Cannon | e974689 | 2008-04-12 23:44:07 +0000 | [diff] [blame] | 373 | |
| 374 | """Tests specific to the _warnings module.""" |
| 375 | |
| 376 | module = c_warnings |
| 377 | |
| 378 | def test_filter(self): |
| 379 | # Everything should function even if 'filters' is not in warnings. |
Brett Cannon | 672237d | 2008-09-09 00:49:16 +0000 | [diff] [blame] | 380 | with original_warnings.catch_warnings(module=self.module) as w: |
Brett Cannon | e974689 | 2008-04-12 23:44:07 +0000 | [diff] [blame] | 381 | self.module.filterwarnings("error", "", Warning, "", 0) |
| 382 | self.assertRaises(UserWarning, self.module.warn, |
| 383 | 'convert to error') |
| 384 | del self.module.filters |
| 385 | self.assertRaises(UserWarning, self.module.warn, |
| 386 | 'convert to error') |
| 387 | |
| 388 | def test_onceregistry(self): |
| 389 | # Replacing or removing the onceregistry should be okay. |
| 390 | global __warningregistry__ |
| 391 | message = UserWarning('onceregistry test') |
| 392 | try: |
| 393 | original_registry = self.module.onceregistry |
| 394 | __warningregistry__ = {} |
Brett Cannon | 672237d | 2008-09-09 00:49:16 +0000 | [diff] [blame] | 395 | with original_warnings.catch_warnings(record=True, |
| 396 | module=self.module) as w: |
Brett Cannon | e974689 | 2008-04-12 23:44:07 +0000 | [diff] [blame] | 397 | self.module.resetwarnings() |
| 398 | self.module.filterwarnings("once", category=UserWarning) |
| 399 | self.module.warn_explicit(message, UserWarning, "file", 42) |
Brett Cannon | 672237d | 2008-09-09 00:49:16 +0000 | [diff] [blame] | 400 | self.failUnlessEqual(w[-1].message, message) |
| 401 | del w[:] |
Brett Cannon | e974689 | 2008-04-12 23:44:07 +0000 | [diff] [blame] | 402 | self.module.warn_explicit(message, UserWarning, "file", 42) |
Brett Cannon | 1eaf074 | 2008-09-02 01:25:16 +0000 | [diff] [blame] | 403 | self.assertEquals(len(w), 0) |
Brett Cannon | e974689 | 2008-04-12 23:44:07 +0000 | [diff] [blame] | 404 | # Test the resetting of onceregistry. |
| 405 | self.module.onceregistry = {} |
| 406 | __warningregistry__ = {} |
| 407 | self.module.warn('onceregistry test') |
Brett Cannon | 672237d | 2008-09-09 00:49:16 +0000 | [diff] [blame] | 408 | self.failUnlessEqual(w[-1].message.args, message.args) |
Brett Cannon | e974689 | 2008-04-12 23:44:07 +0000 | [diff] [blame] | 409 | # Removal of onceregistry is okay. |
Brett Cannon | 672237d | 2008-09-09 00:49:16 +0000 | [diff] [blame] | 410 | del w[:] |
Brett Cannon | e974689 | 2008-04-12 23:44:07 +0000 | [diff] [blame] | 411 | del self.module.onceregistry |
| 412 | __warningregistry__ = {} |
| 413 | self.module.warn_explicit(message, UserWarning, "file", 42) |
Brett Cannon | 1eaf074 | 2008-09-02 01:25:16 +0000 | [diff] [blame] | 414 | self.assertEquals(len(w), 0) |
Brett Cannon | e974689 | 2008-04-12 23:44:07 +0000 | [diff] [blame] | 415 | finally: |
| 416 | self.module.onceregistry = original_registry |
| 417 | |
Brett Cannon | 15ba4da | 2009-04-01 18:03:59 +0000 | [diff] [blame] | 418 | def test_default_action(self): |
| 419 | # Replacing or removing defaultaction should be okay. |
| 420 | message = UserWarning("defaultaction test") |
| 421 | original = self.module.defaultaction |
| 422 | try: |
| 423 | with original_warnings.catch_warnings(record=True, |
| 424 | module=self.module) as w: |
| 425 | self.module.resetwarnings() |
| 426 | registry = {} |
| 427 | self.module.warn_explicit(message, UserWarning, "<test>", 42, |
| 428 | registry=registry) |
| 429 | self.assertEqual(w[-1].message, message) |
| 430 | self.assertEqual(len(w), 1) |
| 431 | self.assertEqual(len(registry), 1) |
| 432 | del w[:] |
| 433 | # Test removal. |
| 434 | del self.module.defaultaction |
| 435 | __warningregistry__ = {} |
| 436 | registry = {} |
| 437 | self.module.warn_explicit(message, UserWarning, "<test>", 43, |
| 438 | registry=registry) |
| 439 | self.assertEqual(w[-1].message, message) |
| 440 | self.assertEqual(len(w), 1) |
| 441 | self.assertEqual(len(registry), 1) |
| 442 | del w[:] |
| 443 | # Test setting. |
| 444 | self.module.defaultaction = "ignore" |
| 445 | __warningregistry__ = {} |
| 446 | registry = {} |
| 447 | self.module.warn_explicit(message, UserWarning, "<test>", 44, |
| 448 | registry=registry) |
| 449 | self.assertEqual(len(w), 0) |
| 450 | finally: |
| 451 | self.module.defaultaction = original |
| 452 | |
Brett Cannon | e974689 | 2008-04-12 23:44:07 +0000 | [diff] [blame] | 453 | def test_showwarning_missing(self): |
| 454 | # Test that showwarning() missing is okay. |
| 455 | text = 'del showwarning test' |
Brett Cannon | 672237d | 2008-09-09 00:49:16 +0000 | [diff] [blame] | 456 | with original_warnings.catch_warnings(module=self.module): |
Brett Cannon | e974689 | 2008-04-12 23:44:07 +0000 | [diff] [blame] | 457 | self.module.filterwarnings("always", category=UserWarning) |
| 458 | del self.module.showwarning |
| 459 | with test_support.captured_output('stderr') as stream: |
| 460 | self.module.warn(text) |
| 461 | result = stream.getvalue() |
| 462 | self.failUnless(text in result) |
| 463 | |
Benjamin Peterson | d295032 | 2008-05-06 22:18:11 +0000 | [diff] [blame] | 464 | def test_showwarning_not_callable(self): |
Brett Cannon | ce3d221 | 2009-04-01 20:25:48 +0000 | [diff] [blame] | 465 | with original_warnings.catch_warnings(module=self.module): |
| 466 | self.module.filterwarnings("always", category=UserWarning) |
| 467 | old_showwarning = self.module.showwarning |
| 468 | self.module.showwarning = 23 |
| 469 | try: |
| 470 | self.assertRaises(TypeError, self.module.warn, "Warning!") |
| 471 | finally: |
| 472 | self.module.showwarning = old_showwarning |
Benjamin Peterson | d295032 | 2008-05-06 22:18:11 +0000 | [diff] [blame] | 473 | |
Brett Cannon | e974689 | 2008-04-12 23:44:07 +0000 | [diff] [blame] | 474 | def test_show_warning_output(self): |
| 475 | # With showarning() missing, make sure that output is okay. |
| 476 | text = 'test show_warning' |
Brett Cannon | 672237d | 2008-09-09 00:49:16 +0000 | [diff] [blame] | 477 | with original_warnings.catch_warnings(module=self.module): |
Brett Cannon | e974689 | 2008-04-12 23:44:07 +0000 | [diff] [blame] | 478 | self.module.filterwarnings("always", category=UserWarning) |
| 479 | del self.module.showwarning |
| 480 | with test_support.captured_output('stderr') as stream: |
| 481 | warning_tests.inner(text) |
| 482 | result = stream.getvalue() |
| 483 | self.failUnlessEqual(result.count('\n'), 2, |
| 484 | "Too many newlines in %r" % result) |
| 485 | first_line, second_line = result.split('\n', 1) |
| 486 | expected_file = os.path.splitext(warning_tests.__file__)[0] + '.py' |
Brett Cannon | c477427 | 2008-04-13 17:41:31 +0000 | [diff] [blame] | 487 | first_line_parts = first_line.rsplit(':', 3) |
Brett Cannon | 25bb818 | 2008-04-13 17:09:43 +0000 | [diff] [blame] | 488 | path, line, warning_class, message = first_line_parts |
Brett Cannon | e974689 | 2008-04-12 23:44:07 +0000 | [diff] [blame] | 489 | line = int(line) |
| 490 | self.failUnlessEqual(expected_file, path) |
| 491 | self.failUnlessEqual(warning_class, ' ' + UserWarning.__name__) |
| 492 | self.failUnlessEqual(message, ' ' + text) |
| 493 | expected_line = ' ' + linecache.getline(path, line).strip() + '\n' |
| 494 | assert expected_line |
| 495 | self.failUnlessEqual(second_line, expected_line) |
Walter Dörwald | e1a9b42 | 2007-04-03 16:53:43 +0000 | [diff] [blame] | 496 | |
Brett Cannon | 53ab5b7 | 2006-06-22 16:49:14 +0000 | [diff] [blame] | 497 | |
Brett Cannon | 905c31c | 2007-12-20 10:09:52 +0000 | [diff] [blame] | 498 | class WarningsDisplayTests(unittest.TestCase): |
| 499 | |
Brett Cannon | e974689 | 2008-04-12 23:44:07 +0000 | [diff] [blame] | 500 | """Test the displaying of warnings and the ability to overload functions |
| 501 | related to displaying warnings.""" |
| 502 | |
Brett Cannon | 905c31c | 2007-12-20 10:09:52 +0000 | [diff] [blame] | 503 | def test_formatwarning(self): |
| 504 | message = "msg" |
| 505 | category = Warning |
| 506 | file_name = os.path.splitext(warning_tests.__file__)[0] + '.py' |
| 507 | line_num = 3 |
| 508 | file_line = linecache.getline(file_name, line_num).strip() |
Brett Cannon | e974689 | 2008-04-12 23:44:07 +0000 | [diff] [blame] | 509 | format = "%s:%s: %s: %s\n %s\n" |
| 510 | expect = format % (file_name, line_num, category.__name__, message, |
| 511 | file_line) |
| 512 | self.failUnlessEqual(expect, self.module.formatwarning(message, |
| 513 | category, file_name, line_num)) |
| 514 | # Test the 'line' argument. |
| 515 | file_line += " for the win!" |
| 516 | expect = format % (file_name, line_num, category.__name__, message, |
| 517 | file_line) |
| 518 | self.failUnlessEqual(expect, self.module.formatwarning(message, |
| 519 | category, file_name, line_num, file_line)) |
Brett Cannon | 905c31c | 2007-12-20 10:09:52 +0000 | [diff] [blame] | 520 | |
| 521 | def test_showwarning(self): |
| 522 | file_name = os.path.splitext(warning_tests.__file__)[0] + '.py' |
| 523 | line_num = 3 |
| 524 | expected_file_line = linecache.getline(file_name, line_num).strip() |
| 525 | message = 'msg' |
| 526 | category = Warning |
| 527 | file_object = StringIO.StringIO() |
Brett Cannon | e974689 | 2008-04-12 23:44:07 +0000 | [diff] [blame] | 528 | expect = self.module.formatwarning(message, category, file_name, |
| 529 | line_num) |
| 530 | self.module.showwarning(message, category, file_name, line_num, |
Brett Cannon | 905c31c | 2007-12-20 10:09:52 +0000 | [diff] [blame] | 531 | file_object) |
| 532 | self.failUnlessEqual(file_object.getvalue(), expect) |
Brett Cannon | e974689 | 2008-04-12 23:44:07 +0000 | [diff] [blame] | 533 | # Test 'line' argument. |
| 534 | expected_file_line += "for the win!" |
| 535 | expect = self.module.formatwarning(message, category, file_name, |
| 536 | line_num, expected_file_line) |
| 537 | file_object = StringIO.StringIO() |
| 538 | self.module.showwarning(message, category, file_name, line_num, |
| 539 | file_object, expected_file_line) |
| 540 | self.failUnlessEqual(expect, file_object.getvalue()) |
| 541 | |
Brett Cannon | 667bb4f | 2008-04-13 02:42:36 +0000 | [diff] [blame] | 542 | class CWarningsDisplayTests(BaseTest, WarningsDisplayTests): |
Brett Cannon | e974689 | 2008-04-12 23:44:07 +0000 | [diff] [blame] | 543 | module = c_warnings |
| 544 | |
Brett Cannon | 667bb4f | 2008-04-13 02:42:36 +0000 | [diff] [blame] | 545 | class PyWarningsDisplayTests(BaseTest, WarningsDisplayTests): |
Brett Cannon | e974689 | 2008-04-12 23:44:07 +0000 | [diff] [blame] | 546 | module = py_warnings |
Brett Cannon | 905c31c | 2007-12-20 10:09:52 +0000 | [diff] [blame] | 547 | |
| 548 | |
Brett Cannon | 1eaf074 | 2008-09-02 01:25:16 +0000 | [diff] [blame] | 549 | class CatchWarningTests(BaseTest): |
Nick Coghlan | 38469e2 | 2008-07-13 12:23:47 +0000 | [diff] [blame] | 550 | |
Brett Cannon | 1eaf074 | 2008-09-02 01:25:16 +0000 | [diff] [blame] | 551 | """Test catch_warnings().""" |
| 552 | |
| 553 | def test_catch_warnings_restore(self): |
Nick Coghlan | 38469e2 | 2008-07-13 12:23:47 +0000 | [diff] [blame] | 554 | wmod = self.module |
| 555 | orig_filters = wmod.filters |
| 556 | orig_showwarning = wmod.showwarning |
Nick Coghlan | d2e0938 | 2008-09-11 12:11:06 +0000 | [diff] [blame] | 557 | # Ensure both showwarning and filters are restored when recording |
| 558 | with wmod.catch_warnings(module=wmod, record=True): |
Nick Coghlan | 38469e2 | 2008-07-13 12:23:47 +0000 | [diff] [blame] | 559 | wmod.filters = wmod.showwarning = object() |
| 560 | self.assert_(wmod.filters is orig_filters) |
| 561 | self.assert_(wmod.showwarning is orig_showwarning) |
Nick Coghlan | d2e0938 | 2008-09-11 12:11:06 +0000 | [diff] [blame] | 562 | # Same test, but with recording disabled |
Brett Cannon | 1eaf074 | 2008-09-02 01:25:16 +0000 | [diff] [blame] | 563 | with wmod.catch_warnings(module=wmod, record=False): |
Nick Coghlan | 38469e2 | 2008-07-13 12:23:47 +0000 | [diff] [blame] | 564 | wmod.filters = wmod.showwarning = object() |
| 565 | self.assert_(wmod.filters is orig_filters) |
| 566 | self.assert_(wmod.showwarning is orig_showwarning) |
| 567 | |
Brett Cannon | 1eaf074 | 2008-09-02 01:25:16 +0000 | [diff] [blame] | 568 | def test_catch_warnings_recording(self): |
Nick Coghlan | 38469e2 | 2008-07-13 12:23:47 +0000 | [diff] [blame] | 569 | wmod = self.module |
Nick Coghlan | d2e0938 | 2008-09-11 12:11:06 +0000 | [diff] [blame] | 570 | # Ensure warnings are recorded when requested |
Brett Cannon | 1eaf074 | 2008-09-02 01:25:16 +0000 | [diff] [blame] | 571 | with wmod.catch_warnings(module=wmod, record=True) as w: |
| 572 | self.assertEqual(w, []) |
Nick Coghlan | d2e0938 | 2008-09-11 12:11:06 +0000 | [diff] [blame] | 573 | self.assert_(type(w) is list) |
Nick Coghlan | 38469e2 | 2008-07-13 12:23:47 +0000 | [diff] [blame] | 574 | wmod.simplefilter("always") |
| 575 | wmod.warn("foo") |
Brett Cannon | 672237d | 2008-09-09 00:49:16 +0000 | [diff] [blame] | 576 | self.assertEqual(str(w[-1].message), "foo") |
Nick Coghlan | 38469e2 | 2008-07-13 12:23:47 +0000 | [diff] [blame] | 577 | wmod.warn("bar") |
Brett Cannon | 672237d | 2008-09-09 00:49:16 +0000 | [diff] [blame] | 578 | self.assertEqual(str(w[-1].message), "bar") |
Brett Cannon | 1eaf074 | 2008-09-02 01:25:16 +0000 | [diff] [blame] | 579 | self.assertEqual(str(w[0].message), "foo") |
| 580 | self.assertEqual(str(w[1].message), "bar") |
Brett Cannon | 672237d | 2008-09-09 00:49:16 +0000 | [diff] [blame] | 581 | del w[:] |
Brett Cannon | 1eaf074 | 2008-09-02 01:25:16 +0000 | [diff] [blame] | 582 | self.assertEqual(w, []) |
Nick Coghlan | d2e0938 | 2008-09-11 12:11:06 +0000 | [diff] [blame] | 583 | # Ensure warnings are not recorded when not requested |
Nick Coghlan | 38469e2 | 2008-07-13 12:23:47 +0000 | [diff] [blame] | 584 | orig_showwarning = wmod.showwarning |
Brett Cannon | 1eaf074 | 2008-09-02 01:25:16 +0000 | [diff] [blame] | 585 | with wmod.catch_warnings(module=wmod, record=False) as w: |
Nick Coghlan | 38469e2 | 2008-07-13 12:23:47 +0000 | [diff] [blame] | 586 | self.assert_(w is None) |
| 587 | self.assert_(wmod.showwarning is orig_showwarning) |
| 588 | |
Nick Coghlan | d2e0938 | 2008-09-11 12:11:06 +0000 | [diff] [blame] | 589 | def test_catch_warnings_reentry_guard(self): |
| 590 | wmod = self.module |
| 591 | # Ensure catch_warnings is protected against incorrect usage |
| 592 | x = wmod.catch_warnings(module=wmod, record=True) |
| 593 | self.assertRaises(RuntimeError, x.__exit__) |
| 594 | with x: |
| 595 | self.assertRaises(RuntimeError, x.__enter__) |
| 596 | # Same test, but with recording disabled |
| 597 | x = wmod.catch_warnings(module=wmod, record=False) |
| 598 | self.assertRaises(RuntimeError, x.__exit__) |
| 599 | with x: |
| 600 | self.assertRaises(RuntimeError, x.__enter__) |
| 601 | |
| 602 | def test_catch_warnings_defaults(self): |
| 603 | wmod = self.module |
| 604 | orig_filters = wmod.filters |
| 605 | orig_showwarning = wmod.showwarning |
| 606 | # Ensure default behaviour is not to record warnings |
| 607 | with wmod.catch_warnings(module=wmod) as w: |
| 608 | self.assert_(w is None) |
| 609 | self.assert_(wmod.showwarning is orig_showwarning) |
| 610 | self.assert_(wmod.filters is not orig_filters) |
| 611 | self.assert_(wmod.filters is orig_filters) |
| 612 | if wmod is sys.modules['warnings']: |
| 613 | # Ensure the default module is this one |
| 614 | with wmod.catch_warnings() as w: |
| 615 | self.assert_(w is None) |
| 616 | self.assert_(wmod.showwarning is orig_showwarning) |
| 617 | self.assert_(wmod.filters is not orig_filters) |
| 618 | self.assert_(wmod.filters is orig_filters) |
| 619 | |
| 620 | def test_check_warnings(self): |
| 621 | # Explicit tests for the test_support convenience wrapper |
| 622 | wmod = self.module |
| 623 | if wmod is sys.modules['warnings']: |
| 624 | with test_support.check_warnings() as w: |
| 625 | self.assertEqual(w.warnings, []) |
| 626 | wmod.simplefilter("always") |
| 627 | wmod.warn("foo") |
| 628 | self.assertEqual(str(w.message), "foo") |
| 629 | wmod.warn("bar") |
| 630 | self.assertEqual(str(w.message), "bar") |
| 631 | self.assertEqual(str(w.warnings[0].message), "foo") |
| 632 | self.assertEqual(str(w.warnings[1].message), "bar") |
| 633 | w.reset() |
| 634 | self.assertEqual(w.warnings, []) |
| 635 | |
| 636 | |
| 637 | |
Brett Cannon | 1eaf074 | 2008-09-02 01:25:16 +0000 | [diff] [blame] | 638 | class CCatchWarningTests(CatchWarningTests): |
Nick Coghlan | 38469e2 | 2008-07-13 12:23:47 +0000 | [diff] [blame] | 639 | module = c_warnings |
| 640 | |
Brett Cannon | 1eaf074 | 2008-09-02 01:25:16 +0000 | [diff] [blame] | 641 | class PyCatchWarningTests(CatchWarningTests): |
Nick Coghlan | 38469e2 | 2008-07-13 12:23:47 +0000 | [diff] [blame] | 642 | module = py_warnings |
| 643 | |
| 644 | |
Brett Cannon | e974689 | 2008-04-12 23:44:07 +0000 | [diff] [blame] | 645 | def test_main(): |
Amaury Forgeot d'Arc | 607bff1 | 2008-04-18 23:31:33 +0000 | [diff] [blame] | 646 | py_warnings.onceregistry.clear() |
| 647 | c_warnings.onceregistry.clear() |
Brett Cannon | 1eaf074 | 2008-09-02 01:25:16 +0000 | [diff] [blame] | 648 | test_support.run_unittest(CFilterTests, PyFilterTests, |
| 649 | CWarnTests, PyWarnTests, |
Brett Cannon | e974689 | 2008-04-12 23:44:07 +0000 | [diff] [blame] | 650 | CWCmdLineTests, PyWCmdLineTests, |
| 651 | _WarningsTests, |
| 652 | CWarningsDisplayTests, PyWarningsDisplayTests, |
Brett Cannon | 1eaf074 | 2008-09-02 01:25:16 +0000 | [diff] [blame] | 653 | CCatchWarningTests, PyCatchWarningTests, |
Brett Cannon | e974689 | 2008-04-12 23:44:07 +0000 | [diff] [blame] | 654 | ) |
| 655 | |
| 656 | |
Raymond Hettinger | d6f6e50 | 2003-07-13 08:37:40 +0000 | [diff] [blame] | 657 | if __name__ == "__main__": |
Brett Cannon | e974689 | 2008-04-12 23:44:07 +0000 | [diff] [blame] | 658 | test_main() |