blob: 68bac364ef4a6d9c424abb476fe98b91f5c9b81f [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
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400188 def test_mutate_filter_list(self):
189 class X:
190 def match(self, a):
191 L[:] = []
192
193 L = [("default",X(),UserWarning,X(),0) for i in range(2)]
194 with original_warnings.catch_warnings(record=True,
195 module=self.module) as w:
196 self.module.filters = L
197 self.module.warn_explicit(UserWarning("b"), None, "f.py", 42)
198 self.assertEqual(str(w[-1].message), "b")
199
Ezio Melotti2688e812013-01-10 06:52:23 +0200200class CFilterTests(FilterTests, unittest.TestCase):
Christian Heimes33fe8092008-04-13 13:53:33 +0000201 module = c_warnings
202
Ezio Melotti2688e812013-01-10 06:52:23 +0200203class PyFilterTests(FilterTests, unittest.TestCase):
Christian Heimes33fe8092008-04-13 13:53:33 +0000204 module = py_warnings
205
206
Ezio Melotti2688e812013-01-10 06:52:23 +0200207class WarnTests(BaseTest):
Christian Heimes33fe8092008-04-13 13:53:33 +0000208
209 """Test warnings.warn() and warnings.warn_explicit()."""
210
211 def test_message(self):
Brett Cannon1cd02472008-09-09 01:52:27 +0000212 with original_warnings.catch_warnings(record=True,
213 module=self.module) as w:
Florent Xiclunafd1b0932010-03-28 00:25:02 +0000214 self.module.simplefilter("once")
Guido van Rossumd8faa362007-04-27 19:54:29 +0000215 for i in range(4):
Christian Heimes33fe8092008-04-13 13:53:33 +0000216 text = 'multi %d' %i # Different text on each call.
217 self.module.warn(text)
Brett Cannon1cd02472008-09-09 01:52:27 +0000218 self.assertEqual(str(w[-1].message), text)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000219 self.assertTrue(w[-1].category is UserWarning)
Raymond Hettingerd6f6e502003-07-13 08:37:40 +0000220
Brett Cannon54bd41d2008-09-02 04:01:42 +0000221 # Issue 3639
222 def test_warn_nonstandard_types(self):
223 # warn() should handle non-standard types without issue.
224 for ob in (Warning, None, 42):
Benjamin Petersonfcf5d632008-10-16 23:24:44 +0000225 with original_warnings.catch_warnings(record=True,
226 module=self.module) as w:
Florent Xiclunafd1b0932010-03-28 00:25:02 +0000227 self.module.simplefilter("once")
Brett Cannon54bd41d2008-09-02 04:01:42 +0000228 self.module.warn(ob)
229 # Don't directly compare objects since
230 # ``Warning() != Warning()``.
Ezio Melottib3aedd42010-11-20 19:04:17 +0000231 self.assertEqual(str(w[-1].message), str(UserWarning(ob)))
Brett Cannon54bd41d2008-09-02 04:01:42 +0000232
Guido van Rossumd8faa362007-04-27 19:54:29 +0000233 def test_filename(self):
Christian Heimes33fe8092008-04-13 13:53:33 +0000234 with warnings_state(self.module):
Brett Cannon1cd02472008-09-09 01:52:27 +0000235 with original_warnings.catch_warnings(record=True,
236 module=self.module) as w:
Christian Heimes33fe8092008-04-13 13:53:33 +0000237 warning_tests.inner("spam1")
Brett Cannon1cd02472008-09-09 01:52:27 +0000238 self.assertEqual(os.path.basename(w[-1].filename),
239 "warning_tests.py")
Christian Heimes33fe8092008-04-13 13:53:33 +0000240 warning_tests.outer("spam2")
Brett Cannon1cd02472008-09-09 01:52:27 +0000241 self.assertEqual(os.path.basename(w[-1].filename),
242 "warning_tests.py")
Guido van Rossumd8faa362007-04-27 19:54:29 +0000243
244 def test_stacklevel(self):
245 # Test stacklevel argument
246 # make sure all messages are different, so the warning won't be skipped
Christian Heimes33fe8092008-04-13 13:53:33 +0000247 with warnings_state(self.module):
Brett Cannon1cd02472008-09-09 01:52:27 +0000248 with original_warnings.catch_warnings(record=True,
249 module=self.module) as w:
Christian Heimes33fe8092008-04-13 13:53:33 +0000250 warning_tests.inner("spam3", stacklevel=1)
Brett Cannon1cd02472008-09-09 01:52:27 +0000251 self.assertEqual(os.path.basename(w[-1].filename),
252 "warning_tests.py")
Christian Heimes33fe8092008-04-13 13:53:33 +0000253 warning_tests.outer("spam4", stacklevel=1)
Brett Cannon1cd02472008-09-09 01:52:27 +0000254 self.assertEqual(os.path.basename(w[-1].filename),
255 "warning_tests.py")
Guido van Rossumd8faa362007-04-27 19:54:29 +0000256
Christian Heimes33fe8092008-04-13 13:53:33 +0000257 warning_tests.inner("spam5", stacklevel=2)
Brett Cannon1cd02472008-09-09 01:52:27 +0000258 self.assertEqual(os.path.basename(w[-1].filename),
259 "test_warnings.py")
Christian Heimes33fe8092008-04-13 13:53:33 +0000260 warning_tests.outer("spam6", stacklevel=2)
Brett Cannon1cd02472008-09-09 01:52:27 +0000261 self.assertEqual(os.path.basename(w[-1].filename),
262 "warning_tests.py")
Christian Heimes5d8da202008-05-06 13:58:24 +0000263 warning_tests.outer("spam6.5", stacklevel=3)
Brett Cannon1cd02472008-09-09 01:52:27 +0000264 self.assertEqual(os.path.basename(w[-1].filename),
265 "test_warnings.py")
Guido van Rossumd8faa362007-04-27 19:54:29 +0000266
Christian Heimes33fe8092008-04-13 13:53:33 +0000267 warning_tests.inner("spam7", stacklevel=9999)
Brett Cannon1cd02472008-09-09 01:52:27 +0000268 self.assertEqual(os.path.basename(w[-1].filename),
269 "sys")
Christian Heimes33fe8092008-04-13 13:53:33 +0000270
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000271 def test_missing_filename_not_main(self):
272 # If __file__ is not specified and __main__ is not the module name,
273 # then __file__ should be set to the module name.
274 filename = warning_tests.__file__
275 try:
276 del warning_tests.__file__
277 with warnings_state(self.module):
Brett Cannon1cd02472008-09-09 01:52:27 +0000278 with original_warnings.catch_warnings(record=True,
279 module=self.module) as w:
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000280 warning_tests.inner("spam8", stacklevel=1)
Brett Cannon1cd02472008-09-09 01:52:27 +0000281 self.assertEqual(w[-1].filename, warning_tests.__name__)
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000282 finally:
283 warning_tests.__file__ = filename
284
Serhiy Storchaka79080682013-11-03 21:31:18 +0200285 @unittest.skipUnless(hasattr(sys, 'argv'), 'test needs sys.argv')
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000286 def test_missing_filename_main_with_argv(self):
287 # If __file__ is not specified and the caller is __main__ and sys.argv
288 # exists, then use sys.argv[0] as the file.
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000289 filename = warning_tests.__file__
290 module_name = warning_tests.__name__
291 try:
292 del warning_tests.__file__
293 warning_tests.__name__ = '__main__'
294 with warnings_state(self.module):
Brett Cannon1cd02472008-09-09 01:52:27 +0000295 with original_warnings.catch_warnings(record=True,
296 module=self.module) as w:
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000297 warning_tests.inner('spam9', stacklevel=1)
Brett Cannon1cd02472008-09-09 01:52:27 +0000298 self.assertEqual(w[-1].filename, sys.argv[0])
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000299 finally:
300 warning_tests.__file__ = filename
301 warning_tests.__name__ = module_name
302
303 def test_missing_filename_main_without_argv(self):
304 # If __file__ is not specified, the caller is __main__, and sys.argv
305 # is not set, then '__main__' is the file name.
306 filename = warning_tests.__file__
307 module_name = warning_tests.__name__
308 argv = sys.argv
309 try:
310 del warning_tests.__file__
311 warning_tests.__name__ = '__main__'
312 del sys.argv
313 with warnings_state(self.module):
Brett Cannon1cd02472008-09-09 01:52:27 +0000314 with original_warnings.catch_warnings(record=True,
315 module=self.module) as w:
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000316 warning_tests.inner('spam10', stacklevel=1)
Brett Cannon1cd02472008-09-09 01:52:27 +0000317 self.assertEqual(w[-1].filename, '__main__')
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000318 finally:
319 warning_tests.__file__ = filename
320 warning_tests.__name__ = module_name
321 sys.argv = argv
322
Christian Heimesdaaf8ee2008-05-04 23:58:41 +0000323 def test_missing_filename_main_with_argv_empty_string(self):
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000324 # If __file__ is not specified, the caller is __main__, and sys.argv[0]
325 # is the empty string, then '__main__ is the file name.
326 # Tests issue 2743.
327 file_name = warning_tests.__file__
328 module_name = warning_tests.__name__
329 argv = sys.argv
330 try:
331 del warning_tests.__file__
332 warning_tests.__name__ = '__main__'
333 sys.argv = ['']
334 with warnings_state(self.module):
Brett Cannon1cd02472008-09-09 01:52:27 +0000335 with original_warnings.catch_warnings(record=True,
336 module=self.module) as w:
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000337 warning_tests.inner('spam11', stacklevel=1)
Brett Cannon1cd02472008-09-09 01:52:27 +0000338 self.assertEqual(w[-1].filename, '__main__')
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000339 finally:
340 warning_tests.__file__ = file_name
341 warning_tests.__name__ = module_name
342 sys.argv = argv
343
Brett Cannondb734912008-06-27 00:52:15 +0000344 def test_warn_explicit_type_errors(self):
Ezio Melotti13925002011-03-16 11:05:33 +0200345 # warn_explicit() should error out gracefully if it is given objects
Brett Cannondb734912008-06-27 00:52:15 +0000346 # of the wrong types.
347 # lineno is expected to be an integer.
348 self.assertRaises(TypeError, self.module.warn_explicit,
349 None, UserWarning, None, None)
350 # Either 'message' needs to be an instance of Warning or 'category'
351 # needs to be a subclass.
352 self.assertRaises(TypeError, self.module.warn_explicit,
353 None, None, None, 1)
354 # 'registry' must be a dict or None.
355 self.assertRaises((TypeError, AttributeError),
356 self.module.warn_explicit,
357 None, Warning, None, 1, registry=42)
358
Hirokazu Yamamoto1c0c0032009-07-17 06:55:42 +0000359 def test_bad_str(self):
360 # issue 6415
361 # Warnings instance with a bad format string for __str__ should not
362 # trigger a bus error.
363 class BadStrWarning(Warning):
364 """Warning with a bad format string for __str__."""
365 def __str__(self):
366 return ("A bad formatted string %(err)" %
367 {"err" : "there is no %(err)s"})
368
369 with self.assertRaises(ValueError):
370 self.module.warn(BadStrWarning())
371
372
Ezio Melotti2688e812013-01-10 06:52:23 +0200373class CWarnTests(WarnTests, unittest.TestCase):
Christian Heimes33fe8092008-04-13 13:53:33 +0000374 module = c_warnings
375
Nick Coghlanfce769e2009-04-11 14:30:59 +0000376 # As an early adopter, we sanity check the
377 # test.support.import_fresh_module utility function
378 def test_accelerated(self):
379 self.assertFalse(original_warnings is self.module)
380 self.assertFalse(hasattr(self.module.warn, '__code__'))
381
Ezio Melotti2688e812013-01-10 06:52:23 +0200382class PyWarnTests(WarnTests, unittest.TestCase):
Christian Heimes33fe8092008-04-13 13:53:33 +0000383 module = py_warnings
384
Nick Coghlanfce769e2009-04-11 14:30:59 +0000385 # As an early adopter, we sanity check the
386 # test.support.import_fresh_module utility function
387 def test_pure_python(self):
388 self.assertFalse(original_warnings is self.module)
389 self.assertTrue(hasattr(self.module.warn, '__code__'))
390
Christian Heimes33fe8092008-04-13 13:53:33 +0000391
Ezio Melotti2688e812013-01-10 06:52:23 +0200392class WCmdLineTests(BaseTest):
Christian Heimes33fe8092008-04-13 13:53:33 +0000393
394 def test_improper_input(self):
395 # Uses the private _setoption() function to test the parsing
396 # of command-line warning arguments
Brett Cannon1cd02472008-09-09 01:52:27 +0000397 with original_warnings.catch_warnings(module=self.module):
Christian Heimes33fe8092008-04-13 13:53:33 +0000398 self.assertRaises(self.module._OptionError,
399 self.module._setoption, '1:2:3:4:5:6')
400 self.assertRaises(self.module._OptionError,
401 self.module._setoption, 'bogus::Warning')
402 self.assertRaises(self.module._OptionError,
403 self.module._setoption, 'ignore:2::4:-5')
404 self.module._setoption('error::Warning::0')
405 self.assertRaises(UserWarning, self.module.warn, 'convert to error')
406
Antoine Pitroucf9f9802010-11-10 13:55:25 +0000407 def test_improper_option(self):
408 # Same as above, but check that the message is printed out when
409 # the interpreter is executed. This also checks that options are
410 # actually parsed at all.
411 rc, out, err = assert_python_ok("-Wxxx", "-c", "pass")
412 self.assertIn(b"Invalid -W option ignored: invalid action: 'xxx'", err)
413
414 def test_warnings_bootstrap(self):
415 # Check that the warnings module does get loaded when -W<some option>
416 # is used (see issue #10372 for an example of silent bootstrap failure).
417 rc, out, err = assert_python_ok("-Wi", "-c",
418 "import sys; sys.modules['warnings'].warn('foo', RuntimeWarning)")
419 # '-Wi' was observed
420 self.assertFalse(out.strip())
421 self.assertNotIn(b'RuntimeWarning', err)
422
Ezio Melotti2688e812013-01-10 06:52:23 +0200423class CWCmdLineTests(WCmdLineTests, unittest.TestCase):
Christian Heimes33fe8092008-04-13 13:53:33 +0000424 module = c_warnings
425
Ezio Melotti2688e812013-01-10 06:52:23 +0200426class PyWCmdLineTests(WCmdLineTests, unittest.TestCase):
Christian Heimes33fe8092008-04-13 13:53:33 +0000427 module = py_warnings
428
429
Ezio Melotti2688e812013-01-10 06:52:23 +0200430class _WarningsTests(BaseTest, unittest.TestCase):
Christian Heimes33fe8092008-04-13 13:53:33 +0000431
432 """Tests specific to the _warnings module."""
433
434 module = c_warnings
435
436 def test_filter(self):
437 # Everything should function even if 'filters' is not in warnings.
Brett Cannon1cd02472008-09-09 01:52:27 +0000438 with original_warnings.catch_warnings(module=self.module) as w:
Christian Heimes33fe8092008-04-13 13:53:33 +0000439 self.module.filterwarnings("error", "", Warning, "", 0)
440 self.assertRaises(UserWarning, self.module.warn,
441 'convert to error')
442 del self.module.filters
443 self.assertRaises(UserWarning, self.module.warn,
444 'convert to error')
445
446 def test_onceregistry(self):
447 # Replacing or removing the onceregistry should be okay.
448 global __warningregistry__
449 message = UserWarning('onceregistry test')
450 try:
451 original_registry = self.module.onceregistry
452 __warningregistry__ = {}
Brett Cannon1cd02472008-09-09 01:52:27 +0000453 with original_warnings.catch_warnings(record=True,
454 module=self.module) as w:
Christian Heimes33fe8092008-04-13 13:53:33 +0000455 self.module.resetwarnings()
456 self.module.filterwarnings("once", category=UserWarning)
457 self.module.warn_explicit(message, UserWarning, "file", 42)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000458 self.assertEqual(w[-1].message, message)
Brett Cannon1cd02472008-09-09 01:52:27 +0000459 del w[:]
Christian Heimes33fe8092008-04-13 13:53:33 +0000460 self.module.warn_explicit(message, UserWarning, "file", 42)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000461 self.assertEqual(len(w), 0)
Christian Heimes33fe8092008-04-13 13:53:33 +0000462 # Test the resetting of onceregistry.
463 self.module.onceregistry = {}
464 __warningregistry__ = {}
465 self.module.warn('onceregistry test')
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000466 self.assertEqual(w[-1].message.args, message.args)
Christian Heimes33fe8092008-04-13 13:53:33 +0000467 # Removal of onceregistry is okay.
Brett Cannon1cd02472008-09-09 01:52:27 +0000468 del w[:]
Christian Heimes33fe8092008-04-13 13:53:33 +0000469 del self.module.onceregistry
470 __warningregistry__ = {}
471 self.module.warn_explicit(message, UserWarning, "file", 42)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000472 self.assertEqual(len(w), 0)
Christian Heimes33fe8092008-04-13 13:53:33 +0000473 finally:
474 self.module.onceregistry = original_registry
475
Brett Cannon0759dd62009-04-01 18:13:07 +0000476 def test_default_action(self):
477 # Replacing or removing defaultaction should be okay.
478 message = UserWarning("defaultaction test")
479 original = self.module.defaultaction
480 try:
481 with original_warnings.catch_warnings(record=True,
482 module=self.module) as w:
483 self.module.resetwarnings()
484 registry = {}
485 self.module.warn_explicit(message, UserWarning, "<test>", 42,
486 registry=registry)
487 self.assertEqual(w[-1].message, message)
488 self.assertEqual(len(w), 1)
489 self.assertEqual(len(registry), 1)
490 del w[:]
491 # Test removal.
492 del self.module.defaultaction
493 __warningregistry__ = {}
494 registry = {}
495 self.module.warn_explicit(message, UserWarning, "<test>", 43,
496 registry=registry)
497 self.assertEqual(w[-1].message, message)
498 self.assertEqual(len(w), 1)
499 self.assertEqual(len(registry), 1)
500 del w[:]
501 # Test setting.
502 self.module.defaultaction = "ignore"
503 __warningregistry__ = {}
504 registry = {}
505 self.module.warn_explicit(message, UserWarning, "<test>", 44,
506 registry=registry)
507 self.assertEqual(len(w), 0)
508 finally:
509 self.module.defaultaction = original
510
Christian Heimes33fe8092008-04-13 13:53:33 +0000511 def test_showwarning_missing(self):
512 # Test that showwarning() missing is okay.
513 text = 'del showwarning test'
Brett Cannon1cd02472008-09-09 01:52:27 +0000514 with original_warnings.catch_warnings(module=self.module):
Christian Heimes33fe8092008-04-13 13:53:33 +0000515 self.module.filterwarnings("always", category=UserWarning)
516 del self.module.showwarning
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000517 with support.captured_output('stderr') as stream:
Christian Heimes33fe8092008-04-13 13:53:33 +0000518 self.module.warn(text)
519 result = stream.getvalue()
Benjamin Peterson577473f2010-01-19 00:09:57 +0000520 self.assertIn(text, result)
Christian Heimes33fe8092008-04-13 13:53:33 +0000521
Christian Heimes8dc226f2008-05-06 23:45:46 +0000522 def test_showwarning_not_callable(self):
Brett Cannonfcc05272009-04-01 20:27:29 +0000523 with original_warnings.catch_warnings(module=self.module):
524 self.module.filterwarnings("always", category=UserWarning)
Brett Cannon52a7d982011-07-17 19:17:55 -0700525 self.module.showwarning = print
526 with support.captured_output('stdout'):
527 self.module.warn('Warning!')
Brett Cannonfcc05272009-04-01 20:27:29 +0000528 self.module.showwarning = 23
Brett Cannon52a7d982011-07-17 19:17:55 -0700529 self.assertRaises(TypeError, self.module.warn, "Warning!")
Christian Heimes8dc226f2008-05-06 23:45:46 +0000530
Christian Heimes33fe8092008-04-13 13:53:33 +0000531 def test_show_warning_output(self):
532 # With showarning() missing, make sure that output is okay.
533 text = 'test show_warning'
Brett Cannon1cd02472008-09-09 01:52:27 +0000534 with original_warnings.catch_warnings(module=self.module):
Christian Heimes33fe8092008-04-13 13:53:33 +0000535 self.module.filterwarnings("always", category=UserWarning)
536 del self.module.showwarning
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000537 with support.captured_output('stderr') as stream:
Christian Heimes33fe8092008-04-13 13:53:33 +0000538 warning_tests.inner(text)
539 result = stream.getvalue()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000540 self.assertEqual(result.count('\n'), 2,
Christian Heimes33fe8092008-04-13 13:53:33 +0000541 "Too many newlines in %r" % result)
542 first_line, second_line = result.split('\n', 1)
543 expected_file = os.path.splitext(warning_tests.__file__)[0] + '.py'
Neal Norwitz32dde222008-04-15 06:43:13 +0000544 first_line_parts = first_line.rsplit(':', 3)
545 path, line, warning_class, message = first_line_parts
Christian Heimes33fe8092008-04-13 13:53:33 +0000546 line = int(line)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000547 self.assertEqual(expected_file, path)
548 self.assertEqual(warning_class, ' ' + UserWarning.__name__)
549 self.assertEqual(message, ' ' + text)
Christian Heimes33fe8092008-04-13 13:53:33 +0000550 expected_line = ' ' + linecache.getline(path, line).strip() + '\n'
551 assert expected_line
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000552 self.assertEqual(second_line, expected_line)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000553
Victor Stinner8b0508e2011-07-04 02:43:09 +0200554 def test_filename_none(self):
555 # issue #12467: race condition if a warning is emitted at shutdown
556 globals_dict = globals()
557 oldfile = globals_dict['__file__']
558 try:
Brett Cannon52a7d982011-07-17 19:17:55 -0700559 catch = original_warnings.catch_warnings(record=True,
560 module=self.module)
561 with catch as w:
Victor Stinner8b0508e2011-07-04 02:43:09 +0200562 self.module.filterwarnings("always", category=UserWarning)
563 globals_dict['__file__'] = None
564 original_warnings.warn('test', UserWarning)
Brett Cannon52a7d982011-07-17 19:17:55 -0700565 self.assertTrue(len(w))
Victor Stinner8b0508e2011-07-04 02:43:09 +0200566 finally:
567 globals_dict['__file__'] = oldfile
568
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000569
Ezio Melotti2688e812013-01-10 06:52:23 +0200570class WarningsDisplayTests(BaseTest):
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000571
Christian Heimes33fe8092008-04-13 13:53:33 +0000572 """Test the displaying of warnings and the ability to overload functions
573 related to displaying warnings."""
574
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000575 def test_formatwarning(self):
576 message = "msg"
577 category = Warning
578 file_name = os.path.splitext(warning_tests.__file__)[0] + '.py'
579 line_num = 3
580 file_line = linecache.getline(file_name, line_num).strip()
Christian Heimes33fe8092008-04-13 13:53:33 +0000581 format = "%s:%s: %s: %s\n %s\n"
582 expect = format % (file_name, line_num, category.__name__, message,
583 file_line)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000584 self.assertEqual(expect, self.module.formatwarning(message,
Christian Heimes33fe8092008-04-13 13:53:33 +0000585 category, file_name, line_num))
586 # Test the 'line' argument.
587 file_line += " for the win!"
588 expect = format % (file_name, line_num, category.__name__, message,
589 file_line)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000590 self.assertEqual(expect, self.module.formatwarning(message,
Christian Heimes33fe8092008-04-13 13:53:33 +0000591 category, file_name, line_num, file_line))
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000592
593 def test_showwarning(self):
594 file_name = os.path.splitext(warning_tests.__file__)[0] + '.py'
595 line_num = 3
596 expected_file_line = linecache.getline(file_name, line_num).strip()
597 message = 'msg'
598 category = Warning
599 file_object = StringIO()
Christian Heimes33fe8092008-04-13 13:53:33 +0000600 expect = self.module.formatwarning(message, category, file_name,
601 line_num)
602 self.module.showwarning(message, category, file_name, line_num,
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000603 file_object)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000604 self.assertEqual(file_object.getvalue(), expect)
Christian Heimes33fe8092008-04-13 13:53:33 +0000605 # Test 'line' argument.
606 expected_file_line += "for the win!"
607 expect = self.module.formatwarning(message, category, file_name,
608 line_num, expected_file_line)
609 file_object = StringIO()
610 self.module.showwarning(message, category, file_name, line_num,
611 file_object, expected_file_line)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000612 self.assertEqual(expect, file_object.getvalue())
Christian Heimes33fe8092008-04-13 13:53:33 +0000613
Ezio Melotti2688e812013-01-10 06:52:23 +0200614class CWarningsDisplayTests(WarningsDisplayTests, unittest.TestCase):
Christian Heimes33fe8092008-04-13 13:53:33 +0000615 module = c_warnings
616
Ezio Melotti2688e812013-01-10 06:52:23 +0200617class PyWarningsDisplayTests(WarningsDisplayTests, unittest.TestCase):
Christian Heimes33fe8092008-04-13 13:53:33 +0000618 module = py_warnings
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000619
Brett Cannon1cd02472008-09-09 01:52:27 +0000620
Brett Cannonec92e182008-09-02 02:46:59 +0000621class CatchWarningTests(BaseTest):
Nick Coghlanb1304932008-07-13 12:25:08 +0000622
Brett Cannonec92e182008-09-02 02:46:59 +0000623 """Test catch_warnings()."""
624
625 def test_catch_warnings_restore(self):
Nick Coghlanb1304932008-07-13 12:25:08 +0000626 wmod = self.module
627 orig_filters = wmod.filters
628 orig_showwarning = wmod.showwarning
Benjamin Petersonfcf5d632008-10-16 23:24:44 +0000629 # Ensure both showwarning and filters are restored when recording
630 with wmod.catch_warnings(module=wmod, record=True):
Nick Coghlanb1304932008-07-13 12:25:08 +0000631 wmod.filters = wmod.showwarning = object()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000632 self.assertTrue(wmod.filters is orig_filters)
633 self.assertTrue(wmod.showwarning is orig_showwarning)
Benjamin Petersonfcf5d632008-10-16 23:24:44 +0000634 # Same test, but with recording disabled
635 with wmod.catch_warnings(module=wmod, record=False):
Nick Coghlanb1304932008-07-13 12:25:08 +0000636 wmod.filters = wmod.showwarning = object()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000637 self.assertTrue(wmod.filters is orig_filters)
638 self.assertTrue(wmod.showwarning is orig_showwarning)
Nick Coghlanb1304932008-07-13 12:25:08 +0000639
Brett Cannonec92e182008-09-02 02:46:59 +0000640 def test_catch_warnings_recording(self):
Nick Coghlanb1304932008-07-13 12:25:08 +0000641 wmod = self.module
Benjamin Petersonfcf5d632008-10-16 23:24:44 +0000642 # Ensure warnings are recorded when requested
643 with wmod.catch_warnings(module=wmod, record=True) as w:
Brett Cannonec92e182008-09-02 02:46:59 +0000644 self.assertEqual(w, [])
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000645 self.assertTrue(type(w) is list)
Nick Coghlanb1304932008-07-13 12:25:08 +0000646 wmod.simplefilter("always")
647 wmod.warn("foo")
Brett Cannon1cd02472008-09-09 01:52:27 +0000648 self.assertEqual(str(w[-1].message), "foo")
Nick Coghlanb1304932008-07-13 12:25:08 +0000649 wmod.warn("bar")
Brett Cannon1cd02472008-09-09 01:52:27 +0000650 self.assertEqual(str(w[-1].message), "bar")
Brett Cannonec92e182008-09-02 02:46:59 +0000651 self.assertEqual(str(w[0].message), "foo")
652 self.assertEqual(str(w[1].message), "bar")
Brett Cannon1cd02472008-09-09 01:52:27 +0000653 del w[:]
Brett Cannonec92e182008-09-02 02:46:59 +0000654 self.assertEqual(w, [])
Benjamin Petersonfcf5d632008-10-16 23:24:44 +0000655 # Ensure warnings are not recorded when not requested
Nick Coghlanb1304932008-07-13 12:25:08 +0000656 orig_showwarning = wmod.showwarning
Benjamin Petersonfcf5d632008-10-16 23:24:44 +0000657 with wmod.catch_warnings(module=wmod, record=False) as w:
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000658 self.assertTrue(w is None)
659 self.assertTrue(wmod.showwarning is orig_showwarning)
Nick Coghlanb1304932008-07-13 12:25:08 +0000660
Benjamin Petersonfcf5d632008-10-16 23:24:44 +0000661 def test_catch_warnings_reentry_guard(self):
662 wmod = self.module
663 # Ensure catch_warnings is protected against incorrect usage
664 x = wmod.catch_warnings(module=wmod, record=True)
665 self.assertRaises(RuntimeError, x.__exit__)
666 with x:
667 self.assertRaises(RuntimeError, x.__enter__)
668 # Same test, but with recording disabled
669 x = wmod.catch_warnings(module=wmod, record=False)
670 self.assertRaises(RuntimeError, x.__exit__)
671 with x:
672 self.assertRaises(RuntimeError, x.__enter__)
673
674 def test_catch_warnings_defaults(self):
675 wmod = self.module
676 orig_filters = wmod.filters
677 orig_showwarning = wmod.showwarning
678 # Ensure default behaviour is not to record warnings
679 with wmod.catch_warnings(module=wmod) as w:
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000680 self.assertTrue(w is None)
681 self.assertTrue(wmod.showwarning is orig_showwarning)
682 self.assertTrue(wmod.filters is not orig_filters)
683 self.assertTrue(wmod.filters is orig_filters)
Benjamin Petersonfcf5d632008-10-16 23:24:44 +0000684 if wmod is sys.modules['warnings']:
685 # Ensure the default module is this one
686 with wmod.catch_warnings() as w:
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000687 self.assertTrue(w is None)
688 self.assertTrue(wmod.showwarning is orig_showwarning)
689 self.assertTrue(wmod.filters is not orig_filters)
690 self.assertTrue(wmod.filters is orig_filters)
Benjamin Petersonfcf5d632008-10-16 23:24:44 +0000691
692 def test_check_warnings(self):
693 # Explicit tests for the test.support convenience wrapper
694 wmod = self.module
Florent Xicluna53b506be2010-03-18 20:00:57 +0000695 if wmod is not sys.modules['warnings']:
Zachary Ware9fe6d862013-12-08 00:20:35 -0600696 self.skipTest('module to test is not loaded warnings module')
Florent Xicluna53b506be2010-03-18 20:00:57 +0000697 with support.check_warnings(quiet=False) as w:
698 self.assertEqual(w.warnings, [])
699 wmod.simplefilter("always")
700 wmod.warn("foo")
701 self.assertEqual(str(w.message), "foo")
702 wmod.warn("bar")
703 self.assertEqual(str(w.message), "bar")
704 self.assertEqual(str(w.warnings[0].message), "foo")
705 self.assertEqual(str(w.warnings[1].message), "bar")
706 w.reset()
707 self.assertEqual(w.warnings, [])
708
709 with support.check_warnings():
710 # defaults to quiet=True without argument
711 pass
712 with support.check_warnings(('foo', UserWarning)):
713 wmod.warn("foo")
714
715 with self.assertRaises(AssertionError):
716 with support.check_warnings(('', RuntimeWarning)):
717 # defaults to quiet=False with argument
718 pass
719 with self.assertRaises(AssertionError):
720 with support.check_warnings(('foo', RuntimeWarning)):
Benjamin Petersonfcf5d632008-10-16 23:24:44 +0000721 wmod.warn("foo")
Benjamin Petersonfcf5d632008-10-16 23:24:44 +0000722
Ezio Melotti2688e812013-01-10 06:52:23 +0200723class CCatchWarningTests(CatchWarningTests, unittest.TestCase):
Nick Coghlanb1304932008-07-13 12:25:08 +0000724 module = c_warnings
725
Ezio Melotti2688e812013-01-10 06:52:23 +0200726class PyCatchWarningTests(CatchWarningTests, unittest.TestCase):
Nick Coghlanb1304932008-07-13 12:25:08 +0000727 module = py_warnings
728
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000729
Philip Jenvey0805ca32010-04-07 04:04:10 +0000730class EnvironmentVariableTests(BaseTest):
731
732 def test_single_warning(self):
Antoine Pitroubb08b362014-01-29 23:44:05 +0100733 rc, stdout, stderr = assert_python_ok("-c",
734 "import sys; sys.stdout.write(str(sys.warnoptions))",
735 PYTHONWARNINGS="ignore::DeprecationWarning")
736 self.assertEqual(stdout, b"['ignore::DeprecationWarning']")
Philip Jenvey0805ca32010-04-07 04:04:10 +0000737
738 def test_comma_separated_warnings(self):
Antoine Pitroubb08b362014-01-29 23:44:05 +0100739 rc, stdout, stderr = assert_python_ok("-c",
740 "import sys; sys.stdout.write(str(sys.warnoptions))",
741 PYTHONWARNINGS="ignore::DeprecationWarning,ignore::UnicodeWarning")
742 self.assertEqual(stdout,
743 b"['ignore::DeprecationWarning', 'ignore::UnicodeWarning']")
Philip Jenvey0805ca32010-04-07 04:04:10 +0000744
745 def test_envvar_and_command_line(self):
Antoine Pitroubb08b362014-01-29 23:44:05 +0100746 rc, stdout, stderr = assert_python_ok("-Wignore::UnicodeWarning", "-c",
747 "import sys; sys.stdout.write(str(sys.warnoptions))",
748 PYTHONWARNINGS="ignore::DeprecationWarning")
749 self.assertEqual(stdout,
750 b"['ignore::UnicodeWarning', 'ignore::DeprecationWarning']")
Philip Jenvey0805ca32010-04-07 04:04:10 +0000751
Philip Jenveye53de3d2010-04-14 03:01:39 +0000752 @unittest.skipUnless(sys.getfilesystemencoding() != 'ascii',
753 'requires non-ascii filesystemencoding')
754 def test_nonascii(self):
Antoine Pitroubb08b362014-01-29 23:44:05 +0100755 rc, stdout, stderr = assert_python_ok("-c",
756 "import sys; sys.stdout.write(str(sys.warnoptions))",
757 PYTHONIOENCODING="utf-8",
758 PYTHONWARNINGS="ignore:DeprecaciónWarning")
759 self.assertEqual(stdout,
760 "['ignore:DeprecaciónWarning']".encode('utf-8'))
Philip Jenveye53de3d2010-04-14 03:01:39 +0000761
Ezio Melotti2688e812013-01-10 06:52:23 +0200762class CEnvironmentVariableTests(EnvironmentVariableTests, unittest.TestCase):
Philip Jenvey0805ca32010-04-07 04:04:10 +0000763 module = c_warnings
764
Ezio Melotti2688e812013-01-10 06:52:23 +0200765class PyEnvironmentVariableTests(EnvironmentVariableTests, unittest.TestCase):
Philip Jenvey0805ca32010-04-07 04:04:10 +0000766 module = py_warnings
767
768
Victor Stinner7d79b8b2010-05-19 20:40:50 +0000769class BootstrapTest(unittest.TestCase):
770 def test_issue_8766(self):
771 # "import encodings" emits a warning whereas the warnings is not loaded
Ezio Melotti42da6632011-03-15 05:18:48 +0200772 # or not completely loaded (warnings imports indirectly encodings by
Victor Stinner7d79b8b2010-05-19 20:40:50 +0000773 # importing linecache) yet
774 with support.temp_cwd() as cwd, support.temp_cwd('encodings'):
Victor Stinner7d79b8b2010-05-19 20:40:50 +0000775 # encodings loaded by initfsencoding()
Antoine Pitroubb08b362014-01-29 23:44:05 +0100776 assert_python_ok('-c', 'pass', PYTHONPATH=cwd)
Victor Stinner7d79b8b2010-05-19 20:40:50 +0000777
778 # Use -W to load warnings module at startup
Antoine Pitroubb08b362014-01-29 23:44:05 +0100779 assert_python_ok('-c', 'pass', '-W', 'always', PYTHONPATH=cwd)
Victor Stinner7d79b8b2010-05-19 20:40:50 +0000780
Ezio Melotti2688e812013-01-10 06:52:23 +0200781
782def setUpModule():
Christian Heimesdae2a892008-04-19 00:55:37 +0000783 py_warnings.onceregistry.clear()
784 c_warnings.onceregistry.clear()
Christian Heimes33fe8092008-04-13 13:53:33 +0000785
Ezio Melotti2688e812013-01-10 06:52:23 +0200786tearDownModule = setUpModule
Raymond Hettingerd6f6e502003-07-13 08:37:40 +0000787
788if __name__ == "__main__":
Ezio Melotti2688e812013-01-10 06:52:23 +0200789 unittest.main()