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