OLD | NEW |
1 from contextlib import contextmanager | 1 from contextlib import contextmanager |
2 import linecache | 2 import linecache |
3 import os | 3 import os |
4 from io import StringIO | 4 from io import StringIO |
5 import sys | 5 import sys |
6 import unittest | 6 import unittest |
7 import subprocess | 7 import subprocess |
8 from test import support | 8 from test import support |
9 from test.script_helper import assert_python_ok | 9 from test.script_helper import assert_python_ok |
10 | 10 |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
73 self.module.filterwarnings("error", category=UserWarning) | 73 self.module.filterwarnings("error", category=UserWarning) |
74 self.assertRaises(UserWarning, self.module.warn, | 74 self.assertRaises(UserWarning, self.module.warn, |
75 "FilterTests.test_error") | 75 "FilterTests.test_error") |
76 | 76 |
77 def test_ignore(self): | 77 def test_ignore(self): |
78 with original_warnings.catch_warnings(record=True, | 78 with original_warnings.catch_warnings(record=True, |
79 module=self.module) as w: | 79 module=self.module) as w: |
80 self.module.resetwarnings() | 80 self.module.resetwarnings() |
81 self.module.filterwarnings("ignore", category=UserWarning) | 81 self.module.filterwarnings("ignore", category=UserWarning) |
82 self.module.warn("FilterTests.test_ignore", UserWarning) | 82 self.module.warn("FilterTests.test_ignore", UserWarning) |
83 self.assertEquals(len(w), 0) | 83 self.assertEqual(len(w), 0) |
84 | 84 |
85 def test_always(self): | 85 def test_always(self): |
86 with original_warnings.catch_warnings(record=True, | 86 with original_warnings.catch_warnings(record=True, |
87 module=self.module) as w: | 87 module=self.module) as w: |
88 self.module.resetwarnings() | 88 self.module.resetwarnings() |
89 self.module.filterwarnings("always", category=UserWarning) | 89 self.module.filterwarnings("always", category=UserWarning) |
90 message = "FilterTests.test_always" | 90 message = "FilterTests.test_always" |
91 self.module.warn(message, UserWarning) | 91 self.module.warn(message, UserWarning) |
92 self.assertTrue(message, w[-1].message) | 92 self.assertTrue(message, w[-1].message) |
93 self.module.warn(message, UserWarning) | 93 self.module.warn(message, UserWarning) |
94 self.assertTrue(w[-1].message, message) | 94 self.assertTrue(w[-1].message, message) |
95 | 95 |
96 def test_default(self): | 96 def test_default(self): |
97 with original_warnings.catch_warnings(record=True, | 97 with original_warnings.catch_warnings(record=True, |
98 module=self.module) as w: | 98 module=self.module) as w: |
99 self.module.resetwarnings() | 99 self.module.resetwarnings() |
100 self.module.filterwarnings("default", category=UserWarning) | 100 self.module.filterwarnings("default", category=UserWarning) |
101 message = UserWarning("FilterTests.test_default") | 101 message = UserWarning("FilterTests.test_default") |
102 for x in range(2): | 102 for x in range(2): |
103 self.module.warn(message, UserWarning) | 103 self.module.warn(message, UserWarning) |
104 if x == 0: | 104 if x == 0: |
105 self.assertEquals(w[-1].message, message) | 105 self.assertEqual(w[-1].message, message) |
106 del w[:] | 106 del w[:] |
107 elif x == 1: | 107 elif x == 1: |
108 self.assertEquals(len(w), 0) | 108 self.assertEqual(len(w), 0) |
109 else: | 109 else: |
110 raise ValueError("loop variant unhandled") | 110 raise ValueError("loop variant unhandled") |
111 | 111 |
112 def test_module(self): | 112 def test_module(self): |
113 with original_warnings.catch_warnings(record=True, | 113 with original_warnings.catch_warnings(record=True, |
114 module=self.module) as w: | 114 module=self.module) as w: |
115 self.module.resetwarnings() | 115 self.module.resetwarnings() |
116 self.module.filterwarnings("module", category=UserWarning) | 116 self.module.filterwarnings("module", category=UserWarning) |
117 message = UserWarning("FilterTests.test_module") | 117 message = UserWarning("FilterTests.test_module") |
118 self.module.warn(message, UserWarning) | 118 self.module.warn(message, UserWarning) |
119 self.assertEquals(w[-1].message, message) | 119 self.assertEqual(w[-1].message, message) |
120 del w[:] | 120 del w[:] |
121 self.module.warn(message, UserWarning) | 121 self.module.warn(message, UserWarning) |
122 self.assertEquals(len(w), 0) | 122 self.assertEqual(len(w), 0) |
123 | 123 |
124 def test_once(self): | 124 def test_once(self): |
125 with original_warnings.catch_warnings(record=True, | 125 with original_warnings.catch_warnings(record=True, |
126 module=self.module) as w: | 126 module=self.module) as w: |
127 self.module.resetwarnings() | 127 self.module.resetwarnings() |
128 self.module.filterwarnings("once", category=UserWarning) | 128 self.module.filterwarnings("once", category=UserWarning) |
129 message = UserWarning("FilterTests.test_once") | 129 message = UserWarning("FilterTests.test_once") |
130 self.module.warn_explicit(message, UserWarning, "test_warnings.py", | 130 self.module.warn_explicit(message, UserWarning, "test_warnings.py", |
131 42) | 131 42) |
132 self.assertEquals(w[-1].message, message) | 132 self.assertEqual(w[-1].message, message) |
133 del w[:] | 133 del w[:] |
134 self.module.warn_explicit(message, UserWarning, "test_warnings.py", | 134 self.module.warn_explicit(message, UserWarning, "test_warnings.py", |
135 13) | 135 13) |
136 self.assertEquals(len(w), 0) | 136 self.assertEqual(len(w), 0) |
137 self.module.warn_explicit(message, UserWarning, "test_warnings2.py", | 137 self.module.warn_explicit(message, UserWarning, "test_warnings2.py", |
138 42) | 138 42) |
139 self.assertEquals(len(w), 0) | 139 self.assertEqual(len(w), 0) |
140 | 140 |
141 def test_inheritance(self): | 141 def test_inheritance(self): |
142 with original_warnings.catch_warnings(module=self.module) as w: | 142 with original_warnings.catch_warnings(module=self.module) as w: |
143 self.module.resetwarnings() | 143 self.module.resetwarnings() |
144 self.module.filterwarnings("error", category=Warning) | 144 self.module.filterwarnings("error", category=Warning) |
145 self.assertRaises(UserWarning, self.module.warn, | 145 self.assertRaises(UserWarning, self.module.warn, |
146 "FilterTests.test_inheritance", UserWarning) | 146 "FilterTests.test_inheritance", UserWarning) |
147 | 147 |
148 def test_ordering(self): | 148 def test_ordering(self): |
149 with original_warnings.catch_warnings(record=True, | 149 with original_warnings.catch_warnings(record=True, |
150 module=self.module) as w: | 150 module=self.module) as w: |
151 self.module.resetwarnings() | 151 self.module.resetwarnings() |
152 self.module.filterwarnings("ignore", category=UserWarning) | 152 self.module.filterwarnings("ignore", category=UserWarning) |
153 self.module.filterwarnings("error", category=UserWarning, | 153 self.module.filterwarnings("error", category=UserWarning, |
154 append=True) | 154 append=True) |
155 del w[:] | 155 del w[:] |
156 try: | 156 try: |
157 self.module.warn("FilterTests.test_ordering", UserWarning) | 157 self.module.warn("FilterTests.test_ordering", UserWarning) |
158 except UserWarning: | 158 except UserWarning: |
159 self.fail("order handling for actions failed") | 159 self.fail("order handling for actions failed") |
160 self.assertEquals(len(w), 0) | 160 self.assertEqual(len(w), 0) |
161 | 161 |
162 def test_filterwarnings(self): | 162 def test_filterwarnings(self): |
163 # Test filterwarnings(). | 163 # Test filterwarnings(). |
164 # Implicitly also tests resetwarnings(). | 164 # Implicitly also tests resetwarnings(). |
165 with original_warnings.catch_warnings(record=True, | 165 with original_warnings.catch_warnings(record=True, |
166 module=self.module) as w: | 166 module=self.module) as w: |
167 self.module.filterwarnings("error", "", Warning, "", 0) | 167 self.module.filterwarnings("error", "", Warning, "", 0) |
168 self.assertRaises(UserWarning, self.module.warn, 'convert to error') | 168 self.assertRaises(UserWarning, self.module.warn, 'convert to error') |
169 | 169 |
170 self.module.resetwarnings() | 170 self.module.resetwarnings() |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
210 # Issue 3639 | 210 # Issue 3639 |
211 def test_warn_nonstandard_types(self): | 211 def test_warn_nonstandard_types(self): |
212 # warn() should handle non-standard types without issue. | 212 # warn() should handle non-standard types without issue. |
213 for ob in (Warning, None, 42): | 213 for ob in (Warning, None, 42): |
214 with original_warnings.catch_warnings(record=True, | 214 with original_warnings.catch_warnings(record=True, |
215 module=self.module) as w: | 215 module=self.module) as w: |
216 self.module.simplefilter("once") | 216 self.module.simplefilter("once") |
217 self.module.warn(ob) | 217 self.module.warn(ob) |
218 # Don't directly compare objects since | 218 # Don't directly compare objects since |
219 # ``Warning() != Warning()``. | 219 # ``Warning() != Warning()``. |
220 self.assertEquals(str(w[-1].message), str(UserWarning(ob))) | 220 self.assertEqual(str(w[-1].message), str(UserWarning(ob))) |
221 | 221 |
222 def test_filename(self): | 222 def test_filename(self): |
223 with warnings_state(self.module): | 223 with warnings_state(self.module): |
224 with original_warnings.catch_warnings(record=True, | 224 with original_warnings.catch_warnings(record=True, |
225 module=self.module) as w: | 225 module=self.module) as w: |
226 warning_tests.inner("spam1") | 226 warning_tests.inner("spam1") |
227 self.assertEqual(os.path.basename(w[-1].filename), | 227 self.assertEqual(os.path.basename(w[-1].filename), |
228 "warning_tests.py") | 228 "warning_tests.py") |
229 warning_tests.outer("spam2") | 229 warning_tests.outer("spam2") |
230 self.assertEqual(os.path.basename(w[-1].filename), | 230 self.assertEqual(os.path.basename(w[-1].filename), |
(...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
441 original_registry = self.module.onceregistry | 441 original_registry = self.module.onceregistry |
442 __warningregistry__ = {} | 442 __warningregistry__ = {} |
443 with original_warnings.catch_warnings(record=True, | 443 with original_warnings.catch_warnings(record=True, |
444 module=self.module) as w: | 444 module=self.module) as w: |
445 self.module.resetwarnings() | 445 self.module.resetwarnings() |
446 self.module.filterwarnings("once", category=UserWarning) | 446 self.module.filterwarnings("once", category=UserWarning) |
447 self.module.warn_explicit(message, UserWarning, "file", 42) | 447 self.module.warn_explicit(message, UserWarning, "file", 42) |
448 self.assertEqual(w[-1].message, message) | 448 self.assertEqual(w[-1].message, message) |
449 del w[:] | 449 del w[:] |
450 self.module.warn_explicit(message, UserWarning, "file", 42) | 450 self.module.warn_explicit(message, UserWarning, "file", 42) |
451 self.assertEquals(len(w), 0) | 451 self.assertEqual(len(w), 0) |
452 # Test the resetting of onceregistry. | 452 # Test the resetting of onceregistry. |
453 self.module.onceregistry = {} | 453 self.module.onceregistry = {} |
454 __warningregistry__ = {} | 454 __warningregistry__ = {} |
455 self.module.warn('onceregistry test') | 455 self.module.warn('onceregistry test') |
456 self.assertEqual(w[-1].message.args, message.args) | 456 self.assertEqual(w[-1].message.args, message.args) |
457 # Removal of onceregistry is okay. | 457 # Removal of onceregistry is okay. |
458 del w[:] | 458 del w[:] |
459 del self.module.onceregistry | 459 del self.module.onceregistry |
460 __warningregistry__ = {} | 460 __warningregistry__ = {} |
461 self.module.warn_explicit(message, UserWarning, "file", 42) | 461 self.module.warn_explicit(message, UserWarning, "file", 42) |
462 self.assertEquals(len(w), 0) | 462 self.assertEqual(len(w), 0) |
463 finally: | 463 finally: |
464 self.module.onceregistry = original_registry | 464 self.module.onceregistry = original_registry |
465 | 465 |
466 def test_default_action(self): | 466 def test_default_action(self): |
467 # Replacing or removing defaultaction should be okay. | 467 # Replacing or removing defaultaction should be okay. |
468 message = UserWarning("defaultaction test") | 468 message = UserWarning("defaultaction test") |
469 original = self.module.defaultaction | 469 original = self.module.defaultaction |
470 try: | 470 try: |
471 with original_warnings.catch_warnings(record=True, | 471 with original_warnings.catch_warnings(record=True, |
472 module=self.module) as w: | 472 module=self.module) as w: |
(...skipping 311 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
784 _WarningsTests, | 784 _WarningsTests, |
785 CWarningsDisplayTests, PyWarningsDisplayTests, | 785 CWarningsDisplayTests, PyWarningsDisplayTests, |
786 CCatchWarningTests, PyCatchWarningTests, | 786 CCatchWarningTests, PyCatchWarningTests, |
787 CEnvironmentVariableTests, PyEnvironmentVariableTests, | 787 CEnvironmentVariableTests, PyEnvironmentVariableTests, |
788 BootstrapTest, | 788 BootstrapTest, |
789 ) | 789 ) |
790 | 790 |
791 | 791 |
792 if __name__ == "__main__": | 792 if __name__ == "__main__": |
793 test_main() | 793 test_main() |
OLD | NEW |