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