Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1 | from contextlib import contextmanager |
Christian Heimes | 5fb7c2a | 2007-12-24 08:52:31 +0000 | [diff] [blame] | 2 | import linecache |
Raymond Hettinger | dc9dcf1 | 2003-07-13 06:15:11 +0000 | [diff] [blame] | 3 | import os |
Christian Heimes | 5fb7c2a | 2007-12-24 08:52:31 +0000 | [diff] [blame] | 4 | from io import StringIO |
Guido van Rossum | 61e21b5 | 2007-08-20 19:06:03 +0000 | [diff] [blame] | 5 | import sys |
Raymond Hettinger | d6f6e50 | 2003-07-13 08:37:40 +0000 | [diff] [blame] | 6 | import unittest |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 7 | from test import support |
Antoine Pitrou | 6999441 | 2014-04-29 00:56:08 +0200 | [diff] [blame] | 8 | from test.script_helper import assert_python_ok, assert_python_failure |
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 | |
Ezio Melotti | 2688e81 | 2013-01-10 06:52:23 +0200 | [diff] [blame] | 42 | class BaseTest: |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 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 | |
Brett Cannon | 14ad531 | 2014-08-22 10:44:47 -0400 | [diff] [blame] | 64 | class PublicAPITests(BaseTest): |
| 65 | |
| 66 | """Ensures that the correct values are exposed in the |
| 67 | public API. |
| 68 | """ |
| 69 | |
| 70 | def test_module_all_attribute(self): |
| 71 | self.assertTrue(hasattr(self.module, '__all__')) |
| 72 | target_api = ["warn", "warn_explicit", "showwarning", |
| 73 | "formatwarning", "filterwarnings", "simplefilter", |
| 74 | "resetwarnings", "catch_warnings"] |
| 75 | self.assertSetEqual(set(self.module.__all__), |
| 76 | set(target_api)) |
| 77 | |
| 78 | class CPublicAPITests(PublicAPITests, unittest.TestCase): |
| 79 | module = c_warnings |
| 80 | |
| 81 | class PyPublicAPITests(PublicAPITests, unittest.TestCase): |
| 82 | module = py_warnings |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 83 | |
Ezio Melotti | 2688e81 | 2013-01-10 06:52:23 +0200 | [diff] [blame] | 84 | class FilterTests(BaseTest): |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 85 | |
| 86 | """Testing the filtering functionality.""" |
| 87 | |
| 88 | def test_error(self): |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 89 | with original_warnings.catch_warnings(module=self.module) as w: |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 90 | self.module.resetwarnings() |
| 91 | self.module.filterwarnings("error", category=UserWarning) |
| 92 | self.assertRaises(UserWarning, self.module.warn, |
| 93 | "FilterTests.test_error") |
| 94 | |
Antoine Pitrou | cb0a006 | 2014-09-18 02:40:46 +0200 | [diff] [blame] | 95 | def test_error_after_default(self): |
| 96 | with original_warnings.catch_warnings(module=self.module) as w: |
| 97 | self.module.resetwarnings() |
| 98 | message = "FilterTests.test_ignore_after_default" |
| 99 | def f(): |
| 100 | self.module.warn(message, UserWarning) |
| 101 | f() |
| 102 | self.module.filterwarnings("error", category=UserWarning) |
| 103 | self.assertRaises(UserWarning, f) |
| 104 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 105 | def test_ignore(self): |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 106 | with original_warnings.catch_warnings(record=True, |
| 107 | module=self.module) as w: |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 108 | self.module.resetwarnings() |
| 109 | self.module.filterwarnings("ignore", category=UserWarning) |
| 110 | self.module.warn("FilterTests.test_ignore", UserWarning) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 111 | self.assertEqual(len(w), 0) |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 112 | |
Antoine Pitrou | cb0a006 | 2014-09-18 02:40:46 +0200 | [diff] [blame] | 113 | def test_ignore_after_default(self): |
| 114 | with original_warnings.catch_warnings(record=True, |
| 115 | module=self.module) as w: |
| 116 | self.module.resetwarnings() |
| 117 | message = "FilterTests.test_ignore_after_default" |
| 118 | def f(): |
| 119 | self.module.warn(message, UserWarning) |
| 120 | f() |
| 121 | self.module.filterwarnings("ignore", category=UserWarning) |
| 122 | f() |
| 123 | f() |
| 124 | self.assertEqual(len(w), 1) |
| 125 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 126 | def test_always(self): |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 127 | with original_warnings.catch_warnings(record=True, |
| 128 | module=self.module) as w: |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 129 | self.module.resetwarnings() |
| 130 | self.module.filterwarnings("always", category=UserWarning) |
| 131 | message = "FilterTests.test_always" |
| 132 | self.module.warn(message, UserWarning) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 133 | self.assertTrue(message, w[-1].message) |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 134 | self.module.warn(message, UserWarning) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 135 | self.assertTrue(w[-1].message, message) |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 136 | |
Antoine Pitrou | cb0a006 | 2014-09-18 02:40:46 +0200 | [diff] [blame] | 137 | def test_always_after_default(self): |
| 138 | with original_warnings.catch_warnings(record=True, |
| 139 | module=self.module) as w: |
| 140 | self.module.resetwarnings() |
| 141 | message = "FilterTests.test_always_after_ignore" |
| 142 | def f(): |
| 143 | self.module.warn(message, UserWarning) |
| 144 | f() |
| 145 | self.assertEqual(len(w), 1) |
| 146 | self.assertEqual(w[-1].message.args[0], message) |
| 147 | f() |
| 148 | self.assertEqual(len(w), 1) |
| 149 | self.module.filterwarnings("always", category=UserWarning) |
| 150 | f() |
| 151 | self.assertEqual(len(w), 2) |
| 152 | self.assertEqual(w[-1].message.args[0], message) |
| 153 | f() |
| 154 | self.assertEqual(len(w), 3) |
| 155 | self.assertEqual(w[-1].message.args[0], message) |
| 156 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 157 | def test_default(self): |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 158 | with original_warnings.catch_warnings(record=True, |
| 159 | module=self.module) as w: |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 160 | self.module.resetwarnings() |
| 161 | self.module.filterwarnings("default", category=UserWarning) |
| 162 | message = UserWarning("FilterTests.test_default") |
| 163 | for x in range(2): |
| 164 | self.module.warn(message, UserWarning) |
| 165 | if x == 0: |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 166 | self.assertEqual(w[-1].message, message) |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 167 | del w[:] |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 168 | elif x == 1: |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 169 | self.assertEqual(len(w), 0) |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 170 | else: |
| 171 | raise ValueError("loop variant unhandled") |
| 172 | |
| 173 | def test_module(self): |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 174 | with original_warnings.catch_warnings(record=True, |
| 175 | module=self.module) as w: |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 176 | self.module.resetwarnings() |
| 177 | self.module.filterwarnings("module", category=UserWarning) |
| 178 | message = UserWarning("FilterTests.test_module") |
| 179 | self.module.warn(message, UserWarning) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 180 | self.assertEqual(w[-1].message, message) |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 181 | del w[:] |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 182 | self.module.warn(message, UserWarning) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 183 | self.assertEqual(len(w), 0) |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 184 | |
| 185 | def test_once(self): |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 186 | with original_warnings.catch_warnings(record=True, |
| 187 | module=self.module) as w: |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 188 | self.module.resetwarnings() |
| 189 | self.module.filterwarnings("once", category=UserWarning) |
| 190 | message = UserWarning("FilterTests.test_once") |
| 191 | self.module.warn_explicit(message, UserWarning, "test_warnings.py", |
| 192 | 42) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 193 | self.assertEqual(w[-1].message, message) |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 194 | del w[:] |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 195 | self.module.warn_explicit(message, UserWarning, "test_warnings.py", |
| 196 | 13) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 197 | self.assertEqual(len(w), 0) |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 198 | self.module.warn_explicit(message, UserWarning, "test_warnings2.py", |
| 199 | 42) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 200 | self.assertEqual(len(w), 0) |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 201 | |
| 202 | def test_inheritance(self): |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 203 | with original_warnings.catch_warnings(module=self.module) as w: |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 204 | self.module.resetwarnings() |
| 205 | self.module.filterwarnings("error", category=Warning) |
| 206 | self.assertRaises(UserWarning, self.module.warn, |
| 207 | "FilterTests.test_inheritance", UserWarning) |
| 208 | |
| 209 | def test_ordering(self): |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 210 | with original_warnings.catch_warnings(record=True, |
| 211 | module=self.module) as w: |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 212 | self.module.resetwarnings() |
| 213 | self.module.filterwarnings("ignore", category=UserWarning) |
| 214 | self.module.filterwarnings("error", category=UserWarning, |
| 215 | append=True) |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 216 | del w[:] |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 217 | try: |
| 218 | self.module.warn("FilterTests.test_ordering", UserWarning) |
| 219 | except UserWarning: |
| 220 | self.fail("order handling for actions failed") |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 221 | self.assertEqual(len(w), 0) |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 222 | |
| 223 | def test_filterwarnings(self): |
| 224 | # Test filterwarnings(). |
| 225 | # Implicitly also tests resetwarnings(). |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 226 | with original_warnings.catch_warnings(record=True, |
| 227 | module=self.module) as w: |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 228 | self.module.filterwarnings("error", "", Warning, "", 0) |
| 229 | self.assertRaises(UserWarning, self.module.warn, 'convert to error') |
| 230 | |
| 231 | self.module.resetwarnings() |
| 232 | text = 'handle normally' |
| 233 | self.module.warn(text) |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 234 | self.assertEqual(str(w[-1].message), text) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 235 | self.assertTrue(w[-1].category is UserWarning) |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 236 | |
| 237 | self.module.filterwarnings("ignore", "", Warning, "", 0) |
| 238 | text = 'filtered out' |
| 239 | self.module.warn(text) |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 240 | self.assertNotEqual(str(w[-1].message), text) |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 241 | |
| 242 | self.module.resetwarnings() |
| 243 | self.module.filterwarnings("error", "hex*", Warning, "", 0) |
| 244 | self.assertRaises(UserWarning, self.module.warn, 'hex/oct') |
| 245 | text = 'nonmatching text' |
| 246 | self.module.warn(text) |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 247 | self.assertEqual(str(w[-1].message), text) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 248 | self.assertTrue(w[-1].category is UserWarning) |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 249 | |
Ezio Melotti | 2688e81 | 2013-01-10 06:52:23 +0200 | [diff] [blame] | 250 | class CFilterTests(FilterTests, unittest.TestCase): |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 251 | module = c_warnings |
| 252 | |
Ezio Melotti | 2688e81 | 2013-01-10 06:52:23 +0200 | [diff] [blame] | 253 | class PyFilterTests(FilterTests, unittest.TestCase): |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 254 | module = py_warnings |
| 255 | |
| 256 | |
Ezio Melotti | 2688e81 | 2013-01-10 06:52:23 +0200 | [diff] [blame] | 257 | class WarnTests(BaseTest): |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 258 | |
| 259 | """Test warnings.warn() and warnings.warn_explicit().""" |
| 260 | |
| 261 | def test_message(self): |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 262 | with original_warnings.catch_warnings(record=True, |
| 263 | module=self.module) as w: |
Florent Xicluna | fd1b093 | 2010-03-28 00:25:02 +0000 | [diff] [blame] | 264 | self.module.simplefilter("once") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 265 | for i in range(4): |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 266 | text = 'multi %d' %i # Different text on each call. |
| 267 | self.module.warn(text) |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 268 | self.assertEqual(str(w[-1].message), text) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 269 | self.assertTrue(w[-1].category is UserWarning) |
Raymond Hettinger | d6f6e50 | 2003-07-13 08:37:40 +0000 | [diff] [blame] | 270 | |
Brett Cannon | 54bd41d | 2008-09-02 04:01:42 +0000 | [diff] [blame] | 271 | # Issue 3639 |
| 272 | def test_warn_nonstandard_types(self): |
| 273 | # warn() should handle non-standard types without issue. |
| 274 | for ob in (Warning, None, 42): |
Benjamin Peterson | fcf5d63 | 2008-10-16 23:24:44 +0000 | [diff] [blame] | 275 | with original_warnings.catch_warnings(record=True, |
| 276 | module=self.module) as w: |
Florent Xicluna | fd1b093 | 2010-03-28 00:25:02 +0000 | [diff] [blame] | 277 | self.module.simplefilter("once") |
Brett Cannon | 54bd41d | 2008-09-02 04:01:42 +0000 | [diff] [blame] | 278 | self.module.warn(ob) |
| 279 | # Don't directly compare objects since |
| 280 | # ``Warning() != Warning()``. |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 281 | self.assertEqual(str(w[-1].message), str(UserWarning(ob))) |
Brett Cannon | 54bd41d | 2008-09-02 04:01:42 +0000 | [diff] [blame] | 282 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 283 | def test_filename(self): |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 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 | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 287 | warning_tests.inner("spam1") |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 288 | self.assertEqual(os.path.basename(w[-1].filename), |
| 289 | "warning_tests.py") |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 290 | warning_tests.outer("spam2") |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 291 | self.assertEqual(os.path.basename(w[-1].filename), |
| 292 | "warning_tests.py") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 293 | |
| 294 | def test_stacklevel(self): |
| 295 | # Test stacklevel argument |
| 296 | # 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] | 297 | with warnings_state(self.module): |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 298 | with original_warnings.catch_warnings(record=True, |
| 299 | module=self.module) as w: |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 300 | warning_tests.inner("spam3", stacklevel=1) |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 301 | self.assertEqual(os.path.basename(w[-1].filename), |
| 302 | "warning_tests.py") |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 303 | warning_tests.outer("spam4", stacklevel=1) |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 304 | self.assertEqual(os.path.basename(w[-1].filename), |
| 305 | "warning_tests.py") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 306 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 307 | warning_tests.inner("spam5", stacklevel=2) |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 308 | self.assertEqual(os.path.basename(w[-1].filename), |
| 309 | "test_warnings.py") |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 310 | warning_tests.outer("spam6", stacklevel=2) |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 311 | self.assertEqual(os.path.basename(w[-1].filename), |
| 312 | "warning_tests.py") |
Christian Heimes | 5d8da20 | 2008-05-06 13:58:24 +0000 | [diff] [blame] | 313 | warning_tests.outer("spam6.5", stacklevel=3) |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 314 | self.assertEqual(os.path.basename(w[-1].filename), |
| 315 | "test_warnings.py") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 316 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 317 | warning_tests.inner("spam7", stacklevel=9999) |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 318 | self.assertEqual(os.path.basename(w[-1].filename), |
| 319 | "sys") |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 320 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 321 | def test_missing_filename_not_main(self): |
| 322 | # If __file__ is not specified and __main__ is not the module name, |
| 323 | # then __file__ should be set to the module name. |
| 324 | filename = warning_tests.__file__ |
| 325 | try: |
| 326 | del warning_tests.__file__ |
| 327 | with warnings_state(self.module): |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 328 | with original_warnings.catch_warnings(record=True, |
| 329 | module=self.module) as w: |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 330 | warning_tests.inner("spam8", stacklevel=1) |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 331 | self.assertEqual(w[-1].filename, warning_tests.__name__) |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 332 | finally: |
| 333 | warning_tests.__file__ = filename |
| 334 | |
Serhiy Storchaka | 4376763 | 2013-11-03 21:31:38 +0200 | [diff] [blame] | 335 | @unittest.skipUnless(hasattr(sys, 'argv'), 'test needs sys.argv') |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 336 | def test_missing_filename_main_with_argv(self): |
| 337 | # If __file__ is not specified and the caller is __main__ and sys.argv |
| 338 | # exists, then use sys.argv[0] as the file. |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 339 | filename = warning_tests.__file__ |
| 340 | module_name = warning_tests.__name__ |
| 341 | try: |
| 342 | del warning_tests.__file__ |
| 343 | warning_tests.__name__ = '__main__' |
| 344 | with warnings_state(self.module): |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 345 | with original_warnings.catch_warnings(record=True, |
| 346 | module=self.module) as w: |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 347 | warning_tests.inner('spam9', stacklevel=1) |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 348 | self.assertEqual(w[-1].filename, sys.argv[0]) |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 349 | finally: |
| 350 | warning_tests.__file__ = filename |
| 351 | warning_tests.__name__ = module_name |
| 352 | |
| 353 | def test_missing_filename_main_without_argv(self): |
| 354 | # If __file__ is not specified, the caller is __main__, and sys.argv |
| 355 | # is not set, then '__main__' is the file name. |
| 356 | filename = warning_tests.__file__ |
| 357 | module_name = warning_tests.__name__ |
| 358 | argv = sys.argv |
| 359 | try: |
| 360 | del warning_tests.__file__ |
| 361 | warning_tests.__name__ = '__main__' |
| 362 | del sys.argv |
| 363 | with warnings_state(self.module): |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 364 | with original_warnings.catch_warnings(record=True, |
| 365 | module=self.module) as w: |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 366 | warning_tests.inner('spam10', stacklevel=1) |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 367 | self.assertEqual(w[-1].filename, '__main__') |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 368 | finally: |
| 369 | warning_tests.__file__ = filename |
| 370 | warning_tests.__name__ = module_name |
| 371 | sys.argv = argv |
| 372 | |
Christian Heimes | daaf8ee | 2008-05-04 23:58:41 +0000 | [diff] [blame] | 373 | def test_missing_filename_main_with_argv_empty_string(self): |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 374 | # If __file__ is not specified, the caller is __main__, and sys.argv[0] |
| 375 | # is the empty string, then '__main__ is the file name. |
| 376 | # Tests issue 2743. |
| 377 | file_name = warning_tests.__file__ |
| 378 | module_name = warning_tests.__name__ |
| 379 | argv = sys.argv |
| 380 | try: |
| 381 | del warning_tests.__file__ |
| 382 | warning_tests.__name__ = '__main__' |
| 383 | sys.argv = [''] |
| 384 | with warnings_state(self.module): |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 385 | with original_warnings.catch_warnings(record=True, |
| 386 | module=self.module) as w: |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 387 | warning_tests.inner('spam11', stacklevel=1) |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 388 | self.assertEqual(w[-1].filename, '__main__') |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 389 | finally: |
| 390 | warning_tests.__file__ = file_name |
| 391 | warning_tests.__name__ = module_name |
| 392 | sys.argv = argv |
| 393 | |
Victor Stinner | a4c704b | 2013-10-29 23:43:41 +0100 | [diff] [blame] | 394 | def test_warn_explicit_non_ascii_filename(self): |
| 395 | with original_warnings.catch_warnings(record=True, |
| 396 | module=self.module) as w: |
| 397 | self.module.resetwarnings() |
| 398 | self.module.filterwarnings("always", category=UserWarning) |
Victor Stinner | c0e07a3 | 2013-10-29 23:58:05 +0100 | [diff] [blame] | 399 | for filename in ("nonascii\xe9\u20ac", "surrogate\udc80"): |
| 400 | try: |
| 401 | os.fsencode(filename) |
| 402 | except UnicodeEncodeError: |
| 403 | continue |
| 404 | self.module.warn_explicit("text", UserWarning, filename, 1) |
| 405 | self.assertEqual(w[-1].filename, filename) |
Victor Stinner | a4c704b | 2013-10-29 23:43:41 +0100 | [diff] [blame] | 406 | |
Brett Cannon | db73491 | 2008-06-27 00:52:15 +0000 | [diff] [blame] | 407 | def test_warn_explicit_type_errors(self): |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 408 | # warn_explicit() should error out gracefully if it is given objects |
Brett Cannon | db73491 | 2008-06-27 00:52:15 +0000 | [diff] [blame] | 409 | # of the wrong types. |
| 410 | # lineno is expected to be an integer. |
| 411 | self.assertRaises(TypeError, self.module.warn_explicit, |
| 412 | None, UserWarning, None, None) |
| 413 | # Either 'message' needs to be an instance of Warning or 'category' |
| 414 | # needs to be a subclass. |
| 415 | self.assertRaises(TypeError, self.module.warn_explicit, |
| 416 | None, None, None, 1) |
| 417 | # 'registry' must be a dict or None. |
| 418 | self.assertRaises((TypeError, AttributeError), |
| 419 | self.module.warn_explicit, |
| 420 | None, Warning, None, 1, registry=42) |
| 421 | |
Hirokazu Yamamoto | 1c0c003 | 2009-07-17 06:55:42 +0000 | [diff] [blame] | 422 | def test_bad_str(self): |
| 423 | # issue 6415 |
| 424 | # Warnings instance with a bad format string for __str__ should not |
| 425 | # trigger a bus error. |
| 426 | class BadStrWarning(Warning): |
| 427 | """Warning with a bad format string for __str__.""" |
| 428 | def __str__(self): |
| 429 | return ("A bad formatted string %(err)" % |
| 430 | {"err" : "there is no %(err)s"}) |
| 431 | |
| 432 | with self.assertRaises(ValueError): |
| 433 | self.module.warn(BadStrWarning()) |
| 434 | |
Berker Peksag | d8089e0 | 2014-07-11 19:50:25 +0300 | [diff] [blame] | 435 | def test_warning_classes(self): |
| 436 | class MyWarningClass(Warning): |
| 437 | pass |
| 438 | |
| 439 | class NonWarningSubclass: |
| 440 | pass |
| 441 | |
| 442 | # passing a non-subclass of Warning should raise a TypeError |
| 443 | with self.assertRaises(TypeError) as cm: |
| 444 | self.module.warn('bad warning category', '') |
| 445 | self.assertIn('category must be a Warning subclass, not ', |
| 446 | str(cm.exception)) |
| 447 | |
| 448 | with self.assertRaises(TypeError) as cm: |
| 449 | self.module.warn('bad warning category', NonWarningSubclass) |
| 450 | self.assertIn('category must be a Warning subclass, not ', |
| 451 | str(cm.exception)) |
| 452 | |
| 453 | # check that warning instances also raise a TypeError |
| 454 | with self.assertRaises(TypeError) as cm: |
| 455 | self.module.warn('bad warning category', MyWarningClass()) |
| 456 | self.assertIn('category must be a Warning subclass, not ', |
| 457 | str(cm.exception)) |
| 458 | |
| 459 | with self.assertWarns(MyWarningClass) as cm: |
| 460 | self.module.warn('good warning category', MyWarningClass) |
| 461 | self.assertEqual('good warning category', str(cm.warning)) |
| 462 | |
| 463 | with self.assertWarns(UserWarning) as cm: |
| 464 | self.module.warn('good warning category', None) |
| 465 | self.assertEqual('good warning category', str(cm.warning)) |
| 466 | |
| 467 | with self.assertWarns(MyWarningClass) as cm: |
| 468 | self.module.warn('good warning category', MyWarningClass) |
| 469 | self.assertIsInstance(cm.warning, Warning) |
Hirokazu Yamamoto | 1c0c003 | 2009-07-17 06:55:42 +0000 | [diff] [blame] | 470 | |
Ezio Melotti | 2688e81 | 2013-01-10 06:52:23 +0200 | [diff] [blame] | 471 | class CWarnTests(WarnTests, unittest.TestCase): |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 472 | module = c_warnings |
| 473 | |
Nick Coghlan | fce769e | 2009-04-11 14:30:59 +0000 | [diff] [blame] | 474 | # As an early adopter, we sanity check the |
| 475 | # test.support.import_fresh_module utility function |
| 476 | def test_accelerated(self): |
| 477 | self.assertFalse(original_warnings is self.module) |
| 478 | self.assertFalse(hasattr(self.module.warn, '__code__')) |
| 479 | |
Ezio Melotti | 2688e81 | 2013-01-10 06:52:23 +0200 | [diff] [blame] | 480 | class PyWarnTests(WarnTests, unittest.TestCase): |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 481 | module = py_warnings |
| 482 | |
Nick Coghlan | fce769e | 2009-04-11 14:30:59 +0000 | [diff] [blame] | 483 | # As an early adopter, we sanity check the |
| 484 | # test.support.import_fresh_module utility function |
| 485 | def test_pure_python(self): |
| 486 | self.assertFalse(original_warnings is self.module) |
| 487 | self.assertTrue(hasattr(self.module.warn, '__code__')) |
| 488 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 489 | |
Ezio Melotti | 2688e81 | 2013-01-10 06:52:23 +0200 | [diff] [blame] | 490 | class WCmdLineTests(BaseTest): |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 491 | |
| 492 | def test_improper_input(self): |
| 493 | # Uses the private _setoption() function to test the parsing |
| 494 | # of command-line warning arguments |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 495 | with original_warnings.catch_warnings(module=self.module): |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 496 | self.assertRaises(self.module._OptionError, |
| 497 | self.module._setoption, '1:2:3:4:5:6') |
| 498 | self.assertRaises(self.module._OptionError, |
| 499 | self.module._setoption, 'bogus::Warning') |
| 500 | self.assertRaises(self.module._OptionError, |
| 501 | self.module._setoption, 'ignore:2::4:-5') |
| 502 | self.module._setoption('error::Warning::0') |
| 503 | self.assertRaises(UserWarning, self.module.warn, 'convert to error') |
| 504 | |
Antoine Pitrou | cf9f980 | 2010-11-10 13:55:25 +0000 | [diff] [blame] | 505 | def test_improper_option(self): |
| 506 | # Same as above, but check that the message is printed out when |
| 507 | # the interpreter is executed. This also checks that options are |
| 508 | # actually parsed at all. |
| 509 | rc, out, err = assert_python_ok("-Wxxx", "-c", "pass") |
| 510 | self.assertIn(b"Invalid -W option ignored: invalid action: 'xxx'", err) |
| 511 | |
| 512 | def test_warnings_bootstrap(self): |
| 513 | # Check that the warnings module does get loaded when -W<some option> |
| 514 | # is used (see issue #10372 for an example of silent bootstrap failure). |
| 515 | rc, out, err = assert_python_ok("-Wi", "-c", |
| 516 | "import sys; sys.modules['warnings'].warn('foo', RuntimeWarning)") |
| 517 | # '-Wi' was observed |
| 518 | self.assertFalse(out.strip()) |
| 519 | self.assertNotIn(b'RuntimeWarning', err) |
| 520 | |
Ezio Melotti | 2688e81 | 2013-01-10 06:52:23 +0200 | [diff] [blame] | 521 | class CWCmdLineTests(WCmdLineTests, unittest.TestCase): |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 522 | module = c_warnings |
| 523 | |
Ezio Melotti | 2688e81 | 2013-01-10 06:52:23 +0200 | [diff] [blame] | 524 | class PyWCmdLineTests(WCmdLineTests, unittest.TestCase): |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 525 | module = py_warnings |
| 526 | |
| 527 | |
Ezio Melotti | 2688e81 | 2013-01-10 06:52:23 +0200 | [diff] [blame] | 528 | class _WarningsTests(BaseTest, unittest.TestCase): |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 529 | |
| 530 | """Tests specific to the _warnings module.""" |
| 531 | |
| 532 | module = c_warnings |
| 533 | |
| 534 | def test_filter(self): |
| 535 | # Everything should function even if 'filters' is not in warnings. |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 536 | with original_warnings.catch_warnings(module=self.module) as w: |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 537 | self.module.filterwarnings("error", "", Warning, "", 0) |
| 538 | self.assertRaises(UserWarning, self.module.warn, |
| 539 | 'convert to error') |
| 540 | del self.module.filters |
| 541 | self.assertRaises(UserWarning, self.module.warn, |
| 542 | 'convert to error') |
| 543 | |
| 544 | def test_onceregistry(self): |
| 545 | # Replacing or removing the onceregistry should be okay. |
| 546 | global __warningregistry__ |
| 547 | message = UserWarning('onceregistry test') |
| 548 | try: |
| 549 | original_registry = self.module.onceregistry |
| 550 | __warningregistry__ = {} |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 551 | with original_warnings.catch_warnings(record=True, |
| 552 | module=self.module) as w: |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 553 | self.module.resetwarnings() |
| 554 | self.module.filterwarnings("once", category=UserWarning) |
| 555 | self.module.warn_explicit(message, UserWarning, "file", 42) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 556 | self.assertEqual(w[-1].message, message) |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 557 | del w[:] |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 558 | self.module.warn_explicit(message, UserWarning, "file", 42) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 559 | self.assertEqual(len(w), 0) |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 560 | # Test the resetting of onceregistry. |
| 561 | self.module.onceregistry = {} |
| 562 | __warningregistry__ = {} |
| 563 | self.module.warn('onceregistry test') |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 564 | self.assertEqual(w[-1].message.args, message.args) |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 565 | # Removal of onceregistry is okay. |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 566 | del w[:] |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 567 | del self.module.onceregistry |
| 568 | __warningregistry__ = {} |
| 569 | self.module.warn_explicit(message, UserWarning, "file", 42) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 570 | self.assertEqual(len(w), 0) |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 571 | finally: |
| 572 | self.module.onceregistry = original_registry |
| 573 | |
Brett Cannon | 0759dd6 | 2009-04-01 18:13:07 +0000 | [diff] [blame] | 574 | def test_default_action(self): |
| 575 | # Replacing or removing defaultaction should be okay. |
| 576 | message = UserWarning("defaultaction test") |
| 577 | original = self.module.defaultaction |
| 578 | try: |
| 579 | with original_warnings.catch_warnings(record=True, |
| 580 | module=self.module) as w: |
| 581 | self.module.resetwarnings() |
| 582 | registry = {} |
| 583 | self.module.warn_explicit(message, UserWarning, "<test>", 42, |
| 584 | registry=registry) |
| 585 | self.assertEqual(w[-1].message, message) |
| 586 | self.assertEqual(len(w), 1) |
Antoine Pitrou | cb0a006 | 2014-09-18 02:40:46 +0200 | [diff] [blame] | 587 | # One actual registry key plus the "version" key |
| 588 | self.assertEqual(len(registry), 2) |
| 589 | self.assertIn("version", registry) |
Brett Cannon | 0759dd6 | 2009-04-01 18:13:07 +0000 | [diff] [blame] | 590 | del w[:] |
| 591 | # Test removal. |
| 592 | del self.module.defaultaction |
| 593 | __warningregistry__ = {} |
| 594 | registry = {} |
| 595 | self.module.warn_explicit(message, UserWarning, "<test>", 43, |
| 596 | registry=registry) |
| 597 | self.assertEqual(w[-1].message, message) |
| 598 | self.assertEqual(len(w), 1) |
Antoine Pitrou | cb0a006 | 2014-09-18 02:40:46 +0200 | [diff] [blame] | 599 | self.assertEqual(len(registry), 2) |
Brett Cannon | 0759dd6 | 2009-04-01 18:13:07 +0000 | [diff] [blame] | 600 | del w[:] |
| 601 | # Test setting. |
| 602 | self.module.defaultaction = "ignore" |
| 603 | __warningregistry__ = {} |
| 604 | registry = {} |
| 605 | self.module.warn_explicit(message, UserWarning, "<test>", 44, |
| 606 | registry=registry) |
| 607 | self.assertEqual(len(w), 0) |
| 608 | finally: |
| 609 | self.module.defaultaction = original |
| 610 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 611 | def test_showwarning_missing(self): |
| 612 | # Test that showwarning() missing is okay. |
| 613 | text = 'del showwarning test' |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 614 | with original_warnings.catch_warnings(module=self.module): |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 615 | self.module.filterwarnings("always", category=UserWarning) |
| 616 | del self.module.showwarning |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 617 | with support.captured_output('stderr') as stream: |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 618 | self.module.warn(text) |
| 619 | result = stream.getvalue() |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 620 | self.assertIn(text, result) |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 621 | |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 622 | def test_showwarning_not_callable(self): |
Brett Cannon | fcc0527 | 2009-04-01 20:27:29 +0000 | [diff] [blame] | 623 | with original_warnings.catch_warnings(module=self.module): |
| 624 | self.module.filterwarnings("always", category=UserWarning) |
Brett Cannon | 52a7d98 | 2011-07-17 19:17:55 -0700 | [diff] [blame] | 625 | self.module.showwarning = print |
| 626 | with support.captured_output('stdout'): |
| 627 | self.module.warn('Warning!') |
Brett Cannon | fcc0527 | 2009-04-01 20:27:29 +0000 | [diff] [blame] | 628 | self.module.showwarning = 23 |
Brett Cannon | 52a7d98 | 2011-07-17 19:17:55 -0700 | [diff] [blame] | 629 | self.assertRaises(TypeError, self.module.warn, "Warning!") |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 630 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 631 | def test_show_warning_output(self): |
| 632 | # With showarning() missing, make sure that output is okay. |
| 633 | text = 'test show_warning' |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 634 | with original_warnings.catch_warnings(module=self.module): |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 635 | self.module.filterwarnings("always", category=UserWarning) |
| 636 | del self.module.showwarning |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 637 | with support.captured_output('stderr') as stream: |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 638 | warning_tests.inner(text) |
| 639 | result = stream.getvalue() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 640 | self.assertEqual(result.count('\n'), 2, |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 641 | "Too many newlines in %r" % result) |
| 642 | first_line, second_line = result.split('\n', 1) |
| 643 | expected_file = os.path.splitext(warning_tests.__file__)[0] + '.py' |
Neal Norwitz | 32dde22 | 2008-04-15 06:43:13 +0000 | [diff] [blame] | 644 | first_line_parts = first_line.rsplit(':', 3) |
| 645 | path, line, warning_class, message = first_line_parts |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 646 | line = int(line) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 647 | self.assertEqual(expected_file, path) |
| 648 | self.assertEqual(warning_class, ' ' + UserWarning.__name__) |
| 649 | self.assertEqual(message, ' ' + text) |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 650 | expected_line = ' ' + linecache.getline(path, line).strip() + '\n' |
| 651 | assert expected_line |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 652 | self.assertEqual(second_line, expected_line) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 653 | |
Victor Stinner | 8b0508e | 2011-07-04 02:43:09 +0200 | [diff] [blame] | 654 | def test_filename_none(self): |
| 655 | # issue #12467: race condition if a warning is emitted at shutdown |
| 656 | globals_dict = globals() |
| 657 | oldfile = globals_dict['__file__'] |
| 658 | try: |
Brett Cannon | 52a7d98 | 2011-07-17 19:17:55 -0700 | [diff] [blame] | 659 | catch = original_warnings.catch_warnings(record=True, |
| 660 | module=self.module) |
| 661 | with catch as w: |
Victor Stinner | 8b0508e | 2011-07-04 02:43:09 +0200 | [diff] [blame] | 662 | self.module.filterwarnings("always", category=UserWarning) |
| 663 | globals_dict['__file__'] = None |
| 664 | original_warnings.warn('test', UserWarning) |
Brett Cannon | 52a7d98 | 2011-07-17 19:17:55 -0700 | [diff] [blame] | 665 | self.assertTrue(len(w)) |
Victor Stinner | 8b0508e | 2011-07-04 02:43:09 +0200 | [diff] [blame] | 666 | finally: |
| 667 | globals_dict['__file__'] = oldfile |
| 668 | |
Serhiy Storchaka | 6059952 | 2014-12-10 22:59:55 +0200 | [diff] [blame] | 669 | def test_stderr_none(self): |
| 670 | rc, stdout, stderr = assert_python_ok("-c", |
| 671 | "import sys; sys.stderr = None; " |
| 672 | "import warnings; warnings.simplefilter('always'); " |
| 673 | "warnings.warn('Warning!')") |
| 674 | self.assertEqual(stdout, b'') |
| 675 | self.assertNotIn(b'Warning!', stderr) |
| 676 | self.assertNotIn(b'Error', stderr) |
| 677 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 678 | |
Ezio Melotti | 2688e81 | 2013-01-10 06:52:23 +0200 | [diff] [blame] | 679 | class WarningsDisplayTests(BaseTest): |
Christian Heimes | 5fb7c2a | 2007-12-24 08:52:31 +0000 | [diff] [blame] | 680 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 681 | """Test the displaying of warnings and the ability to overload functions |
| 682 | related to displaying warnings.""" |
| 683 | |
Christian Heimes | 5fb7c2a | 2007-12-24 08:52:31 +0000 | [diff] [blame] | 684 | def test_formatwarning(self): |
| 685 | message = "msg" |
| 686 | category = Warning |
| 687 | file_name = os.path.splitext(warning_tests.__file__)[0] + '.py' |
| 688 | line_num = 3 |
| 689 | file_line = linecache.getline(file_name, line_num).strip() |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 690 | format = "%s:%s: %s: %s\n %s\n" |
| 691 | expect = format % (file_name, line_num, category.__name__, message, |
| 692 | file_line) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 693 | self.assertEqual(expect, self.module.formatwarning(message, |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 694 | category, file_name, line_num)) |
| 695 | # Test the 'line' argument. |
| 696 | file_line += " for the win!" |
| 697 | expect = format % (file_name, line_num, category.__name__, message, |
| 698 | file_line) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 699 | self.assertEqual(expect, self.module.formatwarning(message, |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 700 | category, file_name, line_num, file_line)) |
Christian Heimes | 5fb7c2a | 2007-12-24 08:52:31 +0000 | [diff] [blame] | 701 | |
| 702 | def test_showwarning(self): |
| 703 | file_name = os.path.splitext(warning_tests.__file__)[0] + '.py' |
| 704 | line_num = 3 |
| 705 | expected_file_line = linecache.getline(file_name, line_num).strip() |
| 706 | message = 'msg' |
| 707 | category = Warning |
| 708 | file_object = StringIO() |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 709 | expect = self.module.formatwarning(message, category, file_name, |
| 710 | line_num) |
| 711 | self.module.showwarning(message, category, file_name, line_num, |
Christian Heimes | 5fb7c2a | 2007-12-24 08:52:31 +0000 | [diff] [blame] | 712 | file_object) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 713 | self.assertEqual(file_object.getvalue(), expect) |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 714 | # Test 'line' argument. |
| 715 | expected_file_line += "for the win!" |
| 716 | expect = self.module.formatwarning(message, category, file_name, |
| 717 | line_num, expected_file_line) |
| 718 | file_object = StringIO() |
| 719 | self.module.showwarning(message, category, file_name, line_num, |
| 720 | file_object, expected_file_line) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 721 | self.assertEqual(expect, file_object.getvalue()) |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 722 | |
Ezio Melotti | 2688e81 | 2013-01-10 06:52:23 +0200 | [diff] [blame] | 723 | class CWarningsDisplayTests(WarningsDisplayTests, unittest.TestCase): |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 724 | module = c_warnings |
| 725 | |
Ezio Melotti | 2688e81 | 2013-01-10 06:52:23 +0200 | [diff] [blame] | 726 | class PyWarningsDisplayTests(WarningsDisplayTests, unittest.TestCase): |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 727 | module = py_warnings |
Christian Heimes | 5fb7c2a | 2007-12-24 08:52:31 +0000 | [diff] [blame] | 728 | |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 729 | |
Brett Cannon | ec92e18 | 2008-09-02 02:46:59 +0000 | [diff] [blame] | 730 | class CatchWarningTests(BaseTest): |
Nick Coghlan | b130493 | 2008-07-13 12:25:08 +0000 | [diff] [blame] | 731 | |
Brett Cannon | ec92e18 | 2008-09-02 02:46:59 +0000 | [diff] [blame] | 732 | """Test catch_warnings().""" |
| 733 | |
| 734 | def test_catch_warnings_restore(self): |
Nick Coghlan | b130493 | 2008-07-13 12:25:08 +0000 | [diff] [blame] | 735 | wmod = self.module |
| 736 | orig_filters = wmod.filters |
| 737 | orig_showwarning = wmod.showwarning |
Benjamin Peterson | fcf5d63 | 2008-10-16 23:24:44 +0000 | [diff] [blame] | 738 | # Ensure both showwarning and filters are restored when recording |
| 739 | with wmod.catch_warnings(module=wmod, record=True): |
Nick Coghlan | b130493 | 2008-07-13 12:25:08 +0000 | [diff] [blame] | 740 | wmod.filters = wmod.showwarning = object() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 741 | self.assertTrue(wmod.filters is orig_filters) |
| 742 | self.assertTrue(wmod.showwarning is orig_showwarning) |
Benjamin Peterson | fcf5d63 | 2008-10-16 23:24:44 +0000 | [diff] [blame] | 743 | # Same test, but with recording disabled |
| 744 | with wmod.catch_warnings(module=wmod, record=False): |
Nick Coghlan | b130493 | 2008-07-13 12:25:08 +0000 | [diff] [blame] | 745 | wmod.filters = wmod.showwarning = object() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 746 | self.assertTrue(wmod.filters is orig_filters) |
| 747 | self.assertTrue(wmod.showwarning is orig_showwarning) |
Nick Coghlan | b130493 | 2008-07-13 12:25:08 +0000 | [diff] [blame] | 748 | |
Brett Cannon | ec92e18 | 2008-09-02 02:46:59 +0000 | [diff] [blame] | 749 | def test_catch_warnings_recording(self): |
Nick Coghlan | b130493 | 2008-07-13 12:25:08 +0000 | [diff] [blame] | 750 | wmod = self.module |
Benjamin Peterson | fcf5d63 | 2008-10-16 23:24:44 +0000 | [diff] [blame] | 751 | # Ensure warnings are recorded when requested |
| 752 | with wmod.catch_warnings(module=wmod, record=True) as w: |
Brett Cannon | ec92e18 | 2008-09-02 02:46:59 +0000 | [diff] [blame] | 753 | self.assertEqual(w, []) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 754 | self.assertTrue(type(w) is list) |
Nick Coghlan | b130493 | 2008-07-13 12:25:08 +0000 | [diff] [blame] | 755 | wmod.simplefilter("always") |
| 756 | wmod.warn("foo") |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 757 | self.assertEqual(str(w[-1].message), "foo") |
Nick Coghlan | b130493 | 2008-07-13 12:25:08 +0000 | [diff] [blame] | 758 | wmod.warn("bar") |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 759 | self.assertEqual(str(w[-1].message), "bar") |
Brett Cannon | ec92e18 | 2008-09-02 02:46:59 +0000 | [diff] [blame] | 760 | self.assertEqual(str(w[0].message), "foo") |
| 761 | self.assertEqual(str(w[1].message), "bar") |
Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 762 | del w[:] |
Brett Cannon | ec92e18 | 2008-09-02 02:46:59 +0000 | [diff] [blame] | 763 | self.assertEqual(w, []) |
Benjamin Peterson | fcf5d63 | 2008-10-16 23:24:44 +0000 | [diff] [blame] | 764 | # Ensure warnings are not recorded when not requested |
Nick Coghlan | b130493 | 2008-07-13 12:25:08 +0000 | [diff] [blame] | 765 | orig_showwarning = wmod.showwarning |
Benjamin Peterson | fcf5d63 | 2008-10-16 23:24:44 +0000 | [diff] [blame] | 766 | with wmod.catch_warnings(module=wmod, record=False) as w: |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 767 | self.assertTrue(w is None) |
| 768 | self.assertTrue(wmod.showwarning is orig_showwarning) |
Nick Coghlan | b130493 | 2008-07-13 12:25:08 +0000 | [diff] [blame] | 769 | |
Benjamin Peterson | fcf5d63 | 2008-10-16 23:24:44 +0000 | [diff] [blame] | 770 | def test_catch_warnings_reentry_guard(self): |
| 771 | wmod = self.module |
| 772 | # Ensure catch_warnings is protected against incorrect usage |
| 773 | x = wmod.catch_warnings(module=wmod, record=True) |
| 774 | self.assertRaises(RuntimeError, x.__exit__) |
| 775 | with x: |
| 776 | self.assertRaises(RuntimeError, x.__enter__) |
| 777 | # Same test, but with recording disabled |
| 778 | x = wmod.catch_warnings(module=wmod, record=False) |
| 779 | self.assertRaises(RuntimeError, x.__exit__) |
| 780 | with x: |
| 781 | self.assertRaises(RuntimeError, x.__enter__) |
| 782 | |
| 783 | def test_catch_warnings_defaults(self): |
| 784 | wmod = self.module |
| 785 | orig_filters = wmod.filters |
| 786 | orig_showwarning = wmod.showwarning |
| 787 | # Ensure default behaviour is not to record warnings |
| 788 | with wmod.catch_warnings(module=wmod) as w: |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 789 | self.assertTrue(w is None) |
| 790 | self.assertTrue(wmod.showwarning is orig_showwarning) |
| 791 | self.assertTrue(wmod.filters is not orig_filters) |
| 792 | self.assertTrue(wmod.filters is orig_filters) |
Benjamin Peterson | fcf5d63 | 2008-10-16 23:24:44 +0000 | [diff] [blame] | 793 | if wmod is sys.modules['warnings']: |
| 794 | # Ensure the default module is this one |
| 795 | with wmod.catch_warnings() as w: |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 796 | self.assertTrue(w is None) |
| 797 | self.assertTrue(wmod.showwarning is orig_showwarning) |
| 798 | self.assertTrue(wmod.filters is not orig_filters) |
| 799 | self.assertTrue(wmod.filters is orig_filters) |
Benjamin Peterson | fcf5d63 | 2008-10-16 23:24:44 +0000 | [diff] [blame] | 800 | |
| 801 | def test_check_warnings(self): |
| 802 | # Explicit tests for the test.support convenience wrapper |
| 803 | wmod = self.module |
Florent Xicluna | 53b506be | 2010-03-18 20:00:57 +0000 | [diff] [blame] | 804 | if wmod is not sys.modules['warnings']: |
Zachary Ware | 9fe6d86 | 2013-12-08 00:20:35 -0600 | [diff] [blame] | 805 | self.skipTest('module to test is not loaded warnings module') |
Florent Xicluna | 53b506be | 2010-03-18 20:00:57 +0000 | [diff] [blame] | 806 | with support.check_warnings(quiet=False) as w: |
| 807 | self.assertEqual(w.warnings, []) |
| 808 | wmod.simplefilter("always") |
| 809 | wmod.warn("foo") |
| 810 | self.assertEqual(str(w.message), "foo") |
| 811 | wmod.warn("bar") |
| 812 | self.assertEqual(str(w.message), "bar") |
| 813 | self.assertEqual(str(w.warnings[0].message), "foo") |
| 814 | self.assertEqual(str(w.warnings[1].message), "bar") |
| 815 | w.reset() |
| 816 | self.assertEqual(w.warnings, []) |
| 817 | |
| 818 | with support.check_warnings(): |
| 819 | # defaults to quiet=True without argument |
| 820 | pass |
| 821 | with support.check_warnings(('foo', UserWarning)): |
| 822 | wmod.warn("foo") |
| 823 | |
| 824 | with self.assertRaises(AssertionError): |
| 825 | with support.check_warnings(('', RuntimeWarning)): |
| 826 | # defaults to quiet=False with argument |
| 827 | pass |
| 828 | with self.assertRaises(AssertionError): |
| 829 | with support.check_warnings(('foo', RuntimeWarning)): |
Benjamin Peterson | fcf5d63 | 2008-10-16 23:24:44 +0000 | [diff] [blame] | 830 | wmod.warn("foo") |
Benjamin Peterson | fcf5d63 | 2008-10-16 23:24:44 +0000 | [diff] [blame] | 831 | |
Ezio Melotti | 2688e81 | 2013-01-10 06:52:23 +0200 | [diff] [blame] | 832 | class CCatchWarningTests(CatchWarningTests, unittest.TestCase): |
Nick Coghlan | b130493 | 2008-07-13 12:25:08 +0000 | [diff] [blame] | 833 | module = c_warnings |
| 834 | |
Ezio Melotti | 2688e81 | 2013-01-10 06:52:23 +0200 | [diff] [blame] | 835 | class PyCatchWarningTests(CatchWarningTests, unittest.TestCase): |
Nick Coghlan | b130493 | 2008-07-13 12:25:08 +0000 | [diff] [blame] | 836 | module = py_warnings |
| 837 | |
Christian Heimes | 5fb7c2a | 2007-12-24 08:52:31 +0000 | [diff] [blame] | 838 | |
Philip Jenvey | 0805ca3 | 2010-04-07 04:04:10 +0000 | [diff] [blame] | 839 | class EnvironmentVariableTests(BaseTest): |
| 840 | |
| 841 | def test_single_warning(self): |
Antoine Pitrou | bb08b36 | 2014-01-29 23:44:05 +0100 | [diff] [blame] | 842 | rc, stdout, stderr = assert_python_ok("-c", |
| 843 | "import sys; sys.stdout.write(str(sys.warnoptions))", |
| 844 | PYTHONWARNINGS="ignore::DeprecationWarning") |
| 845 | self.assertEqual(stdout, b"['ignore::DeprecationWarning']") |
Philip Jenvey | 0805ca3 | 2010-04-07 04:04:10 +0000 | [diff] [blame] | 846 | |
| 847 | def test_comma_separated_warnings(self): |
Antoine Pitrou | bb08b36 | 2014-01-29 23:44:05 +0100 | [diff] [blame] | 848 | rc, stdout, stderr = assert_python_ok("-c", |
| 849 | "import sys; sys.stdout.write(str(sys.warnoptions))", |
| 850 | PYTHONWARNINGS="ignore::DeprecationWarning,ignore::UnicodeWarning") |
| 851 | self.assertEqual(stdout, |
| 852 | b"['ignore::DeprecationWarning', 'ignore::UnicodeWarning']") |
Philip Jenvey | 0805ca3 | 2010-04-07 04:04:10 +0000 | [diff] [blame] | 853 | |
| 854 | def test_envvar_and_command_line(self): |
Antoine Pitrou | bb08b36 | 2014-01-29 23:44:05 +0100 | [diff] [blame] | 855 | rc, stdout, stderr = assert_python_ok("-Wignore::UnicodeWarning", "-c", |
| 856 | "import sys; sys.stdout.write(str(sys.warnoptions))", |
| 857 | PYTHONWARNINGS="ignore::DeprecationWarning") |
| 858 | self.assertEqual(stdout, |
Antoine Pitrou | 6999441 | 2014-04-29 00:56:08 +0200 | [diff] [blame] | 859 | b"['ignore::DeprecationWarning', 'ignore::UnicodeWarning']") |
| 860 | |
| 861 | def test_conflicting_envvar_and_command_line(self): |
| 862 | rc, stdout, stderr = assert_python_failure("-Werror::DeprecationWarning", "-c", |
| 863 | "import sys, warnings; sys.stdout.write(str(sys.warnoptions)); " |
| 864 | "warnings.warn('Message', DeprecationWarning)", |
| 865 | PYTHONWARNINGS="default::DeprecationWarning") |
| 866 | self.assertEqual(stdout, |
| 867 | b"['default::DeprecationWarning', 'error::DeprecationWarning']") |
| 868 | self.assertEqual(stderr.splitlines(), |
| 869 | [b"Traceback (most recent call last):", |
| 870 | b" File \"<string>\", line 1, in <module>", |
| 871 | b"DeprecationWarning: Message"]) |
Philip Jenvey | 0805ca3 | 2010-04-07 04:04:10 +0000 | [diff] [blame] | 872 | |
Philip Jenvey | e53de3d | 2010-04-14 03:01:39 +0000 | [diff] [blame] | 873 | @unittest.skipUnless(sys.getfilesystemencoding() != 'ascii', |
| 874 | 'requires non-ascii filesystemencoding') |
| 875 | def test_nonascii(self): |
Antoine Pitrou | bb08b36 | 2014-01-29 23:44:05 +0100 | [diff] [blame] | 876 | rc, stdout, stderr = assert_python_ok("-c", |
| 877 | "import sys; sys.stdout.write(str(sys.warnoptions))", |
| 878 | PYTHONIOENCODING="utf-8", |
| 879 | PYTHONWARNINGS="ignore:DeprecaciónWarning") |
| 880 | self.assertEqual(stdout, |
| 881 | "['ignore:DeprecaciónWarning']".encode('utf-8')) |
Philip Jenvey | e53de3d | 2010-04-14 03:01:39 +0000 | [diff] [blame] | 882 | |
Ezio Melotti | 2688e81 | 2013-01-10 06:52:23 +0200 | [diff] [blame] | 883 | class CEnvironmentVariableTests(EnvironmentVariableTests, unittest.TestCase): |
Philip Jenvey | 0805ca3 | 2010-04-07 04:04:10 +0000 | [diff] [blame] | 884 | module = c_warnings |
| 885 | |
Ezio Melotti | 2688e81 | 2013-01-10 06:52:23 +0200 | [diff] [blame] | 886 | class PyEnvironmentVariableTests(EnvironmentVariableTests, unittest.TestCase): |
Philip Jenvey | 0805ca3 | 2010-04-07 04:04:10 +0000 | [diff] [blame] | 887 | module = py_warnings |
| 888 | |
| 889 | |
Victor Stinner | 7d79b8b | 2010-05-19 20:40:50 +0000 | [diff] [blame] | 890 | class BootstrapTest(unittest.TestCase): |
| 891 | def test_issue_8766(self): |
| 892 | # "import encodings" emits a warning whereas the warnings is not loaded |
Ezio Melotti | 42da663 | 2011-03-15 05:18:48 +0200 | [diff] [blame] | 893 | # or not completely loaded (warnings imports indirectly encodings by |
Victor Stinner | 7d79b8b | 2010-05-19 20:40:50 +0000 | [diff] [blame] | 894 | # importing linecache) yet |
| 895 | with support.temp_cwd() as cwd, support.temp_cwd('encodings'): |
Victor Stinner | 7d79b8b | 2010-05-19 20:40:50 +0000 | [diff] [blame] | 896 | # encodings loaded by initfsencoding() |
Antoine Pitrou | bb08b36 | 2014-01-29 23:44:05 +0100 | [diff] [blame] | 897 | assert_python_ok('-c', 'pass', PYTHONPATH=cwd) |
Victor Stinner | 7d79b8b | 2010-05-19 20:40:50 +0000 | [diff] [blame] | 898 | |
| 899 | # Use -W to load warnings module at startup |
Antoine Pitrou | bb08b36 | 2014-01-29 23:44:05 +0100 | [diff] [blame] | 900 | assert_python_ok('-c', 'pass', '-W', 'always', PYTHONPATH=cwd) |
Victor Stinner | 7d79b8b | 2010-05-19 20:40:50 +0000 | [diff] [blame] | 901 | |
Victor Stinner | d1b4899 | 2013-10-28 19:16:21 +0100 | [diff] [blame] | 902 | class FinalizationTest(unittest.TestCase): |
| 903 | def test_finalization(self): |
| 904 | # Issue #19421: warnings.warn() should not crash |
| 905 | # during Python finalization |
| 906 | code = """ |
| 907 | import warnings |
| 908 | warn = warnings.warn |
| 909 | |
| 910 | class A: |
| 911 | def __del__(self): |
| 912 | warn("test") |
| 913 | |
| 914 | a=A() |
| 915 | """ |
| 916 | rc, out, err = assert_python_ok("-c", code) |
| 917 | # note: "__main__" filename is not correct, it should be the name |
| 918 | # of the script |
| 919 | self.assertEqual(err, b'__main__:7: UserWarning: test') |
| 920 | |
Ezio Melotti | 2688e81 | 2013-01-10 06:52:23 +0200 | [diff] [blame] | 921 | |
| 922 | def setUpModule(): |
Christian Heimes | dae2a89 | 2008-04-19 00:55:37 +0000 | [diff] [blame] | 923 | py_warnings.onceregistry.clear() |
| 924 | c_warnings.onceregistry.clear() |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 925 | |
Ezio Melotti | 2688e81 | 2013-01-10 06:52:23 +0200 | [diff] [blame] | 926 | tearDownModule = setUpModule |
Raymond Hettinger | d6f6e50 | 2003-07-13 08:37:40 +0000 | [diff] [blame] | 927 | |
| 928 | if __name__ == "__main__": |
Ezio Melotti | 2688e81 | 2013-01-10 06:52:23 +0200 | [diff] [blame] | 929 | unittest.main() |