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