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