Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1 | from contextlib import contextmanager |
Christian Heimes | 5fb7c2a | 2007-12-24 08:52:31 +0000 | [diff] [blame] | 2 | import linecache |
Raymond Hettinger | dc9dcf1 | 2003-07-13 06:15:11 +0000 | [diff] [blame] | 3 | import os |
Christian Heimes | 5fb7c2a | 2007-12-24 08:52:31 +0000 | [diff] [blame] | 4 | from io import StringIO |
Guido van Rossum | 61e21b5 | 2007-08-20 19:06:03 +0000 | [diff] [blame] | 5 | import sys |
Raymond Hettinger | d6f6e50 | 2003-07-13 08:37:40 +0000 | [diff] [blame] | 6 | import unittest |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 7 | from test import support |
Jeremy Hylton | 8501466 | 2003-07-11 15:37:59 +0000 | [diff] [blame] | 8 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 9 | from test import warning_tests |
Jeremy Hylton | 8501466 | 2003-07-11 15:37:59 +0000 | [diff] [blame] | 10 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 11 | import warnings as original_warnings |
Jeremy Hylton | 8501466 | 2003-07-11 15:37:59 +0000 | [diff] [blame] | 12 | |
Nick Coghlan | 4738470 | 2009-04-22 16:13:36 +0000 | [diff] [blame] | 13 | py_warnings = support.import_fresh_module('warnings', blocked=['_warnings']) |
| 14 | c_warnings = support.import_fresh_module('warnings', fresh=['_warnings']) |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 15 | |
| 16 | @contextmanager |
| 17 | def warnings_state(module): |
| 18 | """Use a specific warnings implementation in warning_tests.""" |
| 19 | global __warningregistry__ |
| 20 | for to_clear in (sys, warning_tests): |
| 21 | try: |
| 22 | to_clear.__warningregistry__.clear() |
| 23 | except AttributeError: |
| 24 | pass |
| 25 | try: |
| 26 | __warningregistry__.clear() |
| 27 | except NameError: |
| 28 | pass |
| 29 | original_warnings = warning_tests.warnings |
| 30 | try: |
| 31 | warning_tests.warnings = module |
| 32 | yield |
| 33 | finally: |
| 34 | warning_tests.warnings = original_warnings |
| 35 | |
| 36 | |
| 37 | class BaseTest(unittest.TestCase): |
| 38 | |
| 39 | """Basic bookkeeping required for testing.""" |
| 40 | |
| 41 | def setUp(self): |
| 42 | # The __warningregistry__ needs to be in a pristine state for tests |
| 43 | # to work properly. |
| 44 | if '__warningregistry__' in globals(): |
| 45 | del globals()['__warningregistry__'] |
| 46 | if hasattr(warning_tests, '__warningregistry__'): |
| 47 | del warning_tests.__warningregistry__ |
| 48 | if hasattr(sys, '__warningregistry__'): |
| 49 | del sys.__warningregistry__ |
| 50 | # The 'warnings' module must be explicitly set so that the proper |
| 51 | # interaction between _warnings and 'warnings' can be controlled. |
| 52 | sys.modules['warnings'] = self.module |
| 53 | super(BaseTest, self).setUp() |
| 54 | |
| 55 | def tearDown(self): |
| 56 | sys.modules['warnings'] = original_warnings |
| 57 | super(BaseTest, self).tearDown() |
| 58 | |
| 59 | |
| 60 | class FilterTests(object): |
| 61 | |
| 62 | """Testing the filtering functionality.""" |
| 63 | |
| 64 | def test_error(self): |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 65 | with original_warnings.catch_warnings(module=self.module) as w: |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 66 | self.module.resetwarnings() |
| 67 | self.module.filterwarnings("error", category=UserWarning) |
| 68 | self.assertRaises(UserWarning, self.module.warn, |
| 69 | "FilterTests.test_error") |
| 70 | |
| 71 | def test_ignore(self): |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 72 | with original_warnings.catch_warnings(record=True, |
| 73 | module=self.module) as w: |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 74 | self.module.resetwarnings() |
| 75 | self.module.filterwarnings("ignore", category=UserWarning) |
| 76 | self.module.warn("FilterTests.test_ignore", UserWarning) |
Brett Cannon | ec92e18 | 2008-09-02 02:46:59 +0000 | [diff] [blame] | 77 | self.assertEquals(len(w), 0) |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 78 | |
| 79 | def test_always(self): |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 80 | with original_warnings.catch_warnings(record=True, |
| 81 | module=self.module) as w: |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 82 | self.module.resetwarnings() |
| 83 | self.module.filterwarnings("always", category=UserWarning) |
| 84 | message = "FilterTests.test_always" |
| 85 | self.module.warn(message, UserWarning) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 86 | self.assertTrue(message, w[-1].message) |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 87 | self.module.warn(message, UserWarning) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 88 | self.assertTrue(w[-1].message, message) |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 89 | |
| 90 | def test_default(self): |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 91 | with original_warnings.catch_warnings(record=True, |
| 92 | module=self.module) as w: |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 93 | self.module.resetwarnings() |
| 94 | self.module.filterwarnings("default", category=UserWarning) |
| 95 | message = UserWarning("FilterTests.test_default") |
| 96 | for x in range(2): |
| 97 | self.module.warn(message, UserWarning) |
| 98 | if x == 0: |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 99 | self.assertEquals(w[-1].message, message) |
| 100 | del w[:] |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 101 | elif x == 1: |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 102 | self.assertEquals(len(w), 0) |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 103 | else: |
| 104 | raise ValueError("loop variant unhandled") |
| 105 | |
| 106 | def test_module(self): |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 107 | with original_warnings.catch_warnings(record=True, |
| 108 | module=self.module) as w: |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 109 | self.module.resetwarnings() |
| 110 | self.module.filterwarnings("module", category=UserWarning) |
| 111 | message = UserWarning("FilterTests.test_module") |
| 112 | self.module.warn(message, UserWarning) |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 113 | self.assertEquals(w[-1].message, message) |
| 114 | del w[:] |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 115 | self.module.warn(message, UserWarning) |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 116 | self.assertEquals(len(w), 0) |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 117 | |
| 118 | def test_once(self): |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 119 | with original_warnings.catch_warnings(record=True, |
| 120 | module=self.module) as w: |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 121 | self.module.resetwarnings() |
| 122 | self.module.filterwarnings("once", category=UserWarning) |
| 123 | message = UserWarning("FilterTests.test_once") |
| 124 | self.module.warn_explicit(message, UserWarning, "test_warnings.py", |
| 125 | 42) |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 126 | self.assertEquals(w[-1].message, message) |
| 127 | del w[:] |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 128 | self.module.warn_explicit(message, UserWarning, "test_warnings.py", |
| 129 | 13) |
Brett Cannon | ec92e18 | 2008-09-02 02:46:59 +0000 | [diff] [blame] | 130 | self.assertEquals(len(w), 0) |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 131 | self.module.warn_explicit(message, UserWarning, "test_warnings2.py", |
| 132 | 42) |
Brett Cannon | ec92e18 | 2008-09-02 02:46:59 +0000 | [diff] [blame] | 133 | self.assertEquals(len(w), 0) |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 134 | |
| 135 | def test_inheritance(self): |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 136 | with original_warnings.catch_warnings(module=self.module) as w: |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 137 | self.module.resetwarnings() |
| 138 | self.module.filterwarnings("error", category=Warning) |
| 139 | self.assertRaises(UserWarning, self.module.warn, |
| 140 | "FilterTests.test_inheritance", UserWarning) |
| 141 | |
| 142 | def test_ordering(self): |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 143 | with original_warnings.catch_warnings(record=True, |
| 144 | module=self.module) as w: |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 145 | self.module.resetwarnings() |
| 146 | self.module.filterwarnings("ignore", category=UserWarning) |
| 147 | self.module.filterwarnings("error", category=UserWarning, |
| 148 | append=True) |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 149 | del w[:] |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 150 | try: |
| 151 | self.module.warn("FilterTests.test_ordering", UserWarning) |
| 152 | except UserWarning: |
| 153 | self.fail("order handling for actions failed") |
Brett Cannon | ec92e18 | 2008-09-02 02:46:59 +0000 | [diff] [blame] | 154 | self.assertEquals(len(w), 0) |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 155 | |
| 156 | def test_filterwarnings(self): |
| 157 | # Test filterwarnings(). |
| 158 | # Implicitly also tests resetwarnings(). |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 159 | with original_warnings.catch_warnings(record=True, |
| 160 | module=self.module) as w: |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 161 | self.module.filterwarnings("error", "", Warning, "", 0) |
| 162 | self.assertRaises(UserWarning, self.module.warn, 'convert to error') |
| 163 | |
| 164 | self.module.resetwarnings() |
| 165 | text = 'handle normally' |
| 166 | self.module.warn(text) |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 167 | self.assertEqual(str(w[-1].message), text) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 168 | self.assertTrue(w[-1].category is UserWarning) |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 169 | |
| 170 | self.module.filterwarnings("ignore", "", Warning, "", 0) |
| 171 | text = 'filtered out' |
| 172 | self.module.warn(text) |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 173 | self.assertNotEqual(str(w[-1].message), text) |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 174 | |
| 175 | self.module.resetwarnings() |
| 176 | self.module.filterwarnings("error", "hex*", Warning, "", 0) |
| 177 | self.assertRaises(UserWarning, self.module.warn, 'hex/oct') |
| 178 | text = 'nonmatching text' |
| 179 | self.module.warn(text) |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 180 | self.assertEqual(str(w[-1].message), text) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 181 | self.assertTrue(w[-1].category is UserWarning) |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 182 | |
| 183 | class CFilterTests(BaseTest, FilterTests): |
| 184 | module = c_warnings |
| 185 | |
| 186 | class PyFilterTests(BaseTest, FilterTests): |
| 187 | module = py_warnings |
| 188 | |
| 189 | |
| 190 | class WarnTests(unittest.TestCase): |
| 191 | |
| 192 | """Test warnings.warn() and warnings.warn_explicit().""" |
| 193 | |
| 194 | def test_message(self): |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 195 | with original_warnings.catch_warnings(record=True, |
| 196 | module=self.module) as w: |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 197 | for i in range(4): |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 198 | text = 'multi %d' %i # Different text on each call. |
| 199 | self.module.warn(text) |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 200 | self.assertEqual(str(w[-1].message), text) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 201 | self.assertTrue(w[-1].category is UserWarning) |
Raymond Hettinger | d6f6e50 | 2003-07-13 08:37:40 +0000 | [diff] [blame] | 202 | |
Brett Cannon | 54bd41d | 2008-09-02 04:01:42 +0000 | [diff] [blame] | 203 | # Issue 3639 |
| 204 | def test_warn_nonstandard_types(self): |
| 205 | # warn() should handle non-standard types without issue. |
| 206 | for ob in (Warning, None, 42): |
Benjamin Peterson | fcf5d63 | 2008-10-16 23:24:44 +0000 | [diff] [blame] | 207 | with original_warnings.catch_warnings(record=True, |
| 208 | module=self.module) as w: |
Brett Cannon | 54bd41d | 2008-09-02 04:01:42 +0000 | [diff] [blame] | 209 | self.module.warn(ob) |
| 210 | # Don't directly compare objects since |
| 211 | # ``Warning() != Warning()``. |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 212 | self.assertEquals(str(w[-1].message), str(UserWarning(ob))) |
Brett Cannon | 54bd41d | 2008-09-02 04:01:42 +0000 | [diff] [blame] | 213 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 214 | def test_filename(self): |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 215 | with warnings_state(self.module): |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 216 | with original_warnings.catch_warnings(record=True, |
| 217 | module=self.module) as w: |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 218 | warning_tests.inner("spam1") |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 219 | self.assertEqual(os.path.basename(w[-1].filename), |
| 220 | "warning_tests.py") |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 221 | warning_tests.outer("spam2") |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 222 | self.assertEqual(os.path.basename(w[-1].filename), |
| 223 | "warning_tests.py") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 224 | |
| 225 | def test_stacklevel(self): |
| 226 | # Test stacklevel argument |
| 227 | # make sure all messages are different, so the warning won't be skipped |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 228 | with warnings_state(self.module): |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 229 | with original_warnings.catch_warnings(record=True, |
| 230 | module=self.module) as w: |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 231 | warning_tests.inner("spam3", stacklevel=1) |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 232 | self.assertEqual(os.path.basename(w[-1].filename), |
| 233 | "warning_tests.py") |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 234 | warning_tests.outer("spam4", stacklevel=1) |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 235 | self.assertEqual(os.path.basename(w[-1].filename), |
| 236 | "warning_tests.py") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 237 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 238 | warning_tests.inner("spam5", stacklevel=2) |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 239 | self.assertEqual(os.path.basename(w[-1].filename), |
| 240 | "test_warnings.py") |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 241 | warning_tests.outer("spam6", stacklevel=2) |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 242 | self.assertEqual(os.path.basename(w[-1].filename), |
| 243 | "warning_tests.py") |
Christian Heimes | 5d8da20 | 2008-05-06 13:58:24 +0000 | [diff] [blame] | 244 | warning_tests.outer("spam6.5", stacklevel=3) |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 245 | self.assertEqual(os.path.basename(w[-1].filename), |
| 246 | "test_warnings.py") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 247 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 248 | warning_tests.inner("spam7", stacklevel=9999) |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 249 | self.assertEqual(os.path.basename(w[-1].filename), |
| 250 | "sys") |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 251 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 252 | def test_missing_filename_not_main(self): |
| 253 | # If __file__ is not specified and __main__ is not the module name, |
| 254 | # then __file__ should be set to the module name. |
| 255 | filename = warning_tests.__file__ |
| 256 | try: |
| 257 | del warning_tests.__file__ |
| 258 | with warnings_state(self.module): |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 259 | with original_warnings.catch_warnings(record=True, |
| 260 | module=self.module) as w: |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 261 | warning_tests.inner("spam8", stacklevel=1) |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 262 | self.assertEqual(w[-1].filename, warning_tests.__name__) |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 263 | finally: |
| 264 | warning_tests.__file__ = filename |
| 265 | |
| 266 | def test_missing_filename_main_with_argv(self): |
| 267 | # If __file__ is not specified and the caller is __main__ and sys.argv |
| 268 | # exists, then use sys.argv[0] as the file. |
| 269 | if not hasattr(sys, 'argv'): |
| 270 | return |
| 271 | filename = warning_tests.__file__ |
| 272 | module_name = warning_tests.__name__ |
| 273 | try: |
| 274 | del warning_tests.__file__ |
| 275 | warning_tests.__name__ = '__main__' |
| 276 | with warnings_state(self.module): |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 277 | with original_warnings.catch_warnings(record=True, |
| 278 | module=self.module) as w: |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 279 | warning_tests.inner('spam9', stacklevel=1) |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 280 | self.assertEqual(w[-1].filename, sys.argv[0]) |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 281 | finally: |
| 282 | warning_tests.__file__ = filename |
| 283 | warning_tests.__name__ = module_name |
| 284 | |
| 285 | def test_missing_filename_main_without_argv(self): |
| 286 | # If __file__ is not specified, the caller is __main__, and sys.argv |
| 287 | # is not set, then '__main__' is the file name. |
| 288 | filename = warning_tests.__file__ |
| 289 | module_name = warning_tests.__name__ |
| 290 | argv = sys.argv |
| 291 | try: |
| 292 | del warning_tests.__file__ |
| 293 | warning_tests.__name__ = '__main__' |
| 294 | del sys.argv |
| 295 | with warnings_state(self.module): |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 296 | with original_warnings.catch_warnings(record=True, |
| 297 | module=self.module) as w: |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 298 | warning_tests.inner('spam10', stacklevel=1) |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 299 | self.assertEqual(w[-1].filename, '__main__') |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 300 | finally: |
| 301 | warning_tests.__file__ = filename |
| 302 | warning_tests.__name__ = module_name |
| 303 | sys.argv = argv |
| 304 | |
Christian Heimes | daaf8ee | 2008-05-04 23:58:41 +0000 | [diff] [blame] | 305 | def test_missing_filename_main_with_argv_empty_string(self): |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 306 | # If __file__ is not specified, the caller is __main__, and sys.argv[0] |
| 307 | # is the empty string, then '__main__ is the file name. |
| 308 | # Tests issue 2743. |
| 309 | file_name = warning_tests.__file__ |
| 310 | module_name = warning_tests.__name__ |
| 311 | argv = sys.argv |
| 312 | try: |
| 313 | del warning_tests.__file__ |
| 314 | warning_tests.__name__ = '__main__' |
| 315 | sys.argv = [''] |
| 316 | with warnings_state(self.module): |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 317 | with original_warnings.catch_warnings(record=True, |
| 318 | module=self.module) as w: |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 319 | warning_tests.inner('spam11', stacklevel=1) |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 320 | self.assertEqual(w[-1].filename, '__main__') |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 321 | finally: |
| 322 | warning_tests.__file__ = file_name |
| 323 | warning_tests.__name__ = module_name |
| 324 | sys.argv = argv |
| 325 | |
Brett Cannon | db73491 | 2008-06-27 00:52:15 +0000 | [diff] [blame] | 326 | def test_warn_explicit_type_errors(self): |
| 327 | # warn_explicit() shoud error out gracefully if it is given objects |
| 328 | # of the wrong types. |
| 329 | # lineno is expected to be an integer. |
| 330 | self.assertRaises(TypeError, self.module.warn_explicit, |
| 331 | None, UserWarning, None, None) |
| 332 | # Either 'message' needs to be an instance of Warning or 'category' |
| 333 | # needs to be a subclass. |
| 334 | self.assertRaises(TypeError, self.module.warn_explicit, |
| 335 | None, None, None, 1) |
| 336 | # 'registry' must be a dict or None. |
| 337 | self.assertRaises((TypeError, AttributeError), |
| 338 | self.module.warn_explicit, |
| 339 | None, Warning, None, 1, registry=42) |
| 340 | |
Hirokazu Yamamoto | 1c0c003 | 2009-07-17 06:55:42 +0000 | [diff] [blame] | 341 | def test_bad_str(self): |
| 342 | # issue 6415 |
| 343 | # Warnings instance with a bad format string for __str__ should not |
| 344 | # trigger a bus error. |
| 345 | class BadStrWarning(Warning): |
| 346 | """Warning with a bad format string for __str__.""" |
| 347 | def __str__(self): |
| 348 | return ("A bad formatted string %(err)" % |
| 349 | {"err" : "there is no %(err)s"}) |
| 350 | |
| 351 | with self.assertRaises(ValueError): |
| 352 | self.module.warn(BadStrWarning()) |
| 353 | |
| 354 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 355 | class CWarnTests(BaseTest, WarnTests): |
| 356 | module = c_warnings |
| 357 | |
Nick Coghlan | fce769e | 2009-04-11 14:30:59 +0000 | [diff] [blame] | 358 | # As an early adopter, we sanity check the |
| 359 | # test.support.import_fresh_module utility function |
| 360 | def test_accelerated(self): |
| 361 | self.assertFalse(original_warnings is self.module) |
| 362 | self.assertFalse(hasattr(self.module.warn, '__code__')) |
| 363 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 364 | class PyWarnTests(BaseTest, WarnTests): |
| 365 | module = py_warnings |
| 366 | |
Nick Coghlan | fce769e | 2009-04-11 14:30:59 +0000 | [diff] [blame] | 367 | # As an early adopter, we sanity check the |
| 368 | # test.support.import_fresh_module utility function |
| 369 | def test_pure_python(self): |
| 370 | self.assertFalse(original_warnings is self.module) |
| 371 | self.assertTrue(hasattr(self.module.warn, '__code__')) |
| 372 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 373 | |
| 374 | class WCmdLineTests(unittest.TestCase): |
| 375 | |
| 376 | def test_improper_input(self): |
| 377 | # Uses the private _setoption() function to test the parsing |
| 378 | # of command-line warning arguments |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 379 | with original_warnings.catch_warnings(module=self.module): |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 380 | self.assertRaises(self.module._OptionError, |
| 381 | self.module._setoption, '1:2:3:4:5:6') |
| 382 | self.assertRaises(self.module._OptionError, |
| 383 | self.module._setoption, 'bogus::Warning') |
| 384 | self.assertRaises(self.module._OptionError, |
| 385 | self.module._setoption, 'ignore:2::4:-5') |
| 386 | self.module._setoption('error::Warning::0') |
| 387 | self.assertRaises(UserWarning, self.module.warn, 'convert to error') |
| 388 | |
| 389 | class CWCmdLineTests(BaseTest, WCmdLineTests): |
| 390 | module = c_warnings |
| 391 | |
| 392 | class PyWCmdLineTests(BaseTest, WCmdLineTests): |
| 393 | module = py_warnings |
| 394 | |
| 395 | |
| 396 | class _WarningsTests(BaseTest): |
| 397 | |
| 398 | """Tests specific to the _warnings module.""" |
| 399 | |
| 400 | module = c_warnings |
| 401 | |
| 402 | def test_filter(self): |
| 403 | # Everything should function even if 'filters' is not in warnings. |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 404 | with original_warnings.catch_warnings(module=self.module) as w: |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 405 | self.module.filterwarnings("error", "", Warning, "", 0) |
| 406 | self.assertRaises(UserWarning, self.module.warn, |
| 407 | 'convert to error') |
| 408 | del self.module.filters |
| 409 | self.assertRaises(UserWarning, self.module.warn, |
| 410 | 'convert to error') |
| 411 | |
| 412 | def test_onceregistry(self): |
| 413 | # Replacing or removing the onceregistry should be okay. |
| 414 | global __warningregistry__ |
| 415 | message = UserWarning('onceregistry test') |
| 416 | try: |
| 417 | original_registry = self.module.onceregistry |
| 418 | __warningregistry__ = {} |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 419 | with original_warnings.catch_warnings(record=True, |
| 420 | module=self.module) as w: |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 421 | self.module.resetwarnings() |
| 422 | self.module.filterwarnings("once", category=UserWarning) |
| 423 | self.module.warn_explicit(message, UserWarning, "file", 42) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 424 | self.assertEqual(w[-1].message, message) |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 425 | del w[:] |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 426 | self.module.warn_explicit(message, UserWarning, "file", 42) |
Brett Cannon | ec92e18 | 2008-09-02 02:46:59 +0000 | [diff] [blame] | 427 | self.assertEquals(len(w), 0) |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 428 | # Test the resetting of onceregistry. |
| 429 | self.module.onceregistry = {} |
| 430 | __warningregistry__ = {} |
| 431 | self.module.warn('onceregistry test') |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 432 | self.assertEqual(w[-1].message.args, message.args) |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 433 | # Removal of onceregistry is okay. |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 434 | del w[:] |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 435 | del self.module.onceregistry |
| 436 | __warningregistry__ = {} |
| 437 | self.module.warn_explicit(message, UserWarning, "file", 42) |
Brett Cannon | ec92e18 | 2008-09-02 02:46:59 +0000 | [diff] [blame] | 438 | self.assertEquals(len(w), 0) |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 439 | finally: |
| 440 | self.module.onceregistry = original_registry |
| 441 | |
Brett Cannon | 0759dd6 | 2009-04-01 18:13:07 +0000 | [diff] [blame] | 442 | def test_default_action(self): |
| 443 | # Replacing or removing defaultaction should be okay. |
| 444 | message = UserWarning("defaultaction test") |
| 445 | original = self.module.defaultaction |
| 446 | try: |
| 447 | with original_warnings.catch_warnings(record=True, |
| 448 | module=self.module) as w: |
| 449 | self.module.resetwarnings() |
| 450 | registry = {} |
| 451 | self.module.warn_explicit(message, UserWarning, "<test>", 42, |
| 452 | registry=registry) |
| 453 | self.assertEqual(w[-1].message, message) |
| 454 | self.assertEqual(len(w), 1) |
| 455 | self.assertEqual(len(registry), 1) |
| 456 | del w[:] |
| 457 | # Test removal. |
| 458 | del self.module.defaultaction |
| 459 | __warningregistry__ = {} |
| 460 | registry = {} |
| 461 | self.module.warn_explicit(message, UserWarning, "<test>", 43, |
| 462 | registry=registry) |
| 463 | self.assertEqual(w[-1].message, message) |
| 464 | self.assertEqual(len(w), 1) |
| 465 | self.assertEqual(len(registry), 1) |
| 466 | del w[:] |
| 467 | # Test setting. |
| 468 | self.module.defaultaction = "ignore" |
| 469 | __warningregistry__ = {} |
| 470 | registry = {} |
| 471 | self.module.warn_explicit(message, UserWarning, "<test>", 44, |
| 472 | registry=registry) |
| 473 | self.assertEqual(len(w), 0) |
| 474 | finally: |
| 475 | self.module.defaultaction = original |
| 476 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 477 | def test_showwarning_missing(self): |
| 478 | # Test that showwarning() missing is okay. |
| 479 | text = 'del showwarning test' |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 480 | with original_warnings.catch_warnings(module=self.module): |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 481 | self.module.filterwarnings("always", category=UserWarning) |
| 482 | del self.module.showwarning |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 483 | with support.captured_output('stderr') as stream: |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 484 | self.module.warn(text) |
| 485 | result = stream.getvalue() |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 486 | self.assertIn(text, result) |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 487 | |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 488 | def test_showwarning_not_callable(self): |
Brett Cannon | fcc0527 | 2009-04-01 20:27:29 +0000 | [diff] [blame] | 489 | with original_warnings.catch_warnings(module=self.module): |
| 490 | self.module.filterwarnings("always", category=UserWarning) |
| 491 | old_showwarning = self.module.showwarning |
| 492 | self.module.showwarning = 23 |
| 493 | try: |
| 494 | self.assertRaises(TypeError, self.module.warn, "Warning!") |
| 495 | finally: |
| 496 | self.module.showwarning = old_showwarning |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 497 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 498 | def test_show_warning_output(self): |
| 499 | # With showarning() missing, make sure that output is okay. |
| 500 | text = 'test show_warning' |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 501 | with original_warnings.catch_warnings(module=self.module): |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 502 | self.module.filterwarnings("always", category=UserWarning) |
| 503 | del self.module.showwarning |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 504 | with support.captured_output('stderr') as stream: |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 505 | warning_tests.inner(text) |
| 506 | result = stream.getvalue() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 507 | self.assertEqual(result.count('\n'), 2, |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 508 | "Too many newlines in %r" % result) |
| 509 | first_line, second_line = result.split('\n', 1) |
| 510 | expected_file = os.path.splitext(warning_tests.__file__)[0] + '.py' |
Neal Norwitz | 32dde22 | 2008-04-15 06:43:13 +0000 | [diff] [blame] | 511 | first_line_parts = first_line.rsplit(':', 3) |
| 512 | path, line, warning_class, message = first_line_parts |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 513 | line = int(line) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 514 | self.assertEqual(expected_file, path) |
| 515 | self.assertEqual(warning_class, ' ' + UserWarning.__name__) |
| 516 | self.assertEqual(message, ' ' + text) |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 517 | expected_line = ' ' + linecache.getline(path, line).strip() + '\n' |
| 518 | assert expected_line |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 519 | self.assertEqual(second_line, expected_line) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 520 | |
| 521 | |
Christian Heimes | 5fb7c2a | 2007-12-24 08:52:31 +0000 | [diff] [blame] | 522 | class WarningsDisplayTests(unittest.TestCase): |
| 523 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 524 | """Test the displaying of warnings and the ability to overload functions |
| 525 | related to displaying warnings.""" |
| 526 | |
Christian Heimes | 5fb7c2a | 2007-12-24 08:52:31 +0000 | [diff] [blame] | 527 | def test_formatwarning(self): |
| 528 | message = "msg" |
| 529 | category = Warning |
| 530 | file_name = os.path.splitext(warning_tests.__file__)[0] + '.py' |
| 531 | line_num = 3 |
| 532 | file_line = linecache.getline(file_name, line_num).strip() |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 533 | format = "%s:%s: %s: %s\n %s\n" |
| 534 | expect = format % (file_name, line_num, category.__name__, message, |
| 535 | file_line) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 536 | self.assertEqual(expect, self.module.formatwarning(message, |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 537 | category, file_name, line_num)) |
| 538 | # Test the 'line' argument. |
| 539 | file_line += " for the win!" |
| 540 | expect = format % (file_name, line_num, category.__name__, message, |
| 541 | file_line) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 542 | self.assertEqual(expect, self.module.formatwarning(message, |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 543 | category, file_name, line_num, file_line)) |
Christian Heimes | 5fb7c2a | 2007-12-24 08:52:31 +0000 | [diff] [blame] | 544 | |
| 545 | def test_showwarning(self): |
| 546 | file_name = os.path.splitext(warning_tests.__file__)[0] + '.py' |
| 547 | line_num = 3 |
| 548 | expected_file_line = linecache.getline(file_name, line_num).strip() |
| 549 | message = 'msg' |
| 550 | category = Warning |
| 551 | file_object = StringIO() |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 552 | expect = self.module.formatwarning(message, category, file_name, |
| 553 | line_num) |
| 554 | self.module.showwarning(message, category, file_name, line_num, |
Christian Heimes | 5fb7c2a | 2007-12-24 08:52:31 +0000 | [diff] [blame] | 555 | file_object) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 556 | self.assertEqual(file_object.getvalue(), expect) |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 557 | # Test 'line' argument. |
| 558 | expected_file_line += "for the win!" |
| 559 | expect = self.module.formatwarning(message, category, file_name, |
| 560 | line_num, expected_file_line) |
| 561 | file_object = StringIO() |
| 562 | self.module.showwarning(message, category, file_name, line_num, |
| 563 | file_object, expected_file_line) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 564 | self.assertEqual(expect, file_object.getvalue()) |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 565 | |
| 566 | class CWarningsDisplayTests(BaseTest, WarningsDisplayTests): |
| 567 | module = c_warnings |
| 568 | |
| 569 | class PyWarningsDisplayTests(BaseTest, WarningsDisplayTests): |
| 570 | module = py_warnings |
Christian Heimes | 5fb7c2a | 2007-12-24 08:52:31 +0000 | [diff] [blame] | 571 | |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 572 | |
Brett Cannon | ec92e18 | 2008-09-02 02:46:59 +0000 | [diff] [blame] | 573 | class CatchWarningTests(BaseTest): |
Nick Coghlan | b130493 | 2008-07-13 12:25:08 +0000 | [diff] [blame] | 574 | |
Brett Cannon | ec92e18 | 2008-09-02 02:46:59 +0000 | [diff] [blame] | 575 | """Test catch_warnings().""" |
| 576 | |
| 577 | def test_catch_warnings_restore(self): |
Nick Coghlan | b130493 | 2008-07-13 12:25:08 +0000 | [diff] [blame] | 578 | wmod = self.module |
| 579 | orig_filters = wmod.filters |
| 580 | orig_showwarning = wmod.showwarning |
Benjamin Peterson | fcf5d63 | 2008-10-16 23:24:44 +0000 | [diff] [blame] | 581 | # Ensure both showwarning and filters are restored when recording |
| 582 | with wmod.catch_warnings(module=wmod, record=True): |
Nick Coghlan | b130493 | 2008-07-13 12:25:08 +0000 | [diff] [blame] | 583 | wmod.filters = wmod.showwarning = object() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 584 | self.assertTrue(wmod.filters is orig_filters) |
| 585 | self.assertTrue(wmod.showwarning is orig_showwarning) |
Benjamin Peterson | fcf5d63 | 2008-10-16 23:24:44 +0000 | [diff] [blame] | 586 | # Same test, but with recording disabled |
| 587 | with wmod.catch_warnings(module=wmod, record=False): |
Nick Coghlan | b130493 | 2008-07-13 12:25:08 +0000 | [diff] [blame] | 588 | wmod.filters = wmod.showwarning = object() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 589 | self.assertTrue(wmod.filters is orig_filters) |
| 590 | self.assertTrue(wmod.showwarning is orig_showwarning) |
Nick Coghlan | b130493 | 2008-07-13 12:25:08 +0000 | [diff] [blame] | 591 | |
Brett Cannon | ec92e18 | 2008-09-02 02:46:59 +0000 | [diff] [blame] | 592 | def test_catch_warnings_recording(self): |
Nick Coghlan | b130493 | 2008-07-13 12:25:08 +0000 | [diff] [blame] | 593 | wmod = self.module |
Benjamin Peterson | fcf5d63 | 2008-10-16 23:24:44 +0000 | [diff] [blame] | 594 | # Ensure warnings are recorded when requested |
| 595 | with wmod.catch_warnings(module=wmod, record=True) as w: |
Brett Cannon | ec92e18 | 2008-09-02 02:46:59 +0000 | [diff] [blame] | 596 | self.assertEqual(w, []) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 597 | self.assertTrue(type(w) is list) |
Nick Coghlan | b130493 | 2008-07-13 12:25:08 +0000 | [diff] [blame] | 598 | wmod.simplefilter("always") |
| 599 | wmod.warn("foo") |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 600 | self.assertEqual(str(w[-1].message), "foo") |
Nick Coghlan | b130493 | 2008-07-13 12:25:08 +0000 | [diff] [blame] | 601 | wmod.warn("bar") |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 602 | self.assertEqual(str(w[-1].message), "bar") |
Brett Cannon | ec92e18 | 2008-09-02 02:46:59 +0000 | [diff] [blame] | 603 | self.assertEqual(str(w[0].message), "foo") |
| 604 | self.assertEqual(str(w[1].message), "bar") |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 605 | del w[:] |
Brett Cannon | ec92e18 | 2008-09-02 02:46:59 +0000 | [diff] [blame] | 606 | self.assertEqual(w, []) |
Benjamin Peterson | fcf5d63 | 2008-10-16 23:24:44 +0000 | [diff] [blame] | 607 | # Ensure warnings are not recorded when not requested |
Nick Coghlan | b130493 | 2008-07-13 12:25:08 +0000 | [diff] [blame] | 608 | orig_showwarning = wmod.showwarning |
Benjamin Peterson | fcf5d63 | 2008-10-16 23:24:44 +0000 | [diff] [blame] | 609 | with wmod.catch_warnings(module=wmod, record=False) as w: |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 610 | self.assertTrue(w is None) |
| 611 | self.assertTrue(wmod.showwarning is orig_showwarning) |
Nick Coghlan | b130493 | 2008-07-13 12:25:08 +0000 | [diff] [blame] | 612 | |
Benjamin Peterson | fcf5d63 | 2008-10-16 23:24:44 +0000 | [diff] [blame] | 613 | def test_catch_warnings_reentry_guard(self): |
| 614 | wmod = self.module |
| 615 | # Ensure catch_warnings is protected against incorrect usage |
| 616 | x = wmod.catch_warnings(module=wmod, record=True) |
| 617 | self.assertRaises(RuntimeError, x.__exit__) |
| 618 | with x: |
| 619 | self.assertRaises(RuntimeError, x.__enter__) |
| 620 | # Same test, but with recording disabled |
| 621 | x = wmod.catch_warnings(module=wmod, record=False) |
| 622 | self.assertRaises(RuntimeError, x.__exit__) |
| 623 | with x: |
| 624 | self.assertRaises(RuntimeError, x.__enter__) |
| 625 | |
| 626 | def test_catch_warnings_defaults(self): |
| 627 | wmod = self.module |
| 628 | orig_filters = wmod.filters |
| 629 | orig_showwarning = wmod.showwarning |
| 630 | # Ensure default behaviour is not to record warnings |
| 631 | with wmod.catch_warnings(module=wmod) as w: |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 632 | self.assertTrue(w is None) |
| 633 | self.assertTrue(wmod.showwarning is orig_showwarning) |
| 634 | self.assertTrue(wmod.filters is not orig_filters) |
| 635 | self.assertTrue(wmod.filters is orig_filters) |
Benjamin Peterson | fcf5d63 | 2008-10-16 23:24:44 +0000 | [diff] [blame] | 636 | if wmod is sys.modules['warnings']: |
| 637 | # Ensure the default module is this one |
| 638 | with wmod.catch_warnings() as w: |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 639 | self.assertTrue(w is None) |
| 640 | self.assertTrue(wmod.showwarning is orig_showwarning) |
| 641 | self.assertTrue(wmod.filters is not orig_filters) |
| 642 | self.assertTrue(wmod.filters is orig_filters) |
Benjamin Peterson | fcf5d63 | 2008-10-16 23:24:44 +0000 | [diff] [blame] | 643 | |
| 644 | def test_check_warnings(self): |
| 645 | # Explicit tests for the test.support convenience wrapper |
| 646 | wmod = self.module |
Florent Xicluna | 53b506be | 2010-03-18 20:00:57 +0000 | [diff] [blame] | 647 | if wmod is not sys.modules['warnings']: |
| 648 | return |
| 649 | with support.check_warnings(quiet=False) as w: |
| 650 | self.assertEqual(w.warnings, []) |
| 651 | wmod.simplefilter("always") |
| 652 | wmod.warn("foo") |
| 653 | self.assertEqual(str(w.message), "foo") |
| 654 | wmod.warn("bar") |
| 655 | self.assertEqual(str(w.message), "bar") |
| 656 | self.assertEqual(str(w.warnings[0].message), "foo") |
| 657 | self.assertEqual(str(w.warnings[1].message), "bar") |
| 658 | w.reset() |
| 659 | self.assertEqual(w.warnings, []) |
| 660 | |
| 661 | with support.check_warnings(): |
| 662 | # defaults to quiet=True without argument |
| 663 | pass |
| 664 | with support.check_warnings(('foo', UserWarning)): |
| 665 | wmod.warn("foo") |
| 666 | |
| 667 | with self.assertRaises(AssertionError): |
| 668 | with support.check_warnings(('', RuntimeWarning)): |
| 669 | # defaults to quiet=False with argument |
| 670 | pass |
| 671 | with self.assertRaises(AssertionError): |
| 672 | with support.check_warnings(('foo', RuntimeWarning)): |
Benjamin Peterson | fcf5d63 | 2008-10-16 23:24:44 +0000 | [diff] [blame] | 673 | wmod.warn("foo") |
Benjamin Peterson | fcf5d63 | 2008-10-16 23:24:44 +0000 | [diff] [blame] | 674 | |
Brett Cannon | ec92e18 | 2008-09-02 02:46:59 +0000 | [diff] [blame] | 675 | class CCatchWarningTests(CatchWarningTests): |
Nick Coghlan | b130493 | 2008-07-13 12:25:08 +0000 | [diff] [blame] | 676 | module = c_warnings |
| 677 | |
Brett Cannon | ec92e18 | 2008-09-02 02:46:59 +0000 | [diff] [blame] | 678 | class PyCatchWarningTests(CatchWarningTests): |
Nick Coghlan | b130493 | 2008-07-13 12:25:08 +0000 | [diff] [blame] | 679 | module = py_warnings |
| 680 | |
Christian Heimes | 5fb7c2a | 2007-12-24 08:52:31 +0000 | [diff] [blame] | 681 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 682 | def test_main(): |
Christian Heimes | dae2a89 | 2008-04-19 00:55:37 +0000 | [diff] [blame] | 683 | py_warnings.onceregistry.clear() |
| 684 | c_warnings.onceregistry.clear() |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 685 | support.run_unittest(CFilterTests, |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 686 | PyFilterTests, |
| 687 | CWarnTests, |
| 688 | PyWarnTests, |
| 689 | CWCmdLineTests, PyWCmdLineTests, |
| 690 | _WarningsTests, |
| 691 | CWarningsDisplayTests, PyWarningsDisplayTests, |
Brett Cannon | ec92e18 | 2008-09-02 02:46:59 +0000 | [diff] [blame] | 692 | CCatchWarningTests, PyCatchWarningTests, |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 693 | ) |
| 694 | |
Raymond Hettinger | d6f6e50 | 2003-07-13 08:37:40 +0000 | [diff] [blame] | 695 | |
| 696 | if __name__ == "__main__": |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 697 | test_main() |