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