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

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

Issue 4180: warnings.simplefilter("always") does not make warnings always show up
Patch Set: Created 11 years, 4 months ago
Left:
Right:
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
« no previous file with comments | « no previous file | Python/_warnings.c » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
53 # to work properly. 53 # to work properly.
54 if '__warningregistry__' in globals(): 54 if '__warningregistry__' in globals():
55 del globals()['__warningregistry__'] 55 del globals()['__warningregistry__']
56 if hasattr(warning_tests, '__warningregistry__'): 56 if hasattr(warning_tests, '__warningregistry__'):
57 del warning_tests.__warningregistry__ 57 del warning_tests.__warningregistry__
58 if hasattr(sys, '__warningregistry__'): 58 if hasattr(sys, '__warningregistry__'):
59 del sys.__warningregistry__ 59 del sys.__warningregistry__
60 # The 'warnings' module must be explicitly set so that the proper 60 # The 'warnings' module must be explicitly set so that the proper
61 # interaction between _warnings and 'warnings' can be controlled. 61 # interaction between _warnings and 'warnings' can be controlled.
62 sys.modules['warnings'] = self.module 62 sys.modules['warnings'] = self.module
63 self.save_warn_explicit = self.module.warn_explicit
63 super(BaseTest, self).setUp() 64 super(BaseTest, self).setUp()
64 65
65 def tearDown(self): 66 def tearDown(self):
66 sys.modules['warnings'] = original_warnings 67 sys.modules['warnings'] = original_warnings
68 self.module.warn_explicit = self.save_warn_explicit
67 super(BaseTest, self).tearDown() 69 super(BaseTest, self).tearDown()
68 70
69 71
70 class FilterTests(object): 72 class FilterTests(object):
71 73
72 """Testing the filtering functionality.""" 74 """Testing the filtering functionality."""
73 75
74 def test_error(self): 76 def test_error(self):
75 with original_warnings.catch_warnings(module=self.module) as w: 77 with original_warnings.catch_warnings(module=self.module) as w:
76 self.module.resetwarnings() 78 self.module.resetwarnings()
(...skipping 339 matching lines...) Expand 10 before | Expand all | Expand 10 after
416 def test_showwarning_missing(self): 418 def test_showwarning_missing(self):
417 # Test that showwarning() missing is okay. 419 # Test that showwarning() missing is okay.
418 text = 'del showwarning test' 420 text = 'del showwarning test'
419 with original_warnings.catch_warnings(module=self.module): 421 with original_warnings.catch_warnings(module=self.module):
420 self.module.filterwarnings("always", category=UserWarning) 422 self.module.filterwarnings("always", category=UserWarning)
421 del self.module.showwarning 423 del self.module.showwarning
422 with test_support.captured_output('stderr') as stream: 424 with test_support.captured_output('stderr') as stream:
423 self.module.warn(text) 425 self.module.warn(text)
424 result = stream.getvalue() 426 result = stream.getvalue()
425 self.failUnless(text in result) 427 self.failUnless(text in result)
428
429 def test_replace_warn_explicit(self):
430 self.called = False
431 def my_warn_explicit(*args):
432 self.called = True
433 self.module.warn_explicit = my_warn_explicit
434 self.module.warn("foo")
435 self.assertTrue(self.called)
426 436
427 def test_showwarning_not_callable(self): 437 def test_showwarning_not_callable(self):
428 self.module.filterwarnings("always", category=UserWarning) 438 self.module.filterwarnings("always", category=UserWarning)
429 old_showwarning = self.module.showwarning 439 old_showwarning = self.module.showwarning
430 self.module.showwarning = 23 440 self.module.showwarning = 23
431 try: 441 try:
432 self.assertRaises(TypeError, self.module.warn, "Warning!") 442 self.assertRaises(TypeError, self.module.warn, "Warning!")
433 finally: 443 finally:
434 self.module.showwarning = old_showwarning 444 self.module.showwarning = old_showwarning
435 self.module.resetwarnings() 445 self.module.resetwarnings()
(...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after
649 _WarningsTests, 659 _WarningsTests,
650 CWarningsDisplayTests, PyWarningsDisplayTests, 660 CWarningsDisplayTests, PyWarningsDisplayTests,
651 CCatchWarningTests, PyCatchWarningTests, 661 CCatchWarningTests, PyCatchWarningTests,
652 CShowwarningDeprecationTests, 662 CShowwarningDeprecationTests,
653 PyShowwarningDeprecationTests, 663 PyShowwarningDeprecationTests,
654 ) 664 )
655 665
656 666
657 if __name__ == "__main__": 667 if __name__ == "__main__":
658 test_main() 668 test_main()
OLDNEW
« no previous file with comments | « no previous file | Python/_warnings.c » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+