blob: 10076af3aab75a8f8bcd11320fe4decd0cc79db1 [file] [log] [blame]
Christian Heimes33fe8092008-04-13 13:53:33 +00001from contextlib import contextmanager
Christian Heimes5fb7c2a2007-12-24 08:52:31 +00002import linecache
Raymond Hettingerdc9dcf12003-07-13 06:15:11 +00003import os
Christian Heimes5fb7c2a2007-12-24 08:52:31 +00004from io import StringIO
Guido van Rossum61e21b52007-08-20 19:06:03 +00005import sys
Raymond Hettingerd6f6e502003-07-13 08:37:40 +00006import unittest
Benjamin Petersonee8712c2008-05-20 21:35:26 +00007from test import support
Antoine Pitroucf9f9802010-11-10 13:55:25 +00008from test.script_helper import assert_python_ok
Jeremy Hylton85014662003-07-11 15:37:59 +00009
Guido van Rossum805365e2007-05-07 22:24:25 +000010from test import warning_tests
Jeremy Hylton85014662003-07-11 15:37:59 +000011
Christian Heimes33fe8092008-04-13 13:53:33 +000012import warnings as original_warnings
Jeremy Hylton85014662003-07-11 15:37:59 +000013
Nick Coghlan47384702009-04-22 16:13:36 +000014py_warnings = support.import_fresh_module('warnings', blocked=['_warnings'])
15c_warnings = support.import_fresh_module('warnings', fresh=['_warnings'])
Christian Heimes33fe8092008-04-13 13:53:33 +000016
17@contextmanager
18def warnings_state(module):
19 """Use a specific warnings implementation in warning_tests."""
20 global __warningregistry__
21 for to_clear in (sys, warning_tests):
22 try:
23 to_clear.__warningregistry__.clear()
24 except AttributeError:
25 pass
26 try:
27 __warningregistry__.clear()
28 except NameError:
29 pass
30 original_warnings = warning_tests.warnings
Florent Xiclunafd1b0932010-03-28 00:25:02 +000031 original_filters = module.filters
Christian Heimes33fe8092008-04-13 13:53:33 +000032 try:
Florent Xiclunafd1b0932010-03-28 00:25:02 +000033 module.filters = original_filters[:]
34 module.simplefilter("once")
Christian Heimes33fe8092008-04-13 13:53:33 +000035 warning_tests.warnings = module
36 yield
37 finally:
38 warning_tests.warnings = original_warnings
Florent Xiclunafd1b0932010-03-28 00:25:02 +000039 module.filters = original_filters
Christian Heimes33fe8092008-04-13 13:53:33 +000040
41
Ezio Melotti2688e812013-01-10 06:52:23 +020042class BaseTest:
Christian Heimes33fe8092008-04-13 13:53:33 +000043
44 """Basic bookkeeping required for testing."""
45
46 def setUp(self):
47 # The __warningregistry__ needs to be in a pristine state for tests
48 # to work properly.
49 if '__warningregistry__' in globals():
50 del globals()['__warningregistry__']
51 if hasattr(warning_tests, '__warningregistry__'):
52 del warning_tests.__warningregistry__
53 if hasattr(sys, '__warningregistry__'):
54 del sys.__warningregistry__
55 # The 'warnings' module must be explicitly set so that the proper
56 # interaction between _warnings and 'warnings' can be controlled.
57 sys.modules['warnings'] = self.module
58 super(BaseTest, self).setUp()
59
60 def tearDown(self):
61 sys.modules['warnings'] = original_warnings
62 super(BaseTest, self).tearDown()
63
64
Ezio Melotti2688e812013-01-10 06:52:23 +020065class FilterTests(BaseTest):
Christian Heimes33fe8092008-04-13 13:53:33 +000066
67 """Testing the filtering functionality."""
68
69 def test_error(self):
Brett Cannon1cd02472008-09-09 01:52:27 +000070 with original_warnings.catch_warnings(module=self.module) as w:
Christian Heimes33fe8092008-04-13 13:53:33 +000071 self.module.resetwarnings()
72 self.module.filterwarnings("error", category=UserWarning)
73 self.assertRaises(UserWarning, self.module.warn,
74 "FilterTests.test_error")
75
76 def test_ignore(self):
Brett Cannon1cd02472008-09-09 01:52:27 +000077 with original_warnings.catch_warnings(record=True,
78 module=self.module) as w:
Christian Heimes33fe8092008-04-13 13:53:33 +000079 self.module.resetwarnings()
80 self.module.filterwarnings("ignore", category=UserWarning)
81 self.module.warn("FilterTests.test_ignore", UserWarning)
Ezio Melottib3aedd42010-11-20 19:04:17 +000082 self.assertEqual(len(w), 0)
Christian Heimes33fe8092008-04-13 13:53:33 +000083
84 def test_always(self):
Brett Cannon1cd02472008-09-09 01:52:27 +000085 with original_warnings.catch_warnings(record=True,
86 module=self.module) as w:
Christian Heimes33fe8092008-04-13 13:53:33 +000087 self.module.resetwarnings()
88 self.module.filterwarnings("always", category=UserWarning)
89 message = "FilterTests.test_always"
90 self.module.warn(message, UserWarning)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000091 self.assertTrue(message, w[-1].message)
Christian Heimes33fe8092008-04-13 13:53:33 +000092 self.module.warn(message, UserWarning)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000093 self.assertTrue(w[-1].message, message)
Christian Heimes33fe8092008-04-13 13:53:33 +000094
95 def test_default(self):
Brett Cannon1cd02472008-09-09 01:52:27 +000096 with original_warnings.catch_warnings(record=True,
97 module=self.module) as w:
Christian Heimes33fe8092008-04-13 13:53:33 +000098 self.module.resetwarnings()
99 self.module.filterwarnings("default", category=UserWarning)
100 message = UserWarning("FilterTests.test_default")
101 for x in range(2):
102 self.module.warn(message, UserWarning)
103 if x == 0:
Ezio Melottib3aedd42010-11-20 19:04:17 +0000104 self.assertEqual(w[-1].message, message)
Brett Cannon1cd02472008-09-09 01:52:27 +0000105 del w[:]
Christian Heimes33fe8092008-04-13 13:53:33 +0000106 elif x == 1:
Ezio Melottib3aedd42010-11-20 19:04:17 +0000107 self.assertEqual(len(w), 0)
Christian Heimes33fe8092008-04-13 13:53:33 +0000108 else:
109 raise ValueError("loop variant unhandled")
110
111 def test_module(self):
Brett Cannon1cd02472008-09-09 01:52:27 +0000112 with original_warnings.catch_warnings(record=True,
113 module=self.module) as w:
Christian Heimes33fe8092008-04-13 13:53:33 +0000114 self.module.resetwarnings()
115 self.module.filterwarnings("module", category=UserWarning)
116 message = UserWarning("FilterTests.test_module")
117 self.module.warn(message, UserWarning)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000118 self.assertEqual(w[-1].message, message)
Brett Cannon1cd02472008-09-09 01:52:27 +0000119 del w[:]
Christian Heimes33fe8092008-04-13 13:53:33 +0000120 self.module.warn(message, UserWarning)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000121 self.assertEqual(len(w), 0)
Christian Heimes33fe8092008-04-13 13:53:33 +0000122
123 def test_once(self):
Brett Cannon1cd02472008-09-09 01:52:27 +0000124 with original_warnings.catch_warnings(record=True,
125 module=self.module) as w:
Christian Heimes33fe8092008-04-13 13:53:33 +0000126 self.module.resetwarnings()
127 self.module.filterwarnings("once", category=UserWarning)
128 message = UserWarning("FilterTests.test_once")
129 self.module.warn_explicit(message, UserWarning, "test_warnings.py",
130 42)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000131 self.assertEqual(w[-1].message, message)
Brett Cannon1cd02472008-09-09 01:52:27 +0000132 del w[:]
Christian Heimes33fe8092008-04-13 13:53:33 +0000133 self.module.warn_explicit(message, UserWarning, "test_warnings.py",
134 13)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000135 self.assertEqual(len(w), 0)
Christian Heimes33fe8092008-04-13 13:53:33 +0000136 self.module.warn_explicit(message, UserWarning, "test_warnings2.py",
137 42)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000138 self.assertEqual(len(w), 0)
Christian Heimes33fe8092008-04-13 13:53:33 +0000139
140 def test_inheritance(self):
Brett Cannon1cd02472008-09-09 01:52:27 +0000141 with original_warnings.catch_warnings(module=self.module) as w:
Christian Heimes33fe8092008-04-13 13:53:33 +0000142 self.module.resetwarnings()
143 self.module.filterwarnings("error", category=Warning)
144 self.assertRaises(UserWarning, self.module.warn,
145 "FilterTests.test_inheritance", UserWarning)
146
147 def test_ordering(self):
Brett Cannon1cd02472008-09-09 01:52:27 +0000148 with original_warnings.catch_warnings(record=True,
149 module=self.module) as w:
Christian Heimes33fe8092008-04-13 13:53:33 +0000150 self.module.resetwarnings()
151 self.module.filterwarnings("ignore", category=UserWarning)
152 self.module.filterwarnings("error", category=UserWarning,
153 append=True)
Brett Cannon1cd02472008-09-09 01:52:27 +0000154 del w[:]
Christian Heimes33fe8092008-04-13 13:53:33 +0000155 try:
156 self.module.warn("FilterTests.test_ordering", UserWarning)
157 except UserWarning:
158 self.fail("order handling for actions failed")
Ezio Melottib3aedd42010-11-20 19:04:17 +0000159 self.assertEqual(len(w), 0)
Christian Heimes33fe8092008-04-13 13:53:33 +0000160
161 def test_filterwarnings(self):
162 # Test filterwarnings().
163 # Implicitly also tests resetwarnings().
Brett Cannon1cd02472008-09-09 01:52:27 +0000164 with original_warnings.catch_warnings(record=True,
165 module=self.module) as w:
Christian Heimes33fe8092008-04-13 13:53:33 +0000166 self.module.filterwarnings("error", "", Warning, "", 0)
167 self.assertRaises(UserWarning, self.module.warn, 'convert to error')
168
169 self.module.resetwarnings()
170 text = 'handle normally'
171 self.module.warn(text)
Brett Cannon1cd02472008-09-09 01:52:27 +0000172 self.assertEqual(str(w[-1].message), text)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000173 self.assertTrue(w[-1].category is UserWarning)
Christian Heimes33fe8092008-04-13 13:53:33 +0000174
175 self.module.filterwarnings("ignore", "", Warning, "", 0)
176 text = 'filtered out'
177 self.module.warn(text)
Brett Cannon1cd02472008-09-09 01:52:27 +0000178 self.assertNotEqual(str(w[-1].message), text)
Christian Heimes33fe8092008-04-13 13:53:33 +0000179
180 self.module.resetwarnings()
181 self.module.filterwarnings("error", "hex*", Warning, "", 0)
182 self.assertRaises(UserWarning, self.module.warn, 'hex/oct')
183 text = 'nonmatching text'
184 self.module.warn(text)
Brett Cannon1cd02472008-09-09 01:52:27 +0000185 self.assertEqual(str(w[-1].message), text)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000186 self.assertTrue(w[-1].category is UserWarning)
Christian Heimes33fe8092008-04-13 13:53:33 +0000187
Ezio Melotti2688e812013-01-10 06:52:23 +0200188class CFilterTests(FilterTests, unittest.TestCase):
Christian Heimes33fe8092008-04-13 13:53:33 +0000189 module = c_warnings
190
Ezio Melotti2688e812013-01-10 06:52:23 +0200191class PyFilterTests(FilterTests, unittest.TestCase):
Christian Heimes33fe8092008-04-13 13:53:33 +0000192 module = py_warnings
193
194
Ezio Melotti2688e812013-01-10 06:52:23 +0200195class WarnTests(BaseTest):
Christian Heimes33fe8092008-04-13 13:53:33 +0000196
197 """Test warnings.warn() and warnings.warn_explicit()."""
198
199 def test_message(self):
Brett Cannon1cd02472008-09-09 01:52:27 +0000200 with original_warnings.catch_warnings(record=True,
201 module=self.module) as w:
Florent Xiclunafd1b0932010-03-28 00:25:02 +0000202 self.module.simplefilter("once")
Guido van Rossumd8faa362007-04-27 19:54:29 +0000203 for i in range(4):
Christian Heimes33fe8092008-04-13 13:53:33 +0000204 text = 'multi %d' %i # Different text on each call.
205 self.module.warn(text)
Brett Cannon1cd02472008-09-09 01:52:27 +0000206 self.assertEqual(str(w[-1].message), text)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000207 self.assertTrue(w[-1].category is UserWarning)
Raymond Hettingerd6f6e502003-07-13 08:37:40 +0000208
Brett Cannon54bd41d2008-09-02 04:01:42 +0000209 # Issue 3639
210 def test_warn_nonstandard_types(self):
211 # warn() should handle non-standard types without issue.
212 for ob in (Warning, None, 42):
Benjamin Petersonfcf5d632008-10-16 23:24:44 +0000213 with original_warnings.catch_warnings(record=True,
214 module=self.module) as w:
Florent Xiclunafd1b0932010-03-28 00:25:02 +0000215 self.module.simplefilter("once")
Brett Cannon54bd41d2008-09-02 04:01:42 +0000216 self.module.warn(ob)
217 # Don't directly compare objects since
218 # ``Warning() != Warning()``.
Ezio Melottib3aedd42010-11-20 19:04:17 +0000219 self.assertEqual(str(w[-1].message), str(UserWarning(ob)))
Brett Cannon54bd41d2008-09-02 04:01:42 +0000220
Guido van Rossumd8faa362007-04-27 19:54:29 +0000221 def test_filename(self):
Christian Heimes33fe8092008-04-13 13:53:33 +0000222 with warnings_state(self.module):
Brett Cannon1cd02472008-09-09 01:52:27 +0000223 with original_warnings.catch_warnings(record=True,
224 module=self.module) as w:
Christian Heimes33fe8092008-04-13 13:53:33 +0000225 warning_tests.inner("spam1")
Brett Cannon1cd02472008-09-09 01:52:27 +0000226 self.assertEqual(os.path.basename(w[-1].filename),
227 "warning_tests.py")
Christian Heimes33fe8092008-04-13 13:53:33 +0000228 warning_tests.outer("spam2")
Brett Cannon1cd02472008-09-09 01:52:27 +0000229 self.assertEqual(os.path.basename(w[-1].filename),
230 "warning_tests.py")
Guido van Rossumd8faa362007-04-27 19:54:29 +0000231
232 def test_stacklevel(self):
233 # Test stacklevel argument
234 # make sure all messages are different, so the warning won't be skipped
Christian Heimes33fe8092008-04-13 13:53:33 +0000235 with warnings_state(self.module):
Brett Cannon1cd02472008-09-09 01:52:27 +0000236 with original_warnings.catch_warnings(record=True,
237 module=self.module) as w:
Christian Heimes33fe8092008-04-13 13:53:33 +0000238 warning_tests.inner("spam3", stacklevel=1)
Brett Cannon1cd02472008-09-09 01:52:27 +0000239 self.assertEqual(os.path.basename(w[-1].filename),
240 "warning_tests.py")
Christian Heimes33fe8092008-04-13 13:53:33 +0000241 warning_tests.outer("spam4", stacklevel=1)
Brett Cannon1cd02472008-09-09 01:52:27 +0000242 self.assertEqual(os.path.basename(w[-1].filename),
243 "warning_tests.py")
Guido van Rossumd8faa362007-04-27 19:54:29 +0000244
Christian Heimes33fe8092008-04-13 13:53:33 +0000245 warning_tests.inner("spam5", stacklevel=2)
Brett Cannon1cd02472008-09-09 01:52:27 +0000246 self.assertEqual(os.path.basename(w[-1].filename),
247 "test_warnings.py")
Christian Heimes33fe8092008-04-13 13:53:33 +0000248 warning_tests.outer("spam6", stacklevel=2)
Brett Cannon1cd02472008-09-09 01:52:27 +0000249 self.assertEqual(os.path.basename(w[-1].filename),
250 "warning_tests.py")
Christian Heimes5d8da202008-05-06 13:58:24 +0000251 warning_tests.outer("spam6.5", stacklevel=3)
Brett Cannon1cd02472008-09-09 01:52:27 +0000252 self.assertEqual(os.path.basename(w[-1].filename),
253 "test_warnings.py")
Guido van Rossumd8faa362007-04-27 19:54:29 +0000254
Christian Heimes33fe8092008-04-13 13:53:33 +0000255 warning_tests.inner("spam7", stacklevel=9999)
Brett Cannon1cd02472008-09-09 01:52:27 +0000256 self.assertEqual(os.path.basename(w[-1].filename),
257 "sys")
Christian Heimes33fe8092008-04-13 13:53:33 +0000258
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000259 def test_missing_filename_not_main(self):
260 # If __file__ is not specified and __main__ is not the module name,
261 # then __file__ should be set to the module name.
262 filename = warning_tests.__file__
263 try:
264 del warning_tests.__file__
265 with warnings_state(self.module):
Brett Cannon1cd02472008-09-09 01:52:27 +0000266 with original_warnings.catch_warnings(record=True,
267 module=self.module) as w:
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000268 warning_tests.inner("spam8", stacklevel=1)
Brett Cannon1cd02472008-09-09 01:52:27 +0000269 self.assertEqual(w[-1].filename, warning_tests.__name__)
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000270 finally:
271 warning_tests.__file__ = filename
272
Serhiy Storchaka79080682013-11-03 21:31:18 +0200273 @unittest.skipUnless(hasattr(sys, 'argv'), 'test needs sys.argv')
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000274 def test_missing_filename_main_with_argv(self):
275 # If __file__ is not specified and the caller is __main__ and sys.argv
276 # exists, then use sys.argv[0] as the file.
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000277 filename = warning_tests.__file__
278 module_name = warning_tests.__name__
279 try:
280 del warning_tests.__file__
281 warning_tests.__name__ = '__main__'
282 with warnings_state(self.module):
Brett Cannon1cd02472008-09-09 01:52:27 +0000283 with original_warnings.catch_warnings(record=True,
284 module=self.module) as w:
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000285 warning_tests.inner('spam9', stacklevel=1)
Brett Cannon1cd02472008-09-09 01:52:27 +0000286 self.assertEqual(w[-1].filename, sys.argv[0])
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000287 finally:
288 warning_tests.__file__ = filename
289 warning_tests.__name__ = module_name
290
291 def test_missing_filename_main_without_argv(self):
292 # If __file__ is not specified, the caller is __main__, and sys.argv
293 # is not set, then '__main__' is the file name.
294 filename = warning_tests.__file__
295 module_name = warning_tests.__name__
296 argv = sys.argv
297 try:
298 del warning_tests.__file__
299 warning_tests.__name__ = '__main__'
300 del sys.argv
301 with warnings_state(self.module):
Brett Cannon1cd02472008-09-09 01:52:27 +0000302 with original_warnings.catch_warnings(record=True,
303 module=self.module) as w:
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000304 warning_tests.inner('spam10', stacklevel=1)
Brett Cannon1cd02472008-09-09 01:52:27 +0000305 self.assertEqual(w[-1].filename, '__main__')
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000306 finally:
307 warning_tests.__file__ = filename
308 warning_tests.__name__ = module_name
309 sys.argv = argv
310
Christian Heimesdaaf8ee2008-05-04 23:58:41 +0000311 def test_missing_filename_main_with_argv_empty_string(self):
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000312 # If __file__ is not specified, the caller is __main__, and sys.argv[0]
313 # is the empty string, then '__main__ is the file name.
314 # Tests issue 2743.
315 file_name = warning_tests.__file__
316 module_name = warning_tests.__name__
317 argv = sys.argv
318 try:
319 del warning_tests.__file__
320 warning_tests.__name__ = '__main__'
321 sys.argv = ['']
322 with warnings_state(self.module):
Brett Cannon1cd02472008-09-09 01:52:27 +0000323 with original_warnings.catch_warnings(record=True,
324 module=self.module) as w:
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000325 warning_tests.inner('spam11', stacklevel=1)
Brett Cannon1cd02472008-09-09 01:52:27 +0000326 self.assertEqual(w[-1].filename, '__main__')
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000327 finally:
328 warning_tests.__file__ = file_name
329 warning_tests.__name__ = module_name
330 sys.argv = argv
331
Brett Cannondb734912008-06-27 00:52:15 +0000332 def test_warn_explicit_type_errors(self):
Ezio Melotti13925002011-03-16 11:05:33 +0200333 # warn_explicit() should error out gracefully if it is given objects
Brett Cannondb734912008-06-27 00:52:15 +0000334 # of the wrong types.
335 # lineno is expected to be an integer.
336 self.assertRaises(TypeError, self.module.warn_explicit,
337 None, UserWarning, None, None)
338 # Either 'message' needs to be an instance of Warning or 'category'
339 # needs to be a subclass.
340 self.assertRaises(TypeError, self.module.warn_explicit,
341 None, None, None, 1)
342 # 'registry' must be a dict or None.
343 self.assertRaises((TypeError, AttributeError),
344 self.module.warn_explicit,
345 None, Warning, None, 1, registry=42)
346
Hirokazu Yamamoto1c0c0032009-07-17 06:55:42 +0000347 def test_bad_str(self):
348 # issue 6415
349 # Warnings instance with a bad format string for __str__ should not
350 # trigger a bus error.
351 class BadStrWarning(Warning):
352 """Warning with a bad format string for __str__."""
353 def __str__(self):
354 return ("A bad formatted string %(err)" %
355 {"err" : "there is no %(err)s"})
356
357 with self.assertRaises(ValueError):
358 self.module.warn(BadStrWarning())
359
360
Ezio Melotti2688e812013-01-10 06:52:23 +0200361class CWarnTests(WarnTests, unittest.TestCase):
Christian Heimes33fe8092008-04-13 13:53:33 +0000362 module = c_warnings
363
Nick Coghlanfce769e2009-04-11 14:30:59 +0000364 # As an early adopter, we sanity check the
365 # test.support.import_fresh_module utility function
366 def test_accelerated(self):
367 self.assertFalse(original_warnings is self.module)
368 self.assertFalse(hasattr(self.module.warn, '__code__'))
369
Ezio Melotti2688e812013-01-10 06:52:23 +0200370class PyWarnTests(WarnTests, unittest.TestCase):
Christian Heimes33fe8092008-04-13 13:53:33 +0000371 module = py_warnings
372
Nick Coghlanfce769e2009-04-11 14:30:59 +0000373 # As an early adopter, we sanity check the
374 # test.support.import_fresh_module utility function
375 def test_pure_python(self):
376 self.assertFalse(original_warnings is self.module)
377 self.assertTrue(hasattr(self.module.warn, '__code__'))
378
Christian Heimes33fe8092008-04-13 13:53:33 +0000379
Ezio Melotti2688e812013-01-10 06:52:23 +0200380class WCmdLineTests(BaseTest):
Christian Heimes33fe8092008-04-13 13:53:33 +0000381
382 def test_improper_input(self):
383 # Uses the private _setoption() function to test the parsing
384 # of command-line warning arguments
Brett Cannon1cd02472008-09-09 01:52:27 +0000385 with original_warnings.catch_warnings(module=self.module):
Christian Heimes33fe8092008-04-13 13:53:33 +0000386 self.assertRaises(self.module._OptionError,
387 self.module._setoption, '1:2:3:4:5:6')
388 self.assertRaises(self.module._OptionError,
389 self.module._setoption, 'bogus::Warning')
390 self.assertRaises(self.module._OptionError,
391 self.module._setoption, 'ignore:2::4:-5')
392 self.module._setoption('error::Warning::0')
393 self.assertRaises(UserWarning, self.module.warn, 'convert to error')
394
Antoine Pitroucf9f9802010-11-10 13:55:25 +0000395 def test_improper_option(self):
396 # Same as above, but check that the message is printed out when
397 # the interpreter is executed. This also checks that options are
398 # actually parsed at all.
399 rc, out, err = assert_python_ok("-Wxxx", "-c", "pass")
400 self.assertIn(b"Invalid -W option ignored: invalid action: 'xxx'", err)
401
402 def test_warnings_bootstrap(self):
403 # Check that the warnings module does get loaded when -W<some option>
404 # is used (see issue #10372 for an example of silent bootstrap failure).
405 rc, out, err = assert_python_ok("-Wi", "-c",
406 "import sys; sys.modules['warnings'].warn('foo', RuntimeWarning)")
407 # '-Wi' was observed
408 self.assertFalse(out.strip())
409 self.assertNotIn(b'RuntimeWarning', err)
410
Ezio Melotti2688e812013-01-10 06:52:23 +0200411class CWCmdLineTests(WCmdLineTests, unittest.TestCase):
Christian Heimes33fe8092008-04-13 13:53:33 +0000412 module = c_warnings
413
Ezio Melotti2688e812013-01-10 06:52:23 +0200414class PyWCmdLineTests(WCmdLineTests, unittest.TestCase):
Christian Heimes33fe8092008-04-13 13:53:33 +0000415 module = py_warnings
416
417
Ezio Melotti2688e812013-01-10 06:52:23 +0200418class _WarningsTests(BaseTest, unittest.TestCase):
Christian Heimes33fe8092008-04-13 13:53:33 +0000419
420 """Tests specific to the _warnings module."""
421
422 module = c_warnings
423
424 def test_filter(self):
425 # Everything should function even if 'filters' is not in warnings.
Brett Cannon1cd02472008-09-09 01:52:27 +0000426 with original_warnings.catch_warnings(module=self.module) as w:
Christian Heimes33fe8092008-04-13 13:53:33 +0000427 self.module.filterwarnings("error", "", Warning, "", 0)
428 self.assertRaises(UserWarning, self.module.warn,
429 'convert to error')
430 del self.module.filters
431 self.assertRaises(UserWarning, self.module.warn,
432 'convert to error')
433
434 def test_onceregistry(self):
435 # Replacing or removing the onceregistry should be okay.
436 global __warningregistry__
437 message = UserWarning('onceregistry test')
438 try:
439 original_registry = self.module.onceregistry
440 __warningregistry__ = {}
Brett Cannon1cd02472008-09-09 01:52:27 +0000441 with original_warnings.catch_warnings(record=True,
442 module=self.module) as w:
Christian Heimes33fe8092008-04-13 13:53:33 +0000443 self.module.resetwarnings()
444 self.module.filterwarnings("once", category=UserWarning)
445 self.module.warn_explicit(message, UserWarning, "file", 42)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000446 self.assertEqual(w[-1].message, message)
Brett Cannon1cd02472008-09-09 01:52:27 +0000447 del w[:]
Christian Heimes33fe8092008-04-13 13:53:33 +0000448 self.module.warn_explicit(message, UserWarning, "file", 42)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000449 self.assertEqual(len(w), 0)
Christian Heimes33fe8092008-04-13 13:53:33 +0000450 # Test the resetting of onceregistry.
451 self.module.onceregistry = {}
452 __warningregistry__ = {}
453 self.module.warn('onceregistry test')
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000454 self.assertEqual(w[-1].message.args, message.args)
Christian Heimes33fe8092008-04-13 13:53:33 +0000455 # Removal of onceregistry is okay.
Brett Cannon1cd02472008-09-09 01:52:27 +0000456 del w[:]
Christian Heimes33fe8092008-04-13 13:53:33 +0000457 del self.module.onceregistry
458 __warningregistry__ = {}
459 self.module.warn_explicit(message, UserWarning, "file", 42)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000460 self.assertEqual(len(w), 0)
Christian Heimes33fe8092008-04-13 13:53:33 +0000461 finally:
462 self.module.onceregistry = original_registry
463
Brett Cannon0759dd62009-04-01 18:13:07 +0000464 def test_default_action(self):
465 # Replacing or removing defaultaction should be okay.
466 message = UserWarning("defaultaction test")
467 original = self.module.defaultaction
468 try:
469 with original_warnings.catch_warnings(record=True,
470 module=self.module) as w:
471 self.module.resetwarnings()
472 registry = {}
473 self.module.warn_explicit(message, UserWarning, "<test>", 42,
474 registry=registry)
475 self.assertEqual(w[-1].message, message)
476 self.assertEqual(len(w), 1)
477 self.assertEqual(len(registry), 1)
478 del w[:]
479 # Test removal.
480 del self.module.defaultaction
481 __warningregistry__ = {}
482 registry = {}
483 self.module.warn_explicit(message, UserWarning, "<test>", 43,
484 registry=registry)
485 self.assertEqual(w[-1].message, message)
486 self.assertEqual(len(w), 1)
487 self.assertEqual(len(registry), 1)
488 del w[:]
489 # Test setting.
490 self.module.defaultaction = "ignore"
491 __warningregistry__ = {}
492 registry = {}
493 self.module.warn_explicit(message, UserWarning, "<test>", 44,
494 registry=registry)
495 self.assertEqual(len(w), 0)
496 finally:
497 self.module.defaultaction = original
498
Christian Heimes33fe8092008-04-13 13:53:33 +0000499 def test_showwarning_missing(self):
500 # Test that showwarning() missing is okay.
501 text = 'del showwarning test'
Brett Cannon1cd02472008-09-09 01:52:27 +0000502 with original_warnings.catch_warnings(module=self.module):
Christian Heimes33fe8092008-04-13 13:53:33 +0000503 self.module.filterwarnings("always", category=UserWarning)
504 del self.module.showwarning
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000505 with support.captured_output('stderr') as stream:
Christian Heimes33fe8092008-04-13 13:53:33 +0000506 self.module.warn(text)
507 result = stream.getvalue()
Benjamin Peterson577473f2010-01-19 00:09:57 +0000508 self.assertIn(text, result)
Christian Heimes33fe8092008-04-13 13:53:33 +0000509
Christian Heimes8dc226f2008-05-06 23:45:46 +0000510 def test_showwarning_not_callable(self):
Brett Cannonfcc05272009-04-01 20:27:29 +0000511 with original_warnings.catch_warnings(module=self.module):
512 self.module.filterwarnings("always", category=UserWarning)
Brett Cannon52a7d982011-07-17 19:17:55 -0700513 self.module.showwarning = print
514 with support.captured_output('stdout'):
515 self.module.warn('Warning!')
Brett Cannonfcc05272009-04-01 20:27:29 +0000516 self.module.showwarning = 23
Brett Cannon52a7d982011-07-17 19:17:55 -0700517 self.assertRaises(TypeError, self.module.warn, "Warning!")
Christian Heimes8dc226f2008-05-06 23:45:46 +0000518
Christian Heimes33fe8092008-04-13 13:53:33 +0000519 def test_show_warning_output(self):
520 # With showarning() missing, make sure that output is okay.
521 text = 'test show_warning'
Brett Cannon1cd02472008-09-09 01:52:27 +0000522 with original_warnings.catch_warnings(module=self.module):
Christian Heimes33fe8092008-04-13 13:53:33 +0000523 self.module.filterwarnings("always", category=UserWarning)
524 del self.module.showwarning
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000525 with support.captured_output('stderr') as stream:
Christian Heimes33fe8092008-04-13 13:53:33 +0000526 warning_tests.inner(text)
527 result = stream.getvalue()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000528 self.assertEqual(result.count('\n'), 2,
Christian Heimes33fe8092008-04-13 13:53:33 +0000529 "Too many newlines in %r" % result)
530 first_line, second_line = result.split('\n', 1)
531 expected_file = os.path.splitext(warning_tests.__file__)[0] + '.py'
Neal Norwitz32dde222008-04-15 06:43:13 +0000532 first_line_parts = first_line.rsplit(':', 3)
533 path, line, warning_class, message = first_line_parts
Christian Heimes33fe8092008-04-13 13:53:33 +0000534 line = int(line)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000535 self.assertEqual(expected_file, path)
536 self.assertEqual(warning_class, ' ' + UserWarning.__name__)
537 self.assertEqual(message, ' ' + text)
Christian Heimes33fe8092008-04-13 13:53:33 +0000538 expected_line = ' ' + linecache.getline(path, line).strip() + '\n'
539 assert expected_line
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000540 self.assertEqual(second_line, expected_line)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000541
Victor Stinner8b0508e2011-07-04 02:43:09 +0200542 def test_filename_none(self):
543 # issue #12467: race condition if a warning is emitted at shutdown
544 globals_dict = globals()
545 oldfile = globals_dict['__file__']
546 try:
Brett Cannon52a7d982011-07-17 19:17:55 -0700547 catch = original_warnings.catch_warnings(record=True,
548 module=self.module)
549 with catch as w:
Victor Stinner8b0508e2011-07-04 02:43:09 +0200550 self.module.filterwarnings("always", category=UserWarning)
551 globals_dict['__file__'] = None
552 original_warnings.warn('test', UserWarning)
Brett Cannon52a7d982011-07-17 19:17:55 -0700553 self.assertTrue(len(w))
Victor Stinner8b0508e2011-07-04 02:43:09 +0200554 finally:
555 globals_dict['__file__'] = oldfile
556
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000557
Ezio Melotti2688e812013-01-10 06:52:23 +0200558class WarningsDisplayTests(BaseTest):
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000559
Christian Heimes33fe8092008-04-13 13:53:33 +0000560 """Test the displaying of warnings and the ability to overload functions
561 related to displaying warnings."""
562
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000563 def test_formatwarning(self):
564 message = "msg"
565 category = Warning
566 file_name = os.path.splitext(warning_tests.__file__)[0] + '.py'
567 line_num = 3
568 file_line = linecache.getline(file_name, line_num).strip()
Christian Heimes33fe8092008-04-13 13:53:33 +0000569 format = "%s:%s: %s: %s\n %s\n"
570 expect = format % (file_name, line_num, category.__name__, message,
571 file_line)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000572 self.assertEqual(expect, self.module.formatwarning(message,
Christian Heimes33fe8092008-04-13 13:53:33 +0000573 category, file_name, line_num))
574 # Test the 'line' argument.
575 file_line += " for the win!"
576 expect = format % (file_name, line_num, category.__name__, message,
577 file_line)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000578 self.assertEqual(expect, self.module.formatwarning(message,
Christian Heimes33fe8092008-04-13 13:53:33 +0000579 category, file_name, line_num, file_line))
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000580
581 def test_showwarning(self):
582 file_name = os.path.splitext(warning_tests.__file__)[0] + '.py'
583 line_num = 3
584 expected_file_line = linecache.getline(file_name, line_num).strip()
585 message = 'msg'
586 category = Warning
587 file_object = StringIO()
Christian Heimes33fe8092008-04-13 13:53:33 +0000588 expect = self.module.formatwarning(message, category, file_name,
589 line_num)
590 self.module.showwarning(message, category, file_name, line_num,
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000591 file_object)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000592 self.assertEqual(file_object.getvalue(), expect)
Christian Heimes33fe8092008-04-13 13:53:33 +0000593 # Test 'line' argument.
594 expected_file_line += "for the win!"
595 expect = self.module.formatwarning(message, category, file_name,
596 line_num, expected_file_line)
597 file_object = StringIO()
598 self.module.showwarning(message, category, file_name, line_num,
599 file_object, expected_file_line)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000600 self.assertEqual(expect, file_object.getvalue())
Christian Heimes33fe8092008-04-13 13:53:33 +0000601
Ezio Melotti2688e812013-01-10 06:52:23 +0200602class CWarningsDisplayTests(WarningsDisplayTests, unittest.TestCase):
Christian Heimes33fe8092008-04-13 13:53:33 +0000603 module = c_warnings
604
Ezio Melotti2688e812013-01-10 06:52:23 +0200605class PyWarningsDisplayTests(WarningsDisplayTests, unittest.TestCase):
Christian Heimes33fe8092008-04-13 13:53:33 +0000606 module = py_warnings
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000607
Brett Cannon1cd02472008-09-09 01:52:27 +0000608
Brett Cannonec92e182008-09-02 02:46:59 +0000609class CatchWarningTests(BaseTest):
Nick Coghlanb1304932008-07-13 12:25:08 +0000610
Brett Cannonec92e182008-09-02 02:46:59 +0000611 """Test catch_warnings()."""
612
613 def test_catch_warnings_restore(self):
Nick Coghlanb1304932008-07-13 12:25:08 +0000614 wmod = self.module
615 orig_filters = wmod.filters
616 orig_showwarning = wmod.showwarning
Benjamin Petersonfcf5d632008-10-16 23:24:44 +0000617 # Ensure both showwarning and filters are restored when recording
618 with wmod.catch_warnings(module=wmod, record=True):
Nick Coghlanb1304932008-07-13 12:25:08 +0000619 wmod.filters = wmod.showwarning = object()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000620 self.assertTrue(wmod.filters is orig_filters)
621 self.assertTrue(wmod.showwarning is orig_showwarning)
Benjamin Petersonfcf5d632008-10-16 23:24:44 +0000622 # Same test, but with recording disabled
623 with wmod.catch_warnings(module=wmod, record=False):
Nick Coghlanb1304932008-07-13 12:25:08 +0000624 wmod.filters = wmod.showwarning = object()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000625 self.assertTrue(wmod.filters is orig_filters)
626 self.assertTrue(wmod.showwarning is orig_showwarning)
Nick Coghlanb1304932008-07-13 12:25:08 +0000627
Brett Cannonec92e182008-09-02 02:46:59 +0000628 def test_catch_warnings_recording(self):
Nick Coghlanb1304932008-07-13 12:25:08 +0000629 wmod = self.module
Benjamin Petersonfcf5d632008-10-16 23:24:44 +0000630 # Ensure warnings are recorded when requested
631 with wmod.catch_warnings(module=wmod, record=True) as w:
Brett Cannonec92e182008-09-02 02:46:59 +0000632 self.assertEqual(w, [])
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000633 self.assertTrue(type(w) is list)
Nick Coghlanb1304932008-07-13 12:25:08 +0000634 wmod.simplefilter("always")
635 wmod.warn("foo")
Brett Cannon1cd02472008-09-09 01:52:27 +0000636 self.assertEqual(str(w[-1].message), "foo")
Nick Coghlanb1304932008-07-13 12:25:08 +0000637 wmod.warn("bar")
Brett Cannon1cd02472008-09-09 01:52:27 +0000638 self.assertEqual(str(w[-1].message), "bar")
Brett Cannonec92e182008-09-02 02:46:59 +0000639 self.assertEqual(str(w[0].message), "foo")
640 self.assertEqual(str(w[1].message), "bar")
Brett Cannon1cd02472008-09-09 01:52:27 +0000641 del w[:]
Brett Cannonec92e182008-09-02 02:46:59 +0000642 self.assertEqual(w, [])
Benjamin Petersonfcf5d632008-10-16 23:24:44 +0000643 # Ensure warnings are not recorded when not requested
Nick Coghlanb1304932008-07-13 12:25:08 +0000644 orig_showwarning = wmod.showwarning
Benjamin Petersonfcf5d632008-10-16 23:24:44 +0000645 with wmod.catch_warnings(module=wmod, record=False) as w:
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000646 self.assertTrue(w is None)
647 self.assertTrue(wmod.showwarning is orig_showwarning)
Nick Coghlanb1304932008-07-13 12:25:08 +0000648
Benjamin Petersonfcf5d632008-10-16 23:24:44 +0000649 def test_catch_warnings_reentry_guard(self):
650 wmod = self.module
651 # Ensure catch_warnings is protected against incorrect usage
652 x = wmod.catch_warnings(module=wmod, record=True)
653 self.assertRaises(RuntimeError, x.__exit__)
654 with x:
655 self.assertRaises(RuntimeError, x.__enter__)
656 # Same test, but with recording disabled
657 x = wmod.catch_warnings(module=wmod, record=False)
658 self.assertRaises(RuntimeError, x.__exit__)
659 with x:
660 self.assertRaises(RuntimeError, x.__enter__)
661
662 def test_catch_warnings_defaults(self):
663 wmod = self.module
664 orig_filters = wmod.filters
665 orig_showwarning = wmod.showwarning
666 # Ensure default behaviour is not to record warnings
667 with wmod.catch_warnings(module=wmod) as w:
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000668 self.assertTrue(w is None)
669 self.assertTrue(wmod.showwarning is orig_showwarning)
670 self.assertTrue(wmod.filters is not orig_filters)
671 self.assertTrue(wmod.filters is orig_filters)
Benjamin Petersonfcf5d632008-10-16 23:24:44 +0000672 if wmod is sys.modules['warnings']:
673 # Ensure the default module is this one
674 with wmod.catch_warnings() as w:
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000675 self.assertTrue(w is None)
676 self.assertTrue(wmod.showwarning is orig_showwarning)
677 self.assertTrue(wmod.filters is not orig_filters)
678 self.assertTrue(wmod.filters is orig_filters)
Benjamin Petersonfcf5d632008-10-16 23:24:44 +0000679
680 def test_check_warnings(self):
681 # Explicit tests for the test.support convenience wrapper
682 wmod = self.module
Florent Xicluna53b506be2010-03-18 20:00:57 +0000683 if wmod is not sys.modules['warnings']:
Zachary Ware9fe6d862013-12-08 00:20:35 -0600684 self.skipTest('module to test is not loaded warnings module')
Florent Xicluna53b506be2010-03-18 20:00:57 +0000685 with support.check_warnings(quiet=False) as w:
686 self.assertEqual(w.warnings, [])
687 wmod.simplefilter("always")
688 wmod.warn("foo")
689 self.assertEqual(str(w.message), "foo")
690 wmod.warn("bar")
691 self.assertEqual(str(w.message), "bar")
692 self.assertEqual(str(w.warnings[0].message), "foo")
693 self.assertEqual(str(w.warnings[1].message), "bar")
694 w.reset()
695 self.assertEqual(w.warnings, [])
696
697 with support.check_warnings():
698 # defaults to quiet=True without argument
699 pass
700 with support.check_warnings(('foo', UserWarning)):
701 wmod.warn("foo")
702
703 with self.assertRaises(AssertionError):
704 with support.check_warnings(('', RuntimeWarning)):
705 # defaults to quiet=False with argument
706 pass
707 with self.assertRaises(AssertionError):
708 with support.check_warnings(('foo', RuntimeWarning)):
Benjamin Petersonfcf5d632008-10-16 23:24:44 +0000709 wmod.warn("foo")
Benjamin Petersonfcf5d632008-10-16 23:24:44 +0000710
Ezio Melotti2688e812013-01-10 06:52:23 +0200711class CCatchWarningTests(CatchWarningTests, unittest.TestCase):
Nick Coghlanb1304932008-07-13 12:25:08 +0000712 module = c_warnings
713
Ezio Melotti2688e812013-01-10 06:52:23 +0200714class PyCatchWarningTests(CatchWarningTests, unittest.TestCase):
Nick Coghlanb1304932008-07-13 12:25:08 +0000715 module = py_warnings
716
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000717
Philip Jenvey0805ca32010-04-07 04:04:10 +0000718class EnvironmentVariableTests(BaseTest):
719
720 def test_single_warning(self):
Antoine Pitroubb08b362014-01-29 23:44:05 +0100721 rc, stdout, stderr = assert_python_ok("-c",
722 "import sys; sys.stdout.write(str(sys.warnoptions))",
723 PYTHONWARNINGS="ignore::DeprecationWarning")
724 self.assertEqual(stdout, b"['ignore::DeprecationWarning']")
Philip Jenvey0805ca32010-04-07 04:04:10 +0000725
726 def test_comma_separated_warnings(self):
Antoine Pitroubb08b362014-01-29 23:44:05 +0100727 rc, stdout, stderr = assert_python_ok("-c",
728 "import sys; sys.stdout.write(str(sys.warnoptions))",
729 PYTHONWARNINGS="ignore::DeprecationWarning,ignore::UnicodeWarning")
730 self.assertEqual(stdout,
731 b"['ignore::DeprecationWarning', 'ignore::UnicodeWarning']")
Philip Jenvey0805ca32010-04-07 04:04:10 +0000732
733 def test_envvar_and_command_line(self):
Antoine Pitroubb08b362014-01-29 23:44:05 +0100734 rc, stdout, stderr = assert_python_ok("-Wignore::UnicodeWarning", "-c",
735 "import sys; sys.stdout.write(str(sys.warnoptions))",
736 PYTHONWARNINGS="ignore::DeprecationWarning")
737 self.assertEqual(stdout,
738 b"['ignore::UnicodeWarning', 'ignore::DeprecationWarning']")
Philip Jenvey0805ca32010-04-07 04:04:10 +0000739
Philip Jenveye53de3d2010-04-14 03:01:39 +0000740 @unittest.skipUnless(sys.getfilesystemencoding() != 'ascii',
741 'requires non-ascii filesystemencoding')
742 def test_nonascii(self):
Antoine Pitroubb08b362014-01-29 23:44:05 +0100743 rc, stdout, stderr = assert_python_ok("-c",
744 "import sys; sys.stdout.write(str(sys.warnoptions))",
745 PYTHONIOENCODING="utf-8",
746 PYTHONWARNINGS="ignore:DeprecaciónWarning")
747 self.assertEqual(stdout,
748 "['ignore:DeprecaciónWarning']".encode('utf-8'))
Philip Jenveye53de3d2010-04-14 03:01:39 +0000749
Ezio Melotti2688e812013-01-10 06:52:23 +0200750class CEnvironmentVariableTests(EnvironmentVariableTests, unittest.TestCase):
Philip Jenvey0805ca32010-04-07 04:04:10 +0000751 module = c_warnings
752
Ezio Melotti2688e812013-01-10 06:52:23 +0200753class PyEnvironmentVariableTests(EnvironmentVariableTests, unittest.TestCase):
Philip Jenvey0805ca32010-04-07 04:04:10 +0000754 module = py_warnings
755
756
Victor Stinner7d79b8b2010-05-19 20:40:50 +0000757class BootstrapTest(unittest.TestCase):
758 def test_issue_8766(self):
759 # "import encodings" emits a warning whereas the warnings is not loaded
Ezio Melotti42da6632011-03-15 05:18:48 +0200760 # or not completely loaded (warnings imports indirectly encodings by
Victor Stinner7d79b8b2010-05-19 20:40:50 +0000761 # importing linecache) yet
762 with support.temp_cwd() as cwd, support.temp_cwd('encodings'):
Victor Stinner7d79b8b2010-05-19 20:40:50 +0000763 # encodings loaded by initfsencoding()
Antoine Pitroubb08b362014-01-29 23:44:05 +0100764 assert_python_ok('-c', 'pass', PYTHONPATH=cwd)
Victor Stinner7d79b8b2010-05-19 20:40:50 +0000765
766 # Use -W to load warnings module at startup
Antoine Pitroubb08b362014-01-29 23:44:05 +0100767 assert_python_ok('-c', 'pass', '-W', 'always', PYTHONPATH=cwd)
Victor Stinner7d79b8b2010-05-19 20:40:50 +0000768
Ezio Melotti2688e812013-01-10 06:52:23 +0200769
770def setUpModule():
Christian Heimesdae2a892008-04-19 00:55:37 +0000771 py_warnings.onceregistry.clear()
772 c_warnings.onceregistry.clear()
Christian Heimes33fe8092008-04-13 13:53:33 +0000773
Ezio Melotti2688e812013-01-10 06:52:23 +0200774tearDownModule = setUpModule
Raymond Hettingerd6f6e502003-07-13 08:37:40 +0000775
776if __name__ == "__main__":
Ezio Melotti2688e812013-01-10 06:52:23 +0200777 unittest.main()