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 | e974689 | 2008-04-12 23:44:07 +0000 | [diff] [blame] | 11 | sys.modules['_warnings'] = 0 |
| 12 | if 'warnings' in sys.modules: |
| 13 | del sys.modules['warnings'] |
Jeremy Hylton | 8501466 | 2003-07-11 15:37:59 +0000 | [diff] [blame] | 14 | |
Brett Cannon | e974689 | 2008-04-12 23:44:07 +0000 | [diff] [blame] | 15 | import warnings as py_warnings |
| 16 | |
| 17 | del sys.modules['_warnings'] |
| 18 | del sys.modules['warnings'] |
| 19 | |
| 20 | import warnings as c_warnings |
| 21 | |
| 22 | @contextmanager |
| 23 | def warnings_state(module): |
| 24 | """Use a specific warnings implementation in warning_tests.""" |
| 25 | global __warningregistry__ |
| 26 | for to_clear in (sys, warning_tests): |
| 27 | try: |
| 28 | to_clear.__warningregistry__.clear() |
| 29 | except AttributeError: |
| 30 | pass |
| 31 | try: |
| 32 | __warningregistry__.clear() |
| 33 | except NameError: |
| 34 | pass |
| 35 | original_warnings = warning_tests.warnings |
| 36 | try: |
| 37 | warning_tests.warnings = module |
| 38 | yield |
| 39 | finally: |
| 40 | warning_tests.warnings = original_warnings |
| 41 | |
| 42 | |
| 43 | class FilterTests(unittest.TestCase): |
| 44 | |
| 45 | """Testing the filtering functionality.""" |
| 46 | |
| 47 | def setUp(self): |
| 48 | global __warningregistry__ |
| 49 | try: |
| 50 | __warningregistry__.clear() |
| 51 | except NameError: |
| 52 | pass |
| 53 | |
| 54 | def test_error(self): |
| 55 | with test_support.catch_warning(self.module) as w: |
| 56 | self.module.resetwarnings() |
| 57 | self.module.filterwarnings("error", category=UserWarning) |
| 58 | self.assertRaises(UserWarning, self.module.warn, |
| 59 | "FilterTests.test_error") |
| 60 | |
| 61 | def test_ignore(self): |
| 62 | with test_support.catch_warning(self.module) as w: |
| 63 | self.module.resetwarnings() |
| 64 | self.module.filterwarnings("ignore", category=UserWarning) |
| 65 | self.module.warn("FilterTests.test_ignore", UserWarning) |
| 66 | self.assert_(not w.message) |
| 67 | |
| 68 | def test_always(self): |
| 69 | with test_support.catch_warning(self.module) as w: |
| 70 | self.module.resetwarnings() |
| 71 | self.module.filterwarnings("always", category=UserWarning) |
| 72 | message = "FilterTests.test_always" |
| 73 | self.module.warn(message, UserWarning) |
| 74 | self.assert_(message, w.message) |
| 75 | w.message = None # Reset. |
| 76 | self.module.warn(message, UserWarning) |
| 77 | self.assert_(w.message, message) |
| 78 | |
| 79 | def test_default(self): |
| 80 | with test_support.catch_warning(self.module) as w: |
| 81 | self.module.resetwarnings() |
| 82 | self.module.filterwarnings("default", category=UserWarning) |
| 83 | message = UserWarning("FilterTests.test_default") |
| 84 | for x in xrange(2): |
| 85 | self.module.warn(message, UserWarning) |
| 86 | if x == 0: |
| 87 | self.assertEquals(w.message, message) |
| 88 | w.reset() |
| 89 | elif x == 1: |
| 90 | self.assert_(not w.message, "unexpected warning: " + str(w)) |
| 91 | else: |
| 92 | raise ValueError("loop variant unhandled") |
| 93 | |
| 94 | def test_module(self): |
| 95 | with test_support.catch_warning(self.module) as w: |
| 96 | self.module.resetwarnings() |
| 97 | self.module.filterwarnings("module", category=UserWarning) |
| 98 | message = UserWarning("FilterTests.test_module") |
| 99 | self.module.warn(message, UserWarning) |
| 100 | self.assertEquals(w.message, message) |
| 101 | w.reset() |
| 102 | self.module.warn(message, UserWarning) |
| 103 | self.assert_(not w.message, "unexpected message: " + str(w)) |
| 104 | |
| 105 | def test_once(self): |
| 106 | with test_support.catch_warning(self.module) as w: |
| 107 | self.module.resetwarnings() |
| 108 | self.module.filterwarnings("once", category=UserWarning) |
| 109 | message = UserWarning("FilterTests.test_once") |
| 110 | self.module.warn_explicit(message, UserWarning, "test_warnings.py", |
| 111 | 42) |
| 112 | self.assertEquals(w.message, message) |
| 113 | w.reset() |
| 114 | self.module.warn_explicit(message, UserWarning, "test_warnings.py", |
| 115 | 13) |
| 116 | self.assert_(not w.message) |
| 117 | self.module.warn_explicit(message, UserWarning, "test_warnings2.py", |
| 118 | 42) |
| 119 | self.assert_(not w.message) |
| 120 | |
| 121 | def test_inheritance(self): |
| 122 | with test_support.catch_warning(self.module) as w: |
| 123 | self.module.resetwarnings() |
| 124 | self.module.filterwarnings("error", category=Warning) |
| 125 | self.assertRaises(UserWarning, self.module.warn, |
| 126 | "FilterTests.test_inheritance", UserWarning) |
| 127 | |
| 128 | def test_ordering(self): |
| 129 | with test_support.catch_warning(self.module) as w: |
| 130 | self.module.resetwarnings() |
| 131 | self.module.filterwarnings("ignore", category=UserWarning) |
| 132 | self.module.filterwarnings("error", category=UserWarning, |
| 133 | append=True) |
| 134 | w.reset() |
| 135 | try: |
| 136 | self.module.warn("FilterTests.test_ordering", UserWarning) |
| 137 | except UserWarning: |
| 138 | self.fail("order handling for actions failed") |
| 139 | self.assert_(not w.message) |
| 140 | |
| 141 | def test_filterwarnings(self): |
| 142 | # Test filterwarnings(). |
| 143 | # Implicitly also tests resetwarnings(). |
| 144 | with test_support.catch_warning(self.module) as w: |
| 145 | self.module.filterwarnings("error", "", Warning, "", 0) |
| 146 | self.assertRaises(UserWarning, self.module.warn, 'convert to error') |
| 147 | |
| 148 | self.module.resetwarnings() |
| 149 | text = 'handle normally' |
| 150 | self.module.warn(text) |
| 151 | self.assertEqual(str(w.message), text) |
| 152 | self.assert_(w.category is UserWarning) |
| 153 | |
| 154 | self.module.filterwarnings("ignore", "", Warning, "", 0) |
| 155 | text = 'filtered out' |
| 156 | self.module.warn(text) |
| 157 | self.assertNotEqual(str(w.message), text) |
| 158 | |
| 159 | self.module.resetwarnings() |
| 160 | self.module.filterwarnings("error", "hex*", Warning, "", 0) |
| 161 | self.assertRaises(UserWarning, self.module.warn, 'hex/oct') |
| 162 | text = 'nonmatching text' |
| 163 | self.module.warn(text) |
| 164 | self.assertEqual(str(w.message), text) |
| 165 | self.assert_(w.category is UserWarning) |
| 166 | |
| 167 | class CFilterTests(FilterTests): |
| 168 | module = c_warnings |
| 169 | |
| 170 | class PyFilterTests(FilterTests): |
| 171 | module = py_warnings |
| 172 | |
| 173 | |
| 174 | class WarnTests(unittest.TestCase): |
| 175 | |
| 176 | """Test warnings.warn() and warnings.warn_explicit().""" |
| 177 | |
| 178 | def test_message(self): |
| 179 | with test_support.catch_warning(self.module) as w: |
Walter Dörwald | e6dae6c | 2007-04-03 18:33:29 +0000 | [diff] [blame] | 180 | for i in range(4): |
Brett Cannon | e974689 | 2008-04-12 23:44:07 +0000 | [diff] [blame] | 181 | text = 'multi %d' %i # Different text on each call. |
| 182 | self.module.warn(text) |
Walter Dörwald | e6dae6c | 2007-04-03 18:33:29 +0000 | [diff] [blame] | 183 | self.assertEqual(str(w.message), text) |
| 184 | self.assert_(w.category is UserWarning) |
Raymond Hettinger | d6f6e50 | 2003-07-13 08:37:40 +0000 | [diff] [blame] | 185 | |
Walter Dörwald | e1a9b42 | 2007-04-03 16:53:43 +0000 | [diff] [blame] | 186 | def test_filename(self): |
Brett Cannon | e974689 | 2008-04-12 23:44:07 +0000 | [diff] [blame] | 187 | with warnings_state(self.module): |
| 188 | with test_support.catch_warning(self.module) as w: |
| 189 | warning_tests.inner("spam1") |
| 190 | self.assertEqual(os.path.basename(w.filename), "warning_tests.py") |
| 191 | warning_tests.outer("spam2") |
| 192 | self.assertEqual(os.path.basename(w.filename), "warning_tests.py") |
Walter Dörwald | e1a9b42 | 2007-04-03 16:53:43 +0000 | [diff] [blame] | 193 | |
| 194 | def test_stacklevel(self): |
| 195 | # Test stacklevel argument |
| 196 | # 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] | 197 | with warnings_state(self.module): |
| 198 | with test_support.catch_warning(self.module) as w: |
| 199 | warning_tests.inner("spam3", stacklevel=1) |
| 200 | self.assertEqual(os.path.basename(w.filename), "warning_tests.py") |
| 201 | warning_tests.outer("spam4", stacklevel=1) |
| 202 | self.assertEqual(os.path.basename(w.filename), "warning_tests.py") |
Walter Dörwald | e1a9b42 | 2007-04-03 16:53:43 +0000 | [diff] [blame] | 203 | |
Brett Cannon | e974689 | 2008-04-12 23:44:07 +0000 | [diff] [blame] | 204 | warning_tests.inner("spam5", stacklevel=2) |
| 205 | self.assertEqual(os.path.basename(w.filename), "test_warnings.py") |
| 206 | warning_tests.outer("spam6", stacklevel=2) |
| 207 | self.assertEqual(os.path.basename(w.filename), "warning_tests.py") |
Walter Dörwald | e1a9b42 | 2007-04-03 16:53:43 +0000 | [diff] [blame] | 208 | |
Brett Cannon | e974689 | 2008-04-12 23:44:07 +0000 | [diff] [blame] | 209 | warning_tests.inner("spam7", stacklevel=9999) |
| 210 | self.assertEqual(os.path.basename(w.filename), "sys") |
| 211 | |
| 212 | |
| 213 | class CWarnTests(WarnTests): |
| 214 | module = c_warnings |
| 215 | |
| 216 | class PyWarnTests(WarnTests): |
| 217 | module = py_warnings |
| 218 | |
| 219 | |
| 220 | class WCmdLineTests(unittest.TestCase): |
| 221 | |
| 222 | def test_improper_input(self): |
| 223 | # Uses the private _setoption() function to test the parsing |
| 224 | # of command-line warning arguments |
| 225 | with test_support.catch_warning(self.module): |
| 226 | self.assertRaises(self.module._OptionError, |
| 227 | self.module._setoption, '1:2:3:4:5:6') |
| 228 | self.assertRaises(self.module._OptionError, |
| 229 | self.module._setoption, 'bogus::Warning') |
| 230 | self.assertRaises(self.module._OptionError, |
| 231 | self.module._setoption, 'ignore:2::4:-5') |
| 232 | self.module._setoption('error::Warning::0') |
| 233 | self.assertRaises(UserWarning, self.module.warn, 'convert to error') |
| 234 | |
| 235 | class CWCmdLineTests(WCmdLineTests): |
| 236 | module = c_warnings |
| 237 | |
| 238 | class PyWCmdLineTests(WCmdLineTests): |
| 239 | module = py_warnings |
| 240 | |
| 241 | |
| 242 | class _WarningsTests(unittest.TestCase): |
| 243 | |
| 244 | """Tests specific to the _warnings module.""" |
| 245 | |
| 246 | module = c_warnings |
| 247 | |
| 248 | def test_filter(self): |
| 249 | # Everything should function even if 'filters' is not in warnings. |
| 250 | with test_support.catch_warning(self.module) as w: |
| 251 | self.module.filterwarnings("error", "", Warning, "", 0) |
| 252 | self.assertRaises(UserWarning, self.module.warn, |
| 253 | 'convert to error') |
| 254 | del self.module.filters |
| 255 | self.assertRaises(UserWarning, self.module.warn, |
| 256 | 'convert to error') |
| 257 | |
| 258 | def test_onceregistry(self): |
| 259 | # Replacing or removing the onceregistry should be okay. |
| 260 | global __warningregistry__ |
| 261 | message = UserWarning('onceregistry test') |
| 262 | try: |
| 263 | original_registry = self.module.onceregistry |
| 264 | __warningregistry__ = {} |
| 265 | with test_support.catch_warning(self.module) as w: |
| 266 | self.module.resetwarnings() |
| 267 | self.module.filterwarnings("once", category=UserWarning) |
| 268 | self.module.warn_explicit(message, UserWarning, "file", 42) |
| 269 | self.failUnlessEqual(w.message, message) |
| 270 | w.reset() |
| 271 | self.module.warn_explicit(message, UserWarning, "file", 42) |
| 272 | self.assert_(not w.message) |
| 273 | # Test the resetting of onceregistry. |
| 274 | self.module.onceregistry = {} |
| 275 | __warningregistry__ = {} |
| 276 | self.module.warn('onceregistry test') |
| 277 | self.failUnlessEqual(w.message.args, message.args) |
| 278 | # Removal of onceregistry is okay. |
| 279 | w.reset() |
| 280 | del self.module.onceregistry |
| 281 | __warningregistry__ = {} |
| 282 | self.module.warn_explicit(message, UserWarning, "file", 42) |
| 283 | self.failUnless(not w.message) |
| 284 | finally: |
| 285 | self.module.onceregistry = original_registry |
| 286 | |
| 287 | def test_showwarning_missing(self): |
| 288 | # Test that showwarning() missing is okay. |
| 289 | text = 'del showwarning test' |
| 290 | with test_support.catch_warning(self.module): |
| 291 | self.module.filterwarnings("always", category=UserWarning) |
| 292 | del self.module.showwarning |
| 293 | with test_support.captured_output('stderr') as stream: |
| 294 | self.module.warn(text) |
| 295 | result = stream.getvalue() |
| 296 | self.failUnless(text in result) |
| 297 | |
| 298 | def test_show_warning_output(self): |
| 299 | # With showarning() missing, make sure that output is okay. |
| 300 | text = 'test show_warning' |
| 301 | with test_support.catch_warning(self.module): |
| 302 | self.module.filterwarnings("always", category=UserWarning) |
| 303 | del self.module.showwarning |
| 304 | with test_support.captured_output('stderr') as stream: |
| 305 | warning_tests.inner(text) |
| 306 | result = stream.getvalue() |
| 307 | self.failUnlessEqual(result.count('\n'), 2, |
| 308 | "Too many newlines in %r" % result) |
| 309 | first_line, second_line = result.split('\n', 1) |
| 310 | expected_file = os.path.splitext(warning_tests.__file__)[0] + '.py' |
| 311 | path, line, warning_class, message = first_line.split(':') |
| 312 | line = int(line) |
| 313 | self.failUnlessEqual(expected_file, path) |
| 314 | self.failUnlessEqual(warning_class, ' ' + UserWarning.__name__) |
| 315 | self.failUnlessEqual(message, ' ' + text) |
| 316 | expected_line = ' ' + linecache.getline(path, line).strip() + '\n' |
| 317 | assert expected_line |
| 318 | self.failUnlessEqual(second_line, expected_line) |
Walter Dörwald | e1a9b42 | 2007-04-03 16:53:43 +0000 | [diff] [blame] | 319 | |
Brett Cannon | 53ab5b7 | 2006-06-22 16:49:14 +0000 | [diff] [blame] | 320 | |
Brett Cannon | 905c31c | 2007-12-20 10:09:52 +0000 | [diff] [blame] | 321 | class WarningsDisplayTests(unittest.TestCase): |
| 322 | |
Brett Cannon | e974689 | 2008-04-12 23:44:07 +0000 | [diff] [blame] | 323 | """Test the displaying of warnings and the ability to overload functions |
| 324 | related to displaying warnings.""" |
| 325 | |
Brett Cannon | 905c31c | 2007-12-20 10:09:52 +0000 | [diff] [blame] | 326 | def test_formatwarning(self): |
| 327 | message = "msg" |
| 328 | category = Warning |
| 329 | file_name = os.path.splitext(warning_tests.__file__)[0] + '.py' |
| 330 | line_num = 3 |
| 331 | file_line = linecache.getline(file_name, line_num).strip() |
Brett Cannon | e974689 | 2008-04-12 23:44:07 +0000 | [diff] [blame] | 332 | format = "%s:%s: %s: %s\n %s\n" |
| 333 | expect = format % (file_name, line_num, category.__name__, message, |
| 334 | file_line) |
| 335 | self.failUnlessEqual(expect, self.module.formatwarning(message, |
| 336 | category, file_name, line_num)) |
| 337 | # Test the 'line' argument. |
| 338 | file_line += " for the win!" |
| 339 | expect = format % (file_name, line_num, category.__name__, message, |
| 340 | file_line) |
| 341 | self.failUnlessEqual(expect, self.module.formatwarning(message, |
| 342 | category, file_name, line_num, file_line)) |
Brett Cannon | 905c31c | 2007-12-20 10:09:52 +0000 | [diff] [blame] | 343 | |
| 344 | def test_showwarning(self): |
| 345 | file_name = os.path.splitext(warning_tests.__file__)[0] + '.py' |
| 346 | line_num = 3 |
| 347 | expected_file_line = linecache.getline(file_name, line_num).strip() |
| 348 | message = 'msg' |
| 349 | category = Warning |
| 350 | file_object = StringIO.StringIO() |
Brett Cannon | e974689 | 2008-04-12 23:44:07 +0000 | [diff] [blame] | 351 | expect = self.module.formatwarning(message, category, file_name, |
| 352 | line_num) |
| 353 | self.module.showwarning(message, category, file_name, line_num, |
Brett Cannon | 905c31c | 2007-12-20 10:09:52 +0000 | [diff] [blame] | 354 | file_object) |
| 355 | self.failUnlessEqual(file_object.getvalue(), expect) |
Brett Cannon | e974689 | 2008-04-12 23:44:07 +0000 | [diff] [blame] | 356 | # Test 'line' argument. |
| 357 | expected_file_line += "for the win!" |
| 358 | expect = self.module.formatwarning(message, category, file_name, |
| 359 | line_num, expected_file_line) |
| 360 | file_object = StringIO.StringIO() |
| 361 | self.module.showwarning(message, category, file_name, line_num, |
| 362 | file_object, expected_file_line) |
| 363 | self.failUnlessEqual(expect, file_object.getvalue()) |
| 364 | |
| 365 | class CWarningsDisplayTests(WarningsDisplayTests): |
| 366 | module = c_warnings |
| 367 | |
| 368 | class PyWarningsDisplayTests(WarningsDisplayTests): |
| 369 | module = py_warnings |
Brett Cannon | 905c31c | 2007-12-20 10:09:52 +0000 | [diff] [blame] | 370 | |
| 371 | |
Brett Cannon | e974689 | 2008-04-12 23:44:07 +0000 | [diff] [blame] | 372 | def test_main(): |
Thomas Wouters | 767833d | 2006-04-16 15:43:39 +0000 | [diff] [blame] | 373 | # Obscure hack so that this test passes after reloads or repeated calls |
| 374 | # to test_main (regrtest -R). |
| 375 | if '__warningregistry__' in globals(): |
| 376 | del globals()['__warningregistry__'] |
Brett Cannon | 855da6c | 2007-08-17 20:16:15 +0000 | [diff] [blame] | 377 | if hasattr(warning_tests, '__warningregistry__'): |
| 378 | del warning_tests.__warningregistry__ |
| 379 | if hasattr(sys, '__warningregistry__'): |
| 380 | del sys.__warningregistry__ |
Brett Cannon | e974689 | 2008-04-12 23:44:07 +0000 | [diff] [blame] | 381 | test_support.run_unittest(CFilterTests, PyFilterTests, |
| 382 | CWarnTests, PyWarnTests, |
| 383 | CWCmdLineTests, PyWCmdLineTests, |
| 384 | _WarningsTests, |
| 385 | CWarningsDisplayTests, PyWarningsDisplayTests, |
| 386 | ) |
| 387 | |
| 388 | |
Raymond Hettinger | d6f6e50 | 2003-07-13 08:37:40 +0000 | [diff] [blame] | 389 | |
| 390 | if __name__ == "__main__": |
Brett Cannon | e974689 | 2008-04-12 23:44:07 +0000 | [diff] [blame] | 391 | test_main() |