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 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 13 | sys.modules['_warnings'] = 0 |
| 14 | del sys.modules['warnings'] |
| 15 | |
| 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 | |
| 23 | sys.modules['warnings'] = original_warnings |
| 24 | |
| 25 | |
| 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 |
| 40 | try: |
| 41 | warning_tests.warnings = module |
| 42 | yield |
| 43 | finally: |
| 44 | warning_tests.warnings = original_warnings |
| 45 | |
| 46 | |
| 47 | class BaseTest(unittest.TestCase): |
| 48 | |
| 49 | """Basic bookkeeping required for testing.""" |
| 50 | |
| 51 | def setUp(self): |
| 52 | # The __warningregistry__ needs to be in a pristine state for tests |
| 53 | # to work properly. |
| 54 | if '__warningregistry__' in globals(): |
| 55 | del globals()['__warningregistry__'] |
| 56 | if hasattr(warning_tests, '__warningregistry__'): |
| 57 | del warning_tests.__warningregistry__ |
| 58 | if hasattr(sys, '__warningregistry__'): |
| 59 | del sys.__warningregistry__ |
| 60 | # The 'warnings' module must be explicitly set so that the proper |
| 61 | # interaction between _warnings and 'warnings' can be controlled. |
| 62 | sys.modules['warnings'] = self.module |
| 63 | super(BaseTest, self).setUp() |
| 64 | |
| 65 | def tearDown(self): |
| 66 | sys.modules['warnings'] = original_warnings |
| 67 | super(BaseTest, self).tearDown() |
| 68 | |
| 69 | |
| 70 | class FilterTests(object): |
| 71 | |
| 72 | """Testing the filtering functionality.""" |
| 73 | |
| 74 | def test_error(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 75 | with support.catch_warning(self.module) as w: |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 76 | self.module.resetwarnings() |
| 77 | self.module.filterwarnings("error", category=UserWarning) |
| 78 | self.assertRaises(UserWarning, self.module.warn, |
| 79 | "FilterTests.test_error") |
| 80 | |
| 81 | def test_ignore(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 82 | with support.catch_warning(self.module) as w: |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 83 | self.module.resetwarnings() |
| 84 | self.module.filterwarnings("ignore", category=UserWarning) |
| 85 | self.module.warn("FilterTests.test_ignore", UserWarning) |
| 86 | self.assert_(not w.message) |
| 87 | |
| 88 | def test_always(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 89 | with support.catch_warning(self.module) as w: |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 90 | self.module.resetwarnings() |
| 91 | self.module.filterwarnings("always", category=UserWarning) |
| 92 | message = "FilterTests.test_always" |
| 93 | self.module.warn(message, UserWarning) |
| 94 | self.assert_(message, w.message) |
| 95 | w.message = None # Reset. |
| 96 | self.module.warn(message, UserWarning) |
| 97 | self.assert_(w.message, message) |
| 98 | |
| 99 | def test_default(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 100 | with support.catch_warning(self.module) as w: |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 101 | self.module.resetwarnings() |
| 102 | self.module.filterwarnings("default", category=UserWarning) |
| 103 | message = UserWarning("FilterTests.test_default") |
| 104 | for x in range(2): |
| 105 | self.module.warn(message, UserWarning) |
| 106 | if x == 0: |
| 107 | self.assertEquals(w.message, message) |
| 108 | w.reset() |
| 109 | elif x == 1: |
| 110 | self.assert_(not w.message, "unexpected warning: " + str(w)) |
| 111 | else: |
| 112 | raise ValueError("loop variant unhandled") |
| 113 | |
| 114 | def test_module(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 115 | with support.catch_warning(self.module) as w: |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 116 | self.module.resetwarnings() |
| 117 | self.module.filterwarnings("module", category=UserWarning) |
| 118 | message = UserWarning("FilterTests.test_module") |
| 119 | self.module.warn(message, UserWarning) |
| 120 | self.assertEquals(w.message, message) |
| 121 | w.reset() |
| 122 | self.module.warn(message, UserWarning) |
| 123 | self.assert_(not w.message, "unexpected message: " + str(w)) |
| 124 | |
| 125 | def test_once(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 126 | with support.catch_warning(self.module) as w: |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 127 | self.module.resetwarnings() |
| 128 | self.module.filterwarnings("once", category=UserWarning) |
| 129 | message = UserWarning("FilterTests.test_once") |
| 130 | self.module.warn_explicit(message, UserWarning, "test_warnings.py", |
| 131 | 42) |
| 132 | self.assertEquals(w.message, message) |
| 133 | w.reset() |
| 134 | self.module.warn_explicit(message, UserWarning, "test_warnings.py", |
| 135 | 13) |
| 136 | self.assert_(not w.message) |
| 137 | self.module.warn_explicit(message, UserWarning, "test_warnings2.py", |
| 138 | 42) |
| 139 | self.assert_(not w.message) |
| 140 | |
| 141 | def test_inheritance(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 142 | with support.catch_warning(self.module) as w: |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 143 | self.module.resetwarnings() |
| 144 | self.module.filterwarnings("error", category=Warning) |
| 145 | self.assertRaises(UserWarning, self.module.warn, |
| 146 | "FilterTests.test_inheritance", UserWarning) |
| 147 | |
| 148 | def test_ordering(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 149 | with support.catch_warning(self.module) as w: |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 150 | self.module.resetwarnings() |
| 151 | self.module.filterwarnings("ignore", category=UserWarning) |
| 152 | self.module.filterwarnings("error", category=UserWarning, |
| 153 | append=True) |
| 154 | w.reset() |
| 155 | try: |
| 156 | self.module.warn("FilterTests.test_ordering", UserWarning) |
| 157 | except UserWarning: |
| 158 | self.fail("order handling for actions failed") |
| 159 | self.assert_(not w.message) |
| 160 | |
| 161 | def test_filterwarnings(self): |
| 162 | # Test filterwarnings(). |
| 163 | # Implicitly also tests resetwarnings(). |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 164 | with support.catch_warning(self.module) as w: |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 165 | self.module.filterwarnings("error", "", Warning, "", 0) |
| 166 | self.assertRaises(UserWarning, self.module.warn, 'convert to error') |
| 167 | |
| 168 | self.module.resetwarnings() |
| 169 | text = 'handle normally' |
| 170 | self.module.warn(text) |
| 171 | self.assertEqual(str(w.message), text) |
| 172 | self.assert_(w.category is UserWarning) |
| 173 | |
| 174 | self.module.filterwarnings("ignore", "", Warning, "", 0) |
| 175 | text = 'filtered out' |
| 176 | self.module.warn(text) |
| 177 | self.assertNotEqual(str(w.message), text) |
| 178 | |
| 179 | self.module.resetwarnings() |
| 180 | self.module.filterwarnings("error", "hex*", Warning, "", 0) |
| 181 | self.assertRaises(UserWarning, self.module.warn, 'hex/oct') |
| 182 | text = 'nonmatching text' |
| 183 | self.module.warn(text) |
| 184 | self.assertEqual(str(w.message), text) |
| 185 | self.assert_(w.category is UserWarning) |
| 186 | |
| 187 | class CFilterTests(BaseTest, FilterTests): |
| 188 | module = c_warnings |
| 189 | |
| 190 | class PyFilterTests(BaseTest, FilterTests): |
| 191 | module = py_warnings |
| 192 | |
| 193 | |
| 194 | class WarnTests(unittest.TestCase): |
| 195 | |
| 196 | """Test warnings.warn() and warnings.warn_explicit().""" |
| 197 | |
| 198 | def test_message(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 199 | with support.catch_warning(self.module) as w: |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 200 | for i in range(4): |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 201 | text = 'multi %d' %i # Different text on each call. |
| 202 | self.module.warn(text) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 203 | self.assertEqual(str(w.message), text) |
| 204 | self.assert_(w.category is UserWarning) |
Raymond Hettinger | d6f6e50 | 2003-07-13 08:37:40 +0000 | [diff] [blame] | 205 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 206 | def test_filename(self): |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 207 | with warnings_state(self.module): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 208 | with support.catch_warning(self.module) as w: |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 209 | warning_tests.inner("spam1") |
| 210 | self.assertEqual(os.path.basename(w.filename), "warning_tests.py") |
| 211 | warning_tests.outer("spam2") |
| 212 | self.assertEqual(os.path.basename(w.filename), "warning_tests.py") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 213 | |
| 214 | def test_stacklevel(self): |
| 215 | # Test stacklevel argument |
| 216 | # 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] | 217 | with warnings_state(self.module): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 218 | with support.catch_warning(self.module) as w: |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 219 | warning_tests.inner("spam3", stacklevel=1) |
| 220 | self.assertEqual(os.path.basename(w.filename), "warning_tests.py") |
| 221 | warning_tests.outer("spam4", stacklevel=1) |
| 222 | self.assertEqual(os.path.basename(w.filename), "warning_tests.py") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 223 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 224 | warning_tests.inner("spam5", stacklevel=2) |
| 225 | self.assertEqual(os.path.basename(w.filename), "test_warnings.py") |
| 226 | warning_tests.outer("spam6", stacklevel=2) |
| 227 | self.assertEqual(os.path.basename(w.filename), "warning_tests.py") |
Christian Heimes | 5d8da20 | 2008-05-06 13:58:24 +0000 | [diff] [blame] | 228 | warning_tests.outer("spam6.5", stacklevel=3) |
| 229 | self.assertEqual(os.path.basename(w.filename), "test_warnings.py") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 230 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 231 | warning_tests.inner("spam7", stacklevel=9999) |
| 232 | self.assertEqual(os.path.basename(w.filename), "sys") |
| 233 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 234 | def test_missing_filename_not_main(self): |
| 235 | # If __file__ is not specified and __main__ is not the module name, |
| 236 | # then __file__ should be set to the module name. |
| 237 | filename = warning_tests.__file__ |
| 238 | try: |
| 239 | del warning_tests.__file__ |
| 240 | with warnings_state(self.module): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 241 | with support.catch_warning(self.module) as w: |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 242 | warning_tests.inner("spam8", stacklevel=1) |
| 243 | self.assertEqual(w.filename, warning_tests.__name__) |
| 244 | finally: |
| 245 | warning_tests.__file__ = filename |
| 246 | |
| 247 | def test_missing_filename_main_with_argv(self): |
| 248 | # If __file__ is not specified and the caller is __main__ and sys.argv |
| 249 | # exists, then use sys.argv[0] as the file. |
| 250 | if not hasattr(sys, 'argv'): |
| 251 | return |
| 252 | filename = warning_tests.__file__ |
| 253 | module_name = warning_tests.__name__ |
| 254 | try: |
| 255 | del warning_tests.__file__ |
| 256 | warning_tests.__name__ = '__main__' |
| 257 | with warnings_state(self.module): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 258 | with support.catch_warning(self.module) as w: |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 259 | warning_tests.inner('spam9', stacklevel=1) |
| 260 | self.assertEqual(w.filename, sys.argv[0]) |
| 261 | finally: |
| 262 | warning_tests.__file__ = filename |
| 263 | warning_tests.__name__ = module_name |
| 264 | |
| 265 | def test_missing_filename_main_without_argv(self): |
| 266 | # If __file__ is not specified, the caller is __main__, and sys.argv |
| 267 | # is not set, then '__main__' is the file name. |
| 268 | filename = warning_tests.__file__ |
| 269 | module_name = warning_tests.__name__ |
| 270 | argv = sys.argv |
| 271 | try: |
| 272 | del warning_tests.__file__ |
| 273 | warning_tests.__name__ = '__main__' |
| 274 | del sys.argv |
| 275 | with warnings_state(self.module): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 276 | with support.catch_warning(self.module) as w: |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 277 | warning_tests.inner('spam10', stacklevel=1) |
| 278 | self.assertEqual(w.filename, '__main__') |
| 279 | finally: |
| 280 | warning_tests.__file__ = filename |
| 281 | warning_tests.__name__ = module_name |
| 282 | sys.argv = argv |
| 283 | |
Christian Heimes | daaf8ee | 2008-05-04 23:58:41 +0000 | [diff] [blame] | 284 | def test_missing_filename_main_with_argv_empty_string(self): |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 285 | # If __file__ is not specified, the caller is __main__, and sys.argv[0] |
| 286 | # is the empty string, then '__main__ is the file name. |
| 287 | # Tests issue 2743. |
| 288 | file_name = 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 | sys.argv = [''] |
| 295 | with warnings_state(self.module): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 296 | with support.catch_warning(self.module) as w: |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 297 | warning_tests.inner('spam11', stacklevel=1) |
| 298 | self.assertEqual(w.filename, '__main__') |
| 299 | finally: |
| 300 | warning_tests.__file__ = file_name |
| 301 | warning_tests.__name__ = module_name |
| 302 | sys.argv = argv |
| 303 | |
| 304 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 305 | |
| 306 | class CWarnTests(BaseTest, WarnTests): |
| 307 | module = c_warnings |
| 308 | |
| 309 | class PyWarnTests(BaseTest, WarnTests): |
| 310 | module = py_warnings |
| 311 | |
| 312 | |
| 313 | class WCmdLineTests(unittest.TestCase): |
| 314 | |
| 315 | def test_improper_input(self): |
| 316 | # Uses the private _setoption() function to test the parsing |
| 317 | # of command-line warning arguments |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 318 | with support.catch_warning(self.module): |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 319 | self.assertRaises(self.module._OptionError, |
| 320 | self.module._setoption, '1:2:3:4:5:6') |
| 321 | self.assertRaises(self.module._OptionError, |
| 322 | self.module._setoption, 'bogus::Warning') |
| 323 | self.assertRaises(self.module._OptionError, |
| 324 | self.module._setoption, 'ignore:2::4:-5') |
| 325 | self.module._setoption('error::Warning::0') |
| 326 | self.assertRaises(UserWarning, self.module.warn, 'convert to error') |
| 327 | |
| 328 | class CWCmdLineTests(BaseTest, WCmdLineTests): |
| 329 | module = c_warnings |
| 330 | |
| 331 | class PyWCmdLineTests(BaseTest, WCmdLineTests): |
| 332 | module = py_warnings |
| 333 | |
| 334 | |
| 335 | class _WarningsTests(BaseTest): |
| 336 | |
| 337 | """Tests specific to the _warnings module.""" |
| 338 | |
| 339 | module = c_warnings |
| 340 | |
| 341 | def test_filter(self): |
| 342 | # Everything should function even if 'filters' is not in warnings. |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 343 | with support.catch_warning(self.module) as w: |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 344 | self.module.filterwarnings("error", "", Warning, "", 0) |
| 345 | self.assertRaises(UserWarning, self.module.warn, |
| 346 | 'convert to error') |
| 347 | del self.module.filters |
| 348 | self.assertRaises(UserWarning, self.module.warn, |
| 349 | 'convert to error') |
| 350 | |
| 351 | def test_onceregistry(self): |
| 352 | # Replacing or removing the onceregistry should be okay. |
| 353 | global __warningregistry__ |
| 354 | message = UserWarning('onceregistry test') |
| 355 | try: |
| 356 | original_registry = self.module.onceregistry |
| 357 | __warningregistry__ = {} |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 358 | with support.catch_warning(self.module) as w: |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 359 | self.module.resetwarnings() |
| 360 | self.module.filterwarnings("once", category=UserWarning) |
| 361 | self.module.warn_explicit(message, UserWarning, "file", 42) |
| 362 | self.failUnlessEqual(w.message, message) |
| 363 | w.reset() |
| 364 | self.module.warn_explicit(message, UserWarning, "file", 42) |
| 365 | self.assert_(not w.message) |
| 366 | # Test the resetting of onceregistry. |
| 367 | self.module.onceregistry = {} |
| 368 | __warningregistry__ = {} |
| 369 | self.module.warn('onceregistry test') |
| 370 | self.failUnlessEqual(w.message.args, message.args) |
| 371 | # Removal of onceregistry is okay. |
| 372 | w.reset() |
| 373 | del self.module.onceregistry |
| 374 | __warningregistry__ = {} |
| 375 | self.module.warn_explicit(message, UserWarning, "file", 42) |
| 376 | self.failUnless(not w.message) |
| 377 | finally: |
| 378 | self.module.onceregistry = original_registry |
| 379 | |
| 380 | def test_showwarning_missing(self): |
| 381 | # Test that showwarning() missing is okay. |
| 382 | text = 'del showwarning test' |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 383 | with support.catch_warning(self.module): |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 384 | self.module.filterwarnings("always", category=UserWarning) |
| 385 | del self.module.showwarning |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 386 | with support.captured_output('stderr') as stream: |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 387 | self.module.warn(text) |
| 388 | result = stream.getvalue() |
| 389 | self.failUnless(text in result) |
| 390 | |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 391 | def test_showwarning_not_callable(self): |
| 392 | self.module.filterwarnings("always", category=UserWarning) |
| 393 | old_showwarning = self.module.showwarning |
| 394 | self.module.showwarning = 23 |
| 395 | try: |
| 396 | self.assertRaises(TypeError, self.module.warn, "Warning!") |
| 397 | finally: |
| 398 | self.module.showwarning = old_showwarning |
Alexandre Vassalotti | e9f305f | 2008-05-16 04:39:54 +0000 | [diff] [blame] | 399 | self.module.resetwarnings() |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 400 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 401 | def test_show_warning_output(self): |
| 402 | # With showarning() missing, make sure that output is okay. |
| 403 | text = 'test show_warning' |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 404 | with support.catch_warning(self.module): |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 405 | self.module.filterwarnings("always", category=UserWarning) |
| 406 | del self.module.showwarning |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 407 | with support.captured_output('stderr') as stream: |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 408 | warning_tests.inner(text) |
| 409 | result = stream.getvalue() |
| 410 | self.failUnlessEqual(result.count('\n'), 2, |
| 411 | "Too many newlines in %r" % result) |
| 412 | first_line, second_line = result.split('\n', 1) |
| 413 | expected_file = os.path.splitext(warning_tests.__file__)[0] + '.py' |
Neal Norwitz | 32dde22 | 2008-04-15 06:43:13 +0000 | [diff] [blame] | 414 | first_line_parts = first_line.rsplit(':', 3) |
| 415 | path, line, warning_class, message = first_line_parts |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 416 | line = int(line) |
| 417 | self.failUnlessEqual(expected_file, path) |
| 418 | self.failUnlessEqual(warning_class, ' ' + UserWarning.__name__) |
| 419 | self.failUnlessEqual(message, ' ' + text) |
| 420 | expected_line = ' ' + linecache.getline(path, line).strip() + '\n' |
| 421 | assert expected_line |
| 422 | self.failUnlessEqual(second_line, expected_line) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 423 | |
| 424 | |
Christian Heimes | 5fb7c2a | 2007-12-24 08:52:31 +0000 | [diff] [blame] | 425 | class WarningsDisplayTests(unittest.TestCase): |
| 426 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 427 | """Test the displaying of warnings and the ability to overload functions |
| 428 | related to displaying warnings.""" |
| 429 | |
Christian Heimes | 5fb7c2a | 2007-12-24 08:52:31 +0000 | [diff] [blame] | 430 | def test_formatwarning(self): |
| 431 | message = "msg" |
| 432 | category = Warning |
| 433 | file_name = os.path.splitext(warning_tests.__file__)[0] + '.py' |
| 434 | line_num = 3 |
| 435 | file_line = linecache.getline(file_name, line_num).strip() |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 436 | format = "%s:%s: %s: %s\n %s\n" |
| 437 | expect = format % (file_name, line_num, category.__name__, message, |
| 438 | file_line) |
| 439 | self.failUnlessEqual(expect, self.module.formatwarning(message, |
| 440 | category, file_name, line_num)) |
| 441 | # Test the 'line' argument. |
| 442 | file_line += " for the win!" |
| 443 | expect = format % (file_name, line_num, category.__name__, message, |
| 444 | file_line) |
| 445 | self.failUnlessEqual(expect, self.module.formatwarning(message, |
| 446 | category, file_name, line_num, file_line)) |
Christian Heimes | 5fb7c2a | 2007-12-24 08:52:31 +0000 | [diff] [blame] | 447 | |
| 448 | def test_showwarning(self): |
| 449 | file_name = os.path.splitext(warning_tests.__file__)[0] + '.py' |
| 450 | line_num = 3 |
| 451 | expected_file_line = linecache.getline(file_name, line_num).strip() |
| 452 | message = 'msg' |
| 453 | category = Warning |
| 454 | file_object = StringIO() |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 455 | expect = self.module.formatwarning(message, category, file_name, |
| 456 | line_num) |
| 457 | self.module.showwarning(message, category, file_name, line_num, |
Christian Heimes | 5fb7c2a | 2007-12-24 08:52:31 +0000 | [diff] [blame] | 458 | file_object) |
| 459 | self.failUnlessEqual(file_object.getvalue(), expect) |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 460 | # Test 'line' argument. |
| 461 | expected_file_line += "for the win!" |
| 462 | expect = self.module.formatwarning(message, category, file_name, |
| 463 | line_num, expected_file_line) |
| 464 | file_object = StringIO() |
| 465 | self.module.showwarning(message, category, file_name, line_num, |
| 466 | file_object, expected_file_line) |
| 467 | self.failUnlessEqual(expect, file_object.getvalue()) |
| 468 | |
| 469 | class CWarningsDisplayTests(BaseTest, WarningsDisplayTests): |
| 470 | module = c_warnings |
| 471 | |
| 472 | class PyWarningsDisplayTests(BaseTest, WarningsDisplayTests): |
| 473 | module = py_warnings |
Christian Heimes | 5fb7c2a | 2007-12-24 08:52:31 +0000 | [diff] [blame] | 474 | |
| 475 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 476 | def test_main(): |
Christian Heimes | dae2a89 | 2008-04-19 00:55:37 +0000 | [diff] [blame] | 477 | py_warnings.onceregistry.clear() |
| 478 | c_warnings.onceregistry.clear() |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 479 | support.run_unittest(CFilterTests, |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 480 | PyFilterTests, |
| 481 | CWarnTests, |
| 482 | PyWarnTests, |
| 483 | CWCmdLineTests, PyWCmdLineTests, |
| 484 | _WarningsTests, |
| 485 | CWarningsDisplayTests, PyWarningsDisplayTests, |
| 486 | ) |
| 487 | |
Raymond Hettinger | d6f6e50 | 2003-07-13 08:37:40 +0000 | [diff] [blame] | 488 | |
| 489 | if __name__ == "__main__": |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 490 | test_main() |