Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(17)

Side by Side Diff: Lib/test/test_warnings.py

Issue 3255: Issue #3602 (Closed) SVN Base: http://svn.python.org/view/*checkout*/python/trunk/
Patch Set: Created 1 year, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
OLDNEW
1 from contextlib import contextmanager 1 from contextlib import contextmanager
2 import linecache 2 import linecache
3 import os 3 import os
4 import StringIO 4 import StringIO
5 import sys 5 import sys
6 import unittest 6 import unittest
7 from test import test_support 7 from test import test_support
8 8
9 import warning_tests 9 import warning_tests
10 10
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
72 """Testing the filtering functionality.""" 72 """Testing the filtering functionality."""
73 73
74 def test_error(self): 74 def test_error(self):
75 with test_support.catch_warning(self.module) as w: 75 with test_support.catch_warning(self.module) as w:
76 self.module.resetwarnings() 76 self.module.resetwarnings()
77 self.module.filterwarnings("error", category=UserWarning) 77 self.module.filterwarnings("error", category=UserWarning)
78 self.assertRaises(UserWarning, self.module.warn, 78 self.assertRaises(UserWarning, self.module.warn,
79 "FilterTests.test_error") 79 "FilterTests.test_error")
80 80
81 def test_ignore(self): 81 def test_ignore(self):
82 with test_support.catch_warning(self.module) as w: 82 with test_support.catch_warning(module=self.module) as w:
83 self.module.resetwarnings() 83 self.module.resetwarnings()
84 self.module.filterwarnings("ignore", category=UserWarning) 84 self.module.filterwarnings("ignore", category=UserWarning)
85 self.module.warn("FilterTests.test_ignore", UserWarning) 85 self.module.warn("FilterTests.test_ignore", UserWarning)
86 self.assert_(not w.message) 86 self.assertEquals(len(w), 0)
87 87
88 def test_always(self): 88 def test_always(self):
89 with test_support.catch_warning(self.module) as w: 89 with test_support.catch_warning(module=self.module) as w:
90 self.module.resetwarnings() 90 self.module.resetwarnings()
91 self.module.filterwarnings("always", category=UserWarning) 91 self.module.filterwarnings("always", category=UserWarning)
92 message = "FilterTests.test_always" 92 message = "FilterTests.test_always"
93 self.module.warn(message, UserWarning) 93 self.module.warn(message, UserWarning)
94 self.assert_(message, w.message) 94 self.assert_(message, w.message)
95 w.message = None # Reset.
96 self.module.warn(message, UserWarning) 95 self.module.warn(message, UserWarning)
97 self.assert_(w.message, message) 96 self.assert_(w.message, message)
98 97
99 def test_default(self): 98 def test_default(self):
100 with test_support.catch_warning(self.module) as w: 99 with test_support.catch_warning(self.module) as w:
101 self.module.resetwarnings() 100 self.module.resetwarnings()
102 self.module.filterwarnings("default", category=UserWarning) 101 self.module.filterwarnings("default", category=UserWarning)
103 message = UserWarning("FilterTests.test_default") 102 message = UserWarning("FilterTests.test_default")
104 for x in xrange(2): 103 for x in xrange(2):
105 self.module.warn(message, UserWarning) 104 self.module.warn(message, UserWarning)
106 if x == 0: 105 if x == 0:
107 self.assertEquals(w.message, message) 106 self.assertEquals(w.message, message)
108 w.reset() 107 w.reset()
109 elif x == 1: 108 elif x == 1:
110 self.assert_(not w.message, "unexpected warning: " + str(w)) 109 self.assert_(not len(w), "unexpected warning: " + str(w))
111 else: 110 else:
112 raise ValueError("loop variant unhandled") 111 raise ValueError("loop variant unhandled")
113 112
114 def test_module(self): 113 def test_module(self):
115 with test_support.catch_warning(self.module) as w: 114 with test_support.catch_warning(self.module) as w:
116 self.module.resetwarnings() 115 self.module.resetwarnings()
117 self.module.filterwarnings("module", category=UserWarning) 116 self.module.filterwarnings("module", category=UserWarning)
118 message = UserWarning("FilterTests.test_module") 117 message = UserWarning("FilterTests.test_module")
119 self.module.warn(message, UserWarning) 118 self.module.warn(message, UserWarning)
120 self.assertEquals(w.message, message) 119 self.assertEquals(w.message, message)
121 w.reset() 120 w.reset()
122 self.module.warn(message, UserWarning) 121 self.module.warn(message, UserWarning)
123 self.assert_(not w.message, "unexpected message: " + str(w)) 122 self.assert_(not len(w), "unexpected message: " + str(w))
124 123
125 def test_once(self): 124 def test_once(self):
126 with test_support.catch_warning(self.module) as w: 125 with test_support.catch_warning(self.module) as w:
127 self.module.resetwarnings() 126 self.module.resetwarnings()
128 self.module.filterwarnings("once", category=UserWarning) 127 self.module.filterwarnings("once", category=UserWarning)
129 message = UserWarning("FilterTests.test_once") 128 message = UserWarning("FilterTests.test_once")
130 self.module.warn_explicit(message, UserWarning, "test_warnings.py", 129 self.module.warn_explicit(message, UserWarning, "test_warnings.py",
131 42) 130 42)
132 self.assertEquals(w.message, message) 131 self.assertEquals(w.message, message)
133 w.reset() 132 w.reset()
134 self.module.warn_explicit(message, UserWarning, "test_warnings.py", 133 self.module.warn_explicit(message, UserWarning, "test_warnings.py",
135 13) 134 13)
136 self.assert_(not w.message) 135 self.assertEquals(len(w), 0)
137 self.module.warn_explicit(message, UserWarning, "test_warnings2.py", 136 self.module.warn_explicit(message, UserWarning, "test_warnings2.py",
138 42) 137 42)
139 self.assert_(not w.message) 138 self.assertEquals(len(w), 0)
140 139
141 def test_inheritance(self): 140 def test_inheritance(self):
142 with test_support.catch_warning(self.module) as w: 141 with test_support.catch_warning(self.module) as w:
143 self.module.resetwarnings() 142 self.module.resetwarnings()
144 self.module.filterwarnings("error", category=Warning) 143 self.module.filterwarnings("error", category=Warning)
145 self.assertRaises(UserWarning, self.module.warn, 144 self.assertRaises(UserWarning, self.module.warn,
146 "FilterTests.test_inheritance", UserWarning) 145 "FilterTests.test_inheritance", UserWarning)
147 146
148 def test_ordering(self): 147 def test_ordering(self):
149 with test_support.catch_warning(self.module) as w: 148 with test_support.catch_warning(self.module) as w:
150 self.module.resetwarnings() 149 self.module.resetwarnings()
151 self.module.filterwarnings("ignore", category=UserWarning) 150 self.module.filterwarnings("ignore", category=UserWarning)
152 self.module.filterwarnings("error", category=UserWarning, 151 self.module.filterwarnings("error", category=UserWarning,
153 append=True) 152 append=True)
154 w.reset() 153 w.reset()
155 try: 154 try:
156 self.module.warn("FilterTests.test_ordering", UserWarning) 155 self.module.warn("FilterTests.test_ordering", UserWarning)
157 except UserWarning: 156 except UserWarning:
158 self.fail("order handling for actions failed") 157 self.fail("order handling for actions failed")
159 self.assert_(not w.message) 158 self.assertEquals(len(w), 0)
160 159
161 def test_filterwarnings(self): 160 def test_filterwarnings(self):
162 # Test filterwarnings(). 161 # Test filterwarnings().
163 # Implicitly also tests resetwarnings(). 162 # Implicitly also tests resetwarnings().
164 with test_support.catch_warning(self.module) as w: 163 with test_support.catch_warning(self.module) as w:
165 self.module.filterwarnings("error", "", Warning, "", 0) 164 self.module.filterwarnings("error", "", Warning, "", 0)
166 self.assertRaises(UserWarning, self.module.warn, 'convert to error') 165 self.assertRaises(UserWarning, self.module.warn, 'convert to error')
167 166
168 self.module.resetwarnings() 167 self.module.resetwarnings()
169 text = 'handle normally' 168 text = 'handle normally'
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after
308 self.assertRaises(TypeError, self.module.warn_explicit, 307 self.assertRaises(TypeError, self.module.warn_explicit,
309 None, UserWarning, None, None) 308 None, UserWarning, None, None)
310 # Either 'message' needs to be an instance of Warning or 'category' 309 # Either 'message' needs to be an instance of Warning or 'category'
311 # needs to be a subclass. 310 # needs to be a subclass.
312 self.assertRaises(TypeError, self.module.warn_explicit, 311 self.assertRaises(TypeError, self.module.warn_explicit,
313 None, None, None, 1) 312 None, None, None, 1)
314 # 'registry' must be a dict or None. 313 # 'registry' must be a dict or None.
315 self.assertRaises((TypeError, AttributeError), 314 self.assertRaises((TypeError, AttributeError),
316 self.module.warn_explicit, 315 self.module.warn_explicit,
317 None, Warning, None, 1, registry=42) 316 None, Warning, None, 1, registry=42)
318
319 317
320 318
321 class CWarnTests(BaseTest, WarnTests): 319 class CWarnTests(BaseTest, WarnTests):
322 module = c_warnings 320 module = c_warnings
323 321
324 class PyWarnTests(BaseTest, WarnTests): 322 class PyWarnTests(BaseTest, WarnTests):
325 module = py_warnings 323 module = py_warnings
326 324
327 325
328 class WCmdLineTests(unittest.TestCase): 326 class WCmdLineTests(unittest.TestCase):
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
370 try: 368 try:
371 original_registry = self.module.onceregistry 369 original_registry = self.module.onceregistry
372 __warningregistry__ = {} 370 __warningregistry__ = {}
373 with test_support.catch_warning(self.module) as w: 371 with test_support.catch_warning(self.module) as w:
374 self.module.resetwarnings() 372 self.module.resetwarnings()
375 self.module.filterwarnings("once", category=UserWarning) 373 self.module.filterwarnings("once", category=UserWarning)
376 self.module.warn_explicit(message, UserWarning, "file", 42) 374 self.module.warn_explicit(message, UserWarning, "file", 42)
377 self.failUnlessEqual(w.message, message) 375 self.failUnlessEqual(w.message, message)
378 w.reset() 376 w.reset()
379 self.module.warn_explicit(message, UserWarning, "file", 42) 377 self.module.warn_explicit(message, UserWarning, "file", 42)
380 self.assert_(not w.message) 378 self.assertEquals(len(w), 0)
381 # Test the resetting of onceregistry. 379 # Test the resetting of onceregistry.
382 self.module.onceregistry = {} 380 self.module.onceregistry = {}
383 __warningregistry__ = {} 381 __warningregistry__ = {}
384 self.module.warn('onceregistry test') 382 self.module.warn('onceregistry test')
385 self.failUnlessEqual(w.message.args, message.args) 383 self.failUnlessEqual(w.message.args, message.args)
386 # Removal of onceregistry is okay. 384 # Removal of onceregistry is okay.
387 w.reset() 385 w.reset()
388 del self.module.onceregistry 386 del self.module.onceregistry
389 __warningregistry__ = {} 387 __warningregistry__ = {}
390 self.module.warn_explicit(message, UserWarning, "file", 42) 388 self.module.warn_explicit(message, UserWarning, "file", 42)
391 self.failUnless(not w.message) 389 self.assertEquals(len(w), 0)
392 finally: 390 finally:
393 self.module.onceregistry = original_registry 391 self.module.onceregistry = original_registry
394 392
395 def test_showwarning_missing(self): 393 def test_showwarning_missing(self):
396 # Test that showwarning() missing is okay. 394 # Test that showwarning() missing is okay.
397 text = 'del showwarning test' 395 text = 'del showwarning test'
398 with test_support.catch_warning(self.module): 396 with test_support.catch_warning(self.module):
399 self.module.filterwarnings("always", category=UserWarning) 397 self.module.filterwarnings("always", category=UserWarning)
400 del self.module.showwarning 398 del self.module.showwarning
401 with test_support.captured_output('stderr') as stream: 399 with test_support.captured_output('stderr') as stream:
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
482 self.failUnlessEqual(expect, file_object.getvalue()) 480 self.failUnlessEqual(expect, file_object.getvalue())
483 481
484 class CWarningsDisplayTests(BaseTest, WarningsDisplayTests): 482 class CWarningsDisplayTests(BaseTest, WarningsDisplayTests):
485 module = c_warnings 483 module = c_warnings
486 484
487 class PyWarningsDisplayTests(BaseTest, WarningsDisplayTests): 485 class PyWarningsDisplayTests(BaseTest, WarningsDisplayTests):
488 module = py_warnings 486 module = py_warnings
489 487
490 488
491 489
492 class WarningsSupportTests(object): 490 class CatchWarningTests(BaseTest):
493 """Test the warning tools from test support module"""
494 491
495 def test_catch_warning_restore(self): 492 """Test catch_warnings()."""
493
494 def test_catch_warnings_restore(self):
496 wmod = self.module 495 wmod = self.module
497 orig_filters = wmod.filters 496 orig_filters = wmod.filters
498 orig_showwarning = wmod.showwarning 497 orig_showwarning = wmod.showwarning
499 with test_support.catch_warning(wmod): 498 with wmod.catch_warnings(record=True, module=wmod):
500 wmod.filters = wmod.showwarning = object() 499 wmod.filters = wmod.showwarning = object()
501 self.assert_(wmod.filters is orig_filters) 500 self.assert_(wmod.filters is orig_filters)
502 self.assert_(wmod.showwarning is orig_showwarning) 501 self.assert_(wmod.showwarning is orig_showwarning)
503 with test_support.catch_warning(wmod, record=False): 502 with wmod.catch_warnings(module=wmod, record=False):
504 wmod.filters = wmod.showwarning = object() 503 wmod.filters = wmod.showwarning = object()
505 self.assert_(wmod.filters is orig_filters) 504 self.assert_(wmod.filters is orig_filters)
506 self.assert_(wmod.showwarning is orig_showwarning) 505 self.assert_(wmod.showwarning is orig_showwarning)
507 506
508 def test_catch_warning_recording(self): 507 def test_catch_warnings_recording(self):
509 wmod = self.module 508 wmod = self.module
510 with test_support.catch_warning(wmod) as w: 509 with wmod.catch_warnings(module=wmod, record=True) as w:
511 self.assertEqual(w.warnings, []) 510 self.assertEqual(w, [])
512 wmod.simplefilter("always") 511 wmod.simplefilter("always")
513 wmod.warn("foo") 512 wmod.warn("foo")
514 self.assertEqual(str(w.message), "foo") 513 self.assertEqual(str(w.message), "foo")
515 wmod.warn("bar") 514 wmod.warn("bar")
516 self.assertEqual(str(w.message), "bar") 515 self.assertEqual(str(w.message), "bar")
517 self.assertEqual(str(w.warnings[0].message), "foo") 516 self.assertEqual(str(w[0].message), "foo")
518 self.assertEqual(str(w.warnings[1].message), "bar") 517 self.assertEqual(str(w[1].message), "bar")
519 w.reset() 518 w.reset()
520 self.assertEqual(w.warnings, []) 519 self.assertEqual(w, [])
521 orig_showwarning = wmod.showwarning 520 orig_showwarning = wmod.showwarning
522 with test_support.catch_warning(wmod, record=False) as w: 521 with wmod.catch_warnings(module=wmod, record=False) as w:
523 self.assert_(w is None) 522 self.assert_(w is None)
524 self.assert_(wmod.showwarning is orig_showwarning) 523 self.assert_(wmod.showwarning is orig_showwarning)
525 524
526 525 class CCatchWarningTests(CatchWarningTests):
527 class CWarningsSupportTests(BaseTest, WarningsSupportTests):
528 module = c_warnings 526 module = c_warnings
529 527
530 class PyWarningsSupportTests(BaseTest, WarningsSupportTests): 528 class PyCatchWarningTests(CatchWarningTests):
531 module = py_warnings 529 module = py_warnings
532 530
533 531
534 class ShowwarningDeprecationTests(BaseTest): 532 class ShowwarningDeprecationTests(BaseTest):
535 533
536 """Test the deprecation of the old warnings.showwarning() API works.""" 534 """Test the deprecation of the old warnings.showwarning() API works."""
537 535
538 @staticmethod 536 @staticmethod
539 def bad_showwarning(message, category, filename, lineno, file=None): 537 def bad_showwarning(message, category, filename, lineno, file=None):
538 pass
539
540 @staticmethod
541 def ok_showwarning(*args):
540 pass 542 pass
541 543
542 def test_deprecation(self): 544 def test_deprecation(self):
543 # message, category, filename, lineno[, file[, line]] 545 # message, category, filename, lineno[, file[, line]]
544 args = ("message", UserWarning, "file name", 42) 546 args = ("message", UserWarning, "file name", 42)
545 with test_support.catch_warning(self.module): 547 with test_support.catch_warning(module=self.module):
546 self.module.filterwarnings("error", category=DeprecationWarning) 548 self.module.filterwarnings("error", category=DeprecationWarning)
547 self.module.showwarning = self.bad_showwarning 549 self.module.showwarning = self.bad_showwarning
548 self.assertRaises(DeprecationWarning, self.module.warn_explicit, 550 self.assertRaises(DeprecationWarning, self.module.warn_explicit,
549 *args) 551 *args)
552 self.module.showwarning = self.ok_showwarning
553 try:
554 self.module.warn_explicit(*args)
555 except DeprecationWarning as exc:
556 self.fail('showwarning(*args) should not trigger a '
557 'DeprecationWarning')
550 558
551 class CShowwarningDeprecationTests(ShowwarningDeprecationTests): 559 class CShowwarningDeprecationTests(ShowwarningDeprecationTests):
552 module = c_warnings 560 module = c_warnings
553 561
554 562
555 class PyShowwarningDeprecationTests(ShowwarningDeprecationTests): 563 class PyShowwarningDeprecationTests(ShowwarningDeprecationTests):
556 module = py_warnings 564 module = py_warnings
557 565
558 566
559 def test_main(): 567 def test_main():
560 py_warnings.onceregistry.clear() 568 py_warnings.onceregistry.clear()
561 c_warnings.onceregistry.clear() 569 c_warnings.onceregistry.clear()
562 test_support.run_unittest(CFilterTests, 570 test_support.run_unittest(CFilterTests, PyFilterTests,
563 PyFilterTests, 571 CWarnTests, PyWarnTests,
564 CWarnTests,
565 PyWarnTests,
566 CWCmdLineTests, PyWCmdLineTests, 572 CWCmdLineTests, PyWCmdLineTests,
567 _WarningsTests, 573 _WarningsTests,
568 CWarningsDisplayTests, PyWarningsDisplayTests, 574 CWarningsDisplayTests, PyWarningsDisplayTests,
569 CWarningsSupportTests, PyWarningsSupportTests, 575 CCatchWarningTests, PyCatchWarningTests,
570 CShowwarningDeprecationTests, 576 CShowwarningDeprecationTests,
571 PyShowwarningDeprecationTests, 577 PyShowwarningDeprecationTests,
572 ) 578 )
573 579
574 580
575 if __name__ == "__main__": 581 if __name__ == "__main__":
576 test_main() 582 test_main()
OLDNEW

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld r483