Back to index

python3.2  3.2.2
Public Member Functions | Static Public Attributes
test.test_warnings.CCatchWarningTests Class Reference
Inheritance diagram for test.test_warnings.CCatchWarningTests:
Inheritance graph
[legend]
Collaboration diagram for test.test_warnings.CCatchWarningTests:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def test_catch_warnings_restore
def test_catch_warnings_recording
def test_catch_warnings_reentry_guard
def test_catch_warnings_defaults
def test_check_warnings
def setUp
def tearDown

Static Public Attributes

 module = c_warnings

Detailed Description

Definition at line 711 of file test_warnings.py.


Member Function Documentation

def test.test_warnings.BaseTest.setUp (   self) [inherited]

Definition at line 47 of file test_warnings.py.

00047 
00048     def setUp(self):
00049         # The __warningregistry__ needs to be in a pristine state for tests
00050         # to work properly.
00051         if '__warningregistry__' in globals():
00052             del globals()['__warningregistry__']
00053         if hasattr(warning_tests, '__warningregistry__'):
00054             del warning_tests.__warningregistry__
00055         if hasattr(sys, '__warningregistry__'):
00056             del sys.__warningregistry__
00057         # The 'warnings' module must be explicitly set so that the proper
00058         # interaction between _warnings and 'warnings' can be controlled.
00059         sys.modules['warnings'] = self.module
00060         super(BaseTest, self).setUp()

Here is the caller graph for this function:

def test.test_warnings.BaseTest.tearDown (   self) [inherited]

Definition at line 61 of file test_warnings.py.

00061 
00062     def tearDown(self):
00063         sys.modules['warnings'] = original_warnings
00064         super(BaseTest, self).tearDown()
00065 

Here is the caller graph for this function:

Definition at line 662 of file test_warnings.py.

00662 
00663     def test_catch_warnings_defaults(self):
00664         wmod = self.module
00665         orig_filters = wmod.filters
00666         orig_showwarning = wmod.showwarning
00667         # Ensure default behaviour is not to record warnings
00668         with wmod.catch_warnings(module=wmod) as w:
00669             self.assertTrue(w is None)
00670             self.assertTrue(wmod.showwarning is orig_showwarning)
00671             self.assertTrue(wmod.filters is not orig_filters)
00672         self.assertTrue(wmod.filters is orig_filters)
00673         if wmod is sys.modules['warnings']:
00674             # Ensure the default module is this one
00675             with wmod.catch_warnings() as w:
00676                 self.assertTrue(w is None)
00677                 self.assertTrue(wmod.showwarning is orig_showwarning)
00678                 self.assertTrue(wmod.filters is not orig_filters)
00679             self.assertTrue(wmod.filters is orig_filters)

Here is the call graph for this function:

Definition at line 628 of file test_warnings.py.

00628 
00629     def test_catch_warnings_recording(self):
00630         wmod = self.module
00631         # Ensure warnings are recorded when requested
00632         with wmod.catch_warnings(module=wmod, record=True) as w:
00633             self.assertEqual(w, [])
00634             self.assertTrue(type(w) is list)
00635             wmod.simplefilter("always")
00636             wmod.warn("foo")
00637             self.assertEqual(str(w[-1].message), "foo")
00638             wmod.warn("bar")
00639             self.assertEqual(str(w[-1].message), "bar")
00640             self.assertEqual(str(w[0].message), "foo")
00641             self.assertEqual(str(w[1].message), "bar")
00642             del w[:]
00643             self.assertEqual(w, [])
00644         # Ensure warnings are not recorded when not requested
00645         orig_showwarning = wmod.showwarning
00646         with wmod.catch_warnings(module=wmod, record=False) as w:
00647             self.assertTrue(w is None)
00648             self.assertTrue(wmod.showwarning is orig_showwarning)

Here is the call graph for this function:

Definition at line 649 of file test_warnings.py.

00649 
00650     def test_catch_warnings_reentry_guard(self):
00651         wmod = self.module
00652         # Ensure catch_warnings is protected against incorrect usage
00653         x = wmod.catch_warnings(module=wmod, record=True)
00654         self.assertRaises(RuntimeError, x.__exit__)
00655         with x:
00656             self.assertRaises(RuntimeError, x.__enter__)
00657         # Same test, but with recording disabled
00658         x = wmod.catch_warnings(module=wmod, record=False)
00659         self.assertRaises(RuntimeError, x.__exit__)
00660         with x:
00661             self.assertRaises(RuntimeError, x.__enter__)

Here is the call graph for this function:

Definition at line 613 of file test_warnings.py.

00613 
00614     def test_catch_warnings_restore(self):
00615         wmod = self.module
00616         orig_filters = wmod.filters
00617         orig_showwarning = wmod.showwarning
00618         # Ensure both showwarning and filters are restored when recording
00619         with wmod.catch_warnings(module=wmod, record=True):
00620             wmod.filters = wmod.showwarning = object()
00621         self.assertTrue(wmod.filters is orig_filters)
00622         self.assertTrue(wmod.showwarning is orig_showwarning)
00623         # Same test, but with recording disabled
00624         with wmod.catch_warnings(module=wmod, record=False):
00625             wmod.filters = wmod.showwarning = object()
00626         self.assertTrue(wmod.filters is orig_filters)
00627         self.assertTrue(wmod.showwarning is orig_showwarning)

Here is the call graph for this function:

Definition at line 680 of file test_warnings.py.

00680 
00681     def test_check_warnings(self):
00682         # Explicit tests for the test.support convenience wrapper
00683         wmod = self.module
00684         if wmod is not sys.modules['warnings']:
00685             return
00686         with support.check_warnings(quiet=False) as w:
00687             self.assertEqual(w.warnings, [])
00688             wmod.simplefilter("always")
00689             wmod.warn("foo")
00690             self.assertEqual(str(w.message), "foo")
00691             wmod.warn("bar")
00692             self.assertEqual(str(w.message), "bar")
00693             self.assertEqual(str(w.warnings[0].message), "foo")
00694             self.assertEqual(str(w.warnings[1].message), "bar")
00695             w.reset()
00696             self.assertEqual(w.warnings, [])
00697 
00698         with support.check_warnings():
00699             # defaults to quiet=True without argument
00700             pass
00701         with support.check_warnings(('foo', UserWarning)):
00702             wmod.warn("foo")
00703 
00704         with self.assertRaises(AssertionError):
00705             with support.check_warnings(('', RuntimeWarning)):
00706                 # defaults to quiet=False with argument
00707                 pass
00708         with self.assertRaises(AssertionError):
00709             with support.check_warnings(('foo', RuntimeWarning)):
00710                 wmod.warn("foo")

Here is the call graph for this function:


Member Data Documentation

Definition at line 712 of file test_warnings.py.


The documentation for this class was generated from the following file: