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