diff options
Diffstat (limited to 'Lib/test/test_warnings.py')
-rw-r--r-- | Lib/test/test_warnings.py | 57 |
1 files changed, 55 insertions, 2 deletions
diff --git a/Lib/test/test_warnings.py b/Lib/test/test_warnings.py index 388b5e9..b37cdf7 100644 --- a/Lib/test/test_warnings.py +++ b/Lib/test/test_warnings.py @@ -517,10 +517,12 @@ class CatchWarningTests(BaseTest): wmod = self.module orig_filters = wmod.filters orig_showwarning = wmod.showwarning - with wmod.catch_warnings(record=True, module=wmod): + # Ensure both showwarning and filters are restored when recording + with wmod.catch_warnings(module=wmod, record=True): wmod.filters = wmod.showwarning = object() self.assert_(wmod.filters is orig_filters) self.assert_(wmod.showwarning is orig_showwarning) + # Same test, but with recording disabled with wmod.catch_warnings(module=wmod, record=False): wmod.filters = wmod.showwarning = object() self.assert_(wmod.filters is orig_filters) @@ -528,9 +530,10 @@ class CatchWarningTests(BaseTest): def test_catch_warnings_recording(self): wmod = self.module + # Ensure warnings are recorded when requested with wmod.catch_warnings(module=wmod, record=True) as w: self.assertEqual(w, []) - self.assertRaises(AttributeError, getattr, w, 'message') + self.assert_(type(w) is list) wmod.simplefilter("always") wmod.warn("foo") self.assertEqual(str(w[-1].message), "foo") @@ -540,11 +543,61 @@ class CatchWarningTests(BaseTest): self.assertEqual(str(w[1].message), "bar") del w[:] self.assertEqual(w, []) + # Ensure warnings are not recorded when not requested orig_showwarning = wmod.showwarning with wmod.catch_warnings(module=wmod, record=False) as w: self.assert_(w is None) self.assert_(wmod.showwarning is orig_showwarning) + def test_catch_warnings_reentry_guard(self): + wmod = self.module + # Ensure catch_warnings is protected against incorrect usage + x = wmod.catch_warnings(module=wmod, record=True) + self.assertRaises(RuntimeError, x.__exit__) + with x: + self.assertRaises(RuntimeError, x.__enter__) + # Same test, but with recording disabled + x = wmod.catch_warnings(module=wmod, record=False) + self.assertRaises(RuntimeError, x.__exit__) + with x: + self.assertRaises(RuntimeError, x.__enter__) + + def test_catch_warnings_defaults(self): + wmod = self.module + orig_filters = wmod.filters + orig_showwarning = wmod.showwarning + # Ensure default behaviour is not to record warnings + with wmod.catch_warnings(module=wmod) as w: + self.assert_(w is None) + self.assert_(wmod.showwarning is orig_showwarning) + self.assert_(wmod.filters is not orig_filters) + self.assert_(wmod.filters is orig_filters) + if wmod is sys.modules['warnings']: + # Ensure the default module is this one + with wmod.catch_warnings() as w: + self.assert_(w is None) + self.assert_(wmod.showwarning is orig_showwarning) + self.assert_(wmod.filters is not orig_filters) + self.assert_(wmod.filters is orig_filters) + + def test_check_warnings(self): + # Explicit tests for the test_support convenience wrapper + wmod = self.module + if wmod is sys.modules['warnings']: + with test_support.check_warnings() as w: + self.assertEqual(w.warnings, []) + wmod.simplefilter("always") + wmod.warn("foo") + self.assertEqual(str(w.message), "foo") + wmod.warn("bar") + self.assertEqual(str(w.message), "bar") + self.assertEqual(str(w.warnings[0].message), "foo") + self.assertEqual(str(w.warnings[1].message), "bar") + w.reset() + self.assertEqual(w.warnings, []) + + + class CCatchWarningTests(CatchWarningTests): module = c_warnings |