annotate Lib/test/test_warnings.py @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents fc46acf7a645
children 2d330f7908e7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
1 from contextlib import contextmanager
44359
bb716ebcb082 Add tests for the warnings module; specifically formatwarning and showwarning.
Brett Cannon <bcannon@gmail.com>
parents: 42659
diff changeset
2 import linecache
29836
e57a9bbeec5b This test failed on WindowsME because the full file path did not get
Raymond Hettinger <python@rcn.com>
parents: 29817
diff changeset
3 import os
44359
bb716ebcb082 Add tests for the warnings module; specifically formatwarning and showwarning.
Brett Cannon <bcannon@gmail.com>
parents: 42659
diff changeset
4 import StringIO
42659
38dfedf1d2aa Make test_warnings re-entrant.
Brett Cannon <bcannon@gmail.com>
parents: 42584
diff changeset
5 import sys
29837
64194e775b0b Reworked test_warnings.py:
Raymond Hettinger <python@rcn.com>
parents: 29836
diff changeset
6 import unittest
60433
c0957dada5c2 #7301: add the environment variable $PYTHONWARNINGS to supplement the -W
Philip Jenvey <pjenvey@underboss.org>
parents: 60020
diff changeset
7 import subprocess
29837
64194e775b0b Reworked test_warnings.py:
Raymond Hettinger <python@rcn.com>
parents: 29836
diff changeset
8 from test import test_support
66139
74a5894b9513 I'm only backporting the tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60484
diff changeset
9 from test.script_helper import assert_python_ok
29813
7be24cc6c28d Change warnings to avoid importing re module during startup.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
10
41373
ac255a42ec0e Add tests for the filename.
Walter Dörwald <walter@livinglogic.de>
parents: 41372
diff changeset
11 import warning_tests
ac255a42ec0e Add tests for the filename.
Walter Dörwald <walter@livinglogic.de>
parents: 41372
diff changeset
12
46424
8f2b65ec7bba Fix test_warnings by making the state of things more consistent for each test
Brett Cannon <bcannon@gmail.com>
parents: 46414
diff changeset
13 import warnings as original_warnings
8f2b65ec7bba Fix test_warnings by making the state of things more consistent for each test
Brett Cannon <bcannon@gmail.com>
parents: 46414
diff changeset
14
53315
e9595cb5c786 Issue 5354: Change API for import_fresh_module() to better support test_warnings use case (also fixes some bugs in the original implementation)
Nick Coghlan <ncoghlan@gmail.com>
parents: 53048
diff changeset
15 py_warnings = test_support.import_fresh_module('warnings', blocked=['_warnings'])
e9595cb5c786 Issue 5354: Change API for import_fresh_module() to better support test_warnings use case (also fixes some bugs in the original implementation)
Nick Coghlan <ncoghlan@gmail.com>
parents: 53048
diff changeset
16 c_warnings = test_support.import_fresh_module('warnings', fresh=['_warnings'])
46424
8f2b65ec7bba Fix test_warnings by making the state of things more consistent for each test
Brett Cannon <bcannon@gmail.com>
parents: 46414
diff changeset
17
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
18 @contextmanager
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
19 def warnings_state(module):
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
20 """Use a specific warnings implementation in warning_tests."""
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
21 global __warningregistry__
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
22 for to_clear in (sys, warning_tests):
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
23 try:
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
24 to_clear.__warningregistry__.clear()
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
25 except AttributeError:
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
26 pass
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
27 try:
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
28 __warningregistry__.clear()
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
29 except NameError:
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
30 pass
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
31 original_warnings = warning_tests.warnings
60060
0bc04525af63 Merged revisions 79195,79425,79427,79450 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55336
diff changeset
32 original_filters = module.filters
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
33 try:
60060
0bc04525af63 Merged revisions 79195,79425,79427,79450 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55336
diff changeset
34 module.filters = original_filters[:]
0bc04525af63 Merged revisions 79195,79425,79427,79450 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55336
diff changeset
35 module.simplefilter("once")
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
36 warning_tests.warnings = module
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
37 yield
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
38 finally:
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
39 warning_tests.warnings = original_warnings
60060
0bc04525af63 Merged revisions 79195,79425,79427,79450 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55336
diff changeset
40 module.filters = original_filters
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
41
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
42
46424
8f2b65ec7bba Fix test_warnings by making the state of things more consistent for each test
Brett Cannon <bcannon@gmail.com>
parents: 46414
diff changeset
43 class BaseTest(unittest.TestCase):
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
44
46424
8f2b65ec7bba Fix test_warnings by making the state of things more consistent for each test
Brett Cannon <bcannon@gmail.com>
parents: 46414
diff changeset
45 """Basic bookkeeping required for testing."""
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
46
29837
64194e775b0b Reworked test_warnings.py:
Raymond Hettinger <python@rcn.com>
parents: 29836
diff changeset
47 def setUp(self):
46424
8f2b65ec7bba Fix test_warnings by making the state of things more consistent for each test
Brett Cannon <bcannon@gmail.com>
parents: 46414
diff changeset
48 # The __warningregistry__ needs to be in a pristine state for tests
8f2b65ec7bba Fix test_warnings by making the state of things more consistent for each test
Brett Cannon <bcannon@gmail.com>
parents: 46414
diff changeset
49 # to work properly.
8f2b65ec7bba Fix test_warnings by making the state of things more consistent for each test
Brett Cannon <bcannon@gmail.com>
parents: 46414
diff changeset
50 if '__warningregistry__' in globals():
8f2b65ec7bba Fix test_warnings by making the state of things more consistent for each test
Brett Cannon <bcannon@gmail.com>
parents: 46414
diff changeset
51 del globals()['__warningregistry__']
8f2b65ec7bba Fix test_warnings by making the state of things more consistent for each test
Brett Cannon <bcannon@gmail.com>
parents: 46414
diff changeset
52 if hasattr(warning_tests, '__warningregistry__'):
8f2b65ec7bba Fix test_warnings by making the state of things more consistent for each test
Brett Cannon <bcannon@gmail.com>
parents: 46414
diff changeset
53 del warning_tests.__warningregistry__
8f2b65ec7bba Fix test_warnings by making the state of things more consistent for each test
Brett Cannon <bcannon@gmail.com>
parents: 46414
diff changeset
54 if hasattr(sys, '__warningregistry__'):
8f2b65ec7bba Fix test_warnings by making the state of things more consistent for each test
Brett Cannon <bcannon@gmail.com>
parents: 46414
diff changeset
55 del sys.__warningregistry__
8f2b65ec7bba Fix test_warnings by making the state of things more consistent for each test
Brett Cannon <bcannon@gmail.com>
parents: 46414
diff changeset
56 # The 'warnings' module must be explicitly set so that the proper
8f2b65ec7bba Fix test_warnings by making the state of things more consistent for each test
Brett Cannon <bcannon@gmail.com>
parents: 46414
diff changeset
57 # interaction between _warnings and 'warnings' can be controlled.
8f2b65ec7bba Fix test_warnings by making the state of things more consistent for each test
Brett Cannon <bcannon@gmail.com>
parents: 46414
diff changeset
58 sys.modules['warnings'] = self.module
8f2b65ec7bba Fix test_warnings by making the state of things more consistent for each test
Brett Cannon <bcannon@gmail.com>
parents: 46414
diff changeset
59 super(BaseTest, self).setUp()
8f2b65ec7bba Fix test_warnings by making the state of things more consistent for each test
Brett Cannon <bcannon@gmail.com>
parents: 46414
diff changeset
60
8f2b65ec7bba Fix test_warnings by making the state of things more consistent for each test
Brett Cannon <bcannon@gmail.com>
parents: 46414
diff changeset
61 def tearDown(self):
8f2b65ec7bba Fix test_warnings by making the state of things more consistent for each test
Brett Cannon <bcannon@gmail.com>
parents: 46414
diff changeset
62 sys.modules['warnings'] = original_warnings
8f2b65ec7bba Fix test_warnings by making the state of things more consistent for each test
Brett Cannon <bcannon@gmail.com>
parents: 46414
diff changeset
63 super(BaseTest, self).tearDown()
8f2b65ec7bba Fix test_warnings by making the state of things more consistent for each test
Brett Cannon <bcannon@gmail.com>
parents: 46414
diff changeset
64
8f2b65ec7bba Fix test_warnings by making the state of things more consistent for each test
Brett Cannon <bcannon@gmail.com>
parents: 46414
diff changeset
65
8f2b65ec7bba Fix test_warnings by making the state of things more consistent for each test
Brett Cannon <bcannon@gmail.com>
parents: 46414
diff changeset
66 class FilterTests(object):
8f2b65ec7bba Fix test_warnings by making the state of things more consistent for each test
Brett Cannon <bcannon@gmail.com>
parents: 46414
diff changeset
67
8f2b65ec7bba Fix test_warnings by making the state of things more consistent for each test
Brett Cannon <bcannon@gmail.com>
parents: 46414
diff changeset
68 """Testing the filtering functionality."""
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
69
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
70 def test_error(self):
49099
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
71 with original_warnings.catch_warnings(module=self.module) as w:
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
72 self.module.resetwarnings()
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
73 self.module.filterwarnings("error", category=UserWarning)
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
74 self.assertRaises(UserWarning, self.module.warn,
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
75 "FilterTests.test_error")
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
76
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
77 def test_ignore(self):
49099
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
78 with original_warnings.catch_warnings(record=True,
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
79 module=self.module) as w:
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
80 self.module.resetwarnings()
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
81 self.module.filterwarnings("ignore", category=UserWarning)
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
82 self.module.warn("FilterTests.test_ignore", UserWarning)
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 66139
diff changeset
83 self.assertEqual(len(w), 0)
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
84
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
85 def test_always(self):
49099
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
86 with original_warnings.catch_warnings(record=True,
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
87 module=self.module) as w:
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
88 self.module.resetwarnings()
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
89 self.module.filterwarnings("always", category=UserWarning)
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
90 message = "FilterTests.test_always"
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
91 self.module.warn(message, UserWarning)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 53315
diff changeset
92 self.assertTrue(message, w[-1].message)
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
93 self.module.warn(message, UserWarning)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 53315
diff changeset
94 self.assertTrue(w[-1].message, message)
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
95
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
96 def test_default(self):
49099
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
97 with original_warnings.catch_warnings(record=True,
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
98 module=self.module) as w:
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
99 self.module.resetwarnings()
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
100 self.module.filterwarnings("default", category=UserWarning)
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
101 message = UserWarning("FilterTests.test_default")
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
102 for x in xrange(2):
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
103 self.module.warn(message, UserWarning)
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
104 if x == 0:
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 66139
diff changeset
105 self.assertEqual(w[-1].message, message)
49099
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
106 del w[:]
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
107 elif x == 1:
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 66139
diff changeset
108 self.assertEqual(len(w), 0)
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
109 else:
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
110 raise ValueError("loop variant unhandled")
29813
7be24cc6c28d Change warnings to avoid importing re module during startup.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
111
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
112 def test_module(self):
49099
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
113 with original_warnings.catch_warnings(record=True,
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
114 module=self.module) as w:
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
115 self.module.resetwarnings()
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
116 self.module.filterwarnings("module", category=UserWarning)
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
117 message = UserWarning("FilterTests.test_module")
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
118 self.module.warn(message, UserWarning)
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 66139
diff changeset
119 self.assertEqual(w[-1].message, message)
49099
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
120 del w[:]
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
121 self.module.warn(message, UserWarning)
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 66139
diff changeset
122 self.assertEqual(len(w), 0)
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
123
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
124 def test_once(self):
49099
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
125 with original_warnings.catch_warnings(record=True,
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
126 module=self.module) as w:
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
127 self.module.resetwarnings()
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
128 self.module.filterwarnings("once", category=UserWarning)
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
129 message = UserWarning("FilterTests.test_once")
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
130 self.module.warn_explicit(message, UserWarning, "test_warnings.py",
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
131 42)
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 66139
diff changeset
132 self.assertEqual(w[-1].message, message)
49099
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
133 del w[:]
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
134 self.module.warn_explicit(message, UserWarning, "test_warnings.py",
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
135 13)
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 66139
diff changeset
136 self.assertEqual(len(w), 0)
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
137 self.module.warn_explicit(message, UserWarning, "test_warnings2.py",
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
138 42)
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 66139
diff changeset
139 self.assertEqual(len(w), 0)
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
140
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
141 def test_inheritance(self):
49099
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
142 with original_warnings.catch_warnings(module=self.module) as w:
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
143 self.module.resetwarnings()
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
144 self.module.filterwarnings("error", category=Warning)
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
145 self.assertRaises(UserWarning, self.module.warn,
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
146 "FilterTests.test_inheritance", UserWarning)
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
147
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
148 def test_ordering(self):
49099
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
149 with original_warnings.catch_warnings(record=True,
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
150 module=self.module) as w:
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
151 self.module.resetwarnings()
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
152 self.module.filterwarnings("ignore", category=UserWarning)
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
153 self.module.filterwarnings("error", category=UserWarning,
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
154 append=True)
49099
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
155 del w[:]
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
156 try:
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
157 self.module.warn("FilterTests.test_ordering", UserWarning)
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
158 except UserWarning:
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
159 self.fail("order handling for actions failed")
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 66139
diff changeset
160 self.assertEqual(len(w), 0)
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
161
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
162 def test_filterwarnings(self):
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
163 # Test filterwarnings().
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
164 # Implicitly also tests resetwarnings().
49099
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
165 with original_warnings.catch_warnings(record=True,
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
166 module=self.module) as w:
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
167 self.module.filterwarnings("error", "", Warning, "", 0)
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
168 self.assertRaises(UserWarning, self.module.warn, 'convert to error')
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
169
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
170 self.module.resetwarnings()
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
171 text = 'handle normally'
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
172 self.module.warn(text)
49099
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
173 self.assertEqual(str(w[-1].message), text)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 53315
diff changeset
174 self.assertTrue(w[-1].category is UserWarning)
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
175
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
176 self.module.filterwarnings("ignore", "", Warning, "", 0)
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
177 text = 'filtered out'
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
178 self.module.warn(text)
49099
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
179 self.assertNotEqual(str(w[-1].message), text)
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
180
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
181 self.module.resetwarnings()
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
182 self.module.filterwarnings("error", "hex*", Warning, "", 0)
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
183 self.assertRaises(UserWarning, self.module.warn, 'hex/oct')
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
184 text = 'nonmatching text'
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
185 self.module.warn(text)
49099
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
186 self.assertEqual(str(w[-1].message), text)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 53315
diff changeset
187 self.assertTrue(w[-1].category is UserWarning)
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
188
46424
8f2b65ec7bba Fix test_warnings by making the state of things more consistent for each test
Brett Cannon <bcannon@gmail.com>
parents: 46414
diff changeset
189 class CFilterTests(BaseTest, FilterTests):
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
190 module = c_warnings
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
191
46424
8f2b65ec7bba Fix test_warnings by making the state of things more consistent for each test
Brett Cannon <bcannon@gmail.com>
parents: 46414
diff changeset
192 class PyFilterTests(BaseTest, FilterTests):
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
193 module = py_warnings
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
194
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
195
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
196 class WarnTests(unittest.TestCase):
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
197
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
198 """Test warnings.warn() and warnings.warn_explicit()."""
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
199
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
200 def test_message(self):
49099
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
201 with original_warnings.catch_warnings(record=True,
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
202 module=self.module) as w:
60060
0bc04525af63 Merged revisions 79195,79425,79427,79450 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55336
diff changeset
203 self.module.simplefilter("once")
41375
cb6cce4d4232 Implement a contextmanager test.test_support.catch_warning that can
Walter Dörwald <walter@livinglogic.de>
parents: 41373
diff changeset
204 for i in range(4):
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
205 text = 'multi %d' %i # Different text on each call.
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
206 self.module.warn(text)
49099
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
207 self.assertEqual(str(w[-1].message), text)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 53315
diff changeset
208 self.assertTrue(w[-1].category is UserWarning)
29813
7be24cc6c28d Change warnings to avoid importing re module during startup.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
209
41373
ac255a42ec0e Add tests for the filename.
Walter Dörwald <walter@livinglogic.de>
parents: 41372
diff changeset
210 def test_filename(self):
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
211 with warnings_state(self.module):
49099
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
212 with original_warnings.catch_warnings(record=True,
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
213 module=self.module) as w:
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
214 warning_tests.inner("spam1")
49099
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
215 self.assertEqual(os.path.basename(w[-1].filename),
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
216 "warning_tests.py")
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
217 warning_tests.outer("spam2")
49099
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
218 self.assertEqual(os.path.basename(w[-1].filename),
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
219 "warning_tests.py")
41373
ac255a42ec0e Add tests for the filename.
Walter Dörwald <walter@livinglogic.de>
parents: 41372
diff changeset
220
ac255a42ec0e Add tests for the filename.
Walter Dörwald <walter@livinglogic.de>
parents: 41372
diff changeset
221 def test_stacklevel(self):
ac255a42ec0e Add tests for the filename.
Walter Dörwald <walter@livinglogic.de>
parents: 41372
diff changeset
222 # Test stacklevel argument
ac255a42ec0e Add tests for the filename.
Walter Dörwald <walter@livinglogic.de>
parents: 41372
diff changeset
223 # make sure all messages are different, so the warning won't be skipped
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
224 with warnings_state(self.module):
49099
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
225 with original_warnings.catch_warnings(record=True,
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
226 module=self.module) as w:
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
227 warning_tests.inner("spam3", stacklevel=1)
49099
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
228 self.assertEqual(os.path.basename(w[-1].filename),
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
229 "warning_tests.py")
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
230 warning_tests.outer("spam4", stacklevel=1)
49099
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
231 self.assertEqual(os.path.basename(w[-1].filename),
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
232 "warning_tests.py")
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
233
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
234 warning_tests.inner("spam5", stacklevel=2)
49099
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
235 self.assertEqual(os.path.basename(w[-1].filename),
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
236 "test_warnings.py")
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
237 warning_tests.outer("spam6", stacklevel=2)
49099
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
238 self.assertEqual(os.path.basename(w[-1].filename),
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
239 "warning_tests.py")
46686
2f351fa53a6b Fix a bug in the handling of the stacklevel argument in warnings.warn() where
Brett Cannon <bcannon@gmail.com>
parents: 46667
diff changeset
240 warning_tests.outer("spam6.5", stacklevel=3)
49099
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
241 self.assertEqual(os.path.basename(w[-1].filename),
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
242 "test_warnings.py")
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
243
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
244 warning_tests.inner("spam7", stacklevel=9999)
49099
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
245 self.assertEqual(os.path.basename(w[-1].filename),
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
246 "sys")
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
247
46637
a859f835ef53 Fix the C implementation of 'warnings' to infer the filename of the module that
Brett Cannon <bcannon@gmail.com>
parents: 46474
diff changeset
248 def test_missing_filename_not_main(self):
a859f835ef53 Fix the C implementation of 'warnings' to infer the filename of the module that
Brett Cannon <bcannon@gmail.com>
parents: 46474
diff changeset
249 # If __file__ is not specified and __main__ is not the module name,
a859f835ef53 Fix the C implementation of 'warnings' to infer the filename of the module that
Brett Cannon <bcannon@gmail.com>
parents: 46474
diff changeset
250 # then __file__ should be set to the module name.
a859f835ef53 Fix the C implementation of 'warnings' to infer the filename of the module that
Brett Cannon <bcannon@gmail.com>
parents: 46474
diff changeset
251 filename = warning_tests.__file__
a859f835ef53 Fix the C implementation of 'warnings' to infer the filename of the module that
Brett Cannon <bcannon@gmail.com>
parents: 46474
diff changeset
252 try:
a859f835ef53 Fix the C implementation of 'warnings' to infer the filename of the module that
Brett Cannon <bcannon@gmail.com>
parents: 46474
diff changeset
253 del warning_tests.__file__
a859f835ef53 Fix the C implementation of 'warnings' to infer the filename of the module that
Brett Cannon <bcannon@gmail.com>
parents: 46474
diff changeset
254 with warnings_state(self.module):
49099
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
255 with original_warnings.catch_warnings(record=True,
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
256 module=self.module) as w:
46637
a859f835ef53 Fix the C implementation of 'warnings' to infer the filename of the module that
Brett Cannon <bcannon@gmail.com>
parents: 46474
diff changeset
257 warning_tests.inner("spam8", stacklevel=1)
49099
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
258 self.assertEqual(w[-1].filename, warning_tests.__name__)
46637
a859f835ef53 Fix the C implementation of 'warnings' to infer the filename of the module that
Brett Cannon <bcannon@gmail.com>
parents: 46474
diff changeset
259 finally:
a859f835ef53 Fix the C implementation of 'warnings' to infer the filename of the module that
Brett Cannon <bcannon@gmail.com>
parents: 46474
diff changeset
260 warning_tests.__file__ = filename
a859f835ef53 Fix the C implementation of 'warnings' to infer the filename of the module that
Brett Cannon <bcannon@gmail.com>
parents: 46474
diff changeset
261
a859f835ef53 Fix the C implementation of 'warnings' to infer the filename of the module that
Brett Cannon <bcannon@gmail.com>
parents: 46474
diff changeset
262 def test_missing_filename_main_with_argv(self):
a859f835ef53 Fix the C implementation of 'warnings' to infer the filename of the module that
Brett Cannon <bcannon@gmail.com>
parents: 46474
diff changeset
263 # If __file__ is not specified and the caller is __main__ and sys.argv
a859f835ef53 Fix the C implementation of 'warnings' to infer the filename of the module that
Brett Cannon <bcannon@gmail.com>
parents: 46474
diff changeset
264 # exists, then use sys.argv[0] as the file.
a859f835ef53 Fix the C implementation of 'warnings' to infer the filename of the module that
Brett Cannon <bcannon@gmail.com>
parents: 46474
diff changeset
265 if not hasattr(sys, 'argv'):
a859f835ef53 Fix the C implementation of 'warnings' to infer the filename of the module that
Brett Cannon <bcannon@gmail.com>
parents: 46474
diff changeset
266 return
a859f835ef53 Fix the C implementation of 'warnings' to infer the filename of the module that
Brett Cannon <bcannon@gmail.com>
parents: 46474
diff changeset
267 filename = warning_tests.__file__
a859f835ef53 Fix the C implementation of 'warnings' to infer the filename of the module that
Brett Cannon <bcannon@gmail.com>
parents: 46474
diff changeset
268 module_name = warning_tests.__name__
a859f835ef53 Fix the C implementation of 'warnings' to infer the filename of the module that
Brett Cannon <bcannon@gmail.com>
parents: 46474
diff changeset
269 try:
a859f835ef53 Fix the C implementation of 'warnings' to infer the filename of the module that
Brett Cannon <bcannon@gmail.com>
parents: 46474
diff changeset
270 del warning_tests.__file__
a859f835ef53 Fix the C implementation of 'warnings' to infer the filename of the module that
Brett Cannon <bcannon@gmail.com>
parents: 46474
diff changeset
271 warning_tests.__name__ = '__main__'
a859f835ef53 Fix the C implementation of 'warnings' to infer the filename of the module that
Brett Cannon <bcannon@gmail.com>
parents: 46474
diff changeset
272 with warnings_state(self.module):
49099
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
273 with original_warnings.catch_warnings(record=True,
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
274 module=self.module) as w:
46637
a859f835ef53 Fix the C implementation of 'warnings' to infer the filename of the module that
Brett Cannon <bcannon@gmail.com>
parents: 46474
diff changeset
275 warning_tests.inner('spam9', stacklevel=1)
49099
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
276 self.assertEqual(w[-1].filename, sys.argv[0])
46637
a859f835ef53 Fix the C implementation of 'warnings' to infer the filename of the module that
Brett Cannon <bcannon@gmail.com>
parents: 46474
diff changeset
277 finally:
a859f835ef53 Fix the C implementation of 'warnings' to infer the filename of the module that
Brett Cannon <bcannon@gmail.com>
parents: 46474
diff changeset
278 warning_tests.__file__ = filename
a859f835ef53 Fix the C implementation of 'warnings' to infer the filename of the module that
Brett Cannon <bcannon@gmail.com>
parents: 46474
diff changeset
279 warning_tests.__name__ = module_name
a859f835ef53 Fix the C implementation of 'warnings' to infer the filename of the module that
Brett Cannon <bcannon@gmail.com>
parents: 46474
diff changeset
280
a859f835ef53 Fix the C implementation of 'warnings' to infer the filename of the module that
Brett Cannon <bcannon@gmail.com>
parents: 46474
diff changeset
281 def test_missing_filename_main_without_argv(self):
a859f835ef53 Fix the C implementation of 'warnings' to infer the filename of the module that
Brett Cannon <bcannon@gmail.com>
parents: 46474
diff changeset
282 # If __file__ is not specified, the caller is __main__, and sys.argv
a859f835ef53 Fix the C implementation of 'warnings' to infer the filename of the module that
Brett Cannon <bcannon@gmail.com>
parents: 46474
diff changeset
283 # is not set, then '__main__' is the file name.
a859f835ef53 Fix the C implementation of 'warnings' to infer the filename of the module that
Brett Cannon <bcannon@gmail.com>
parents: 46474
diff changeset
284 filename = warning_tests.__file__
a859f835ef53 Fix the C implementation of 'warnings' to infer the filename of the module that
Brett Cannon <bcannon@gmail.com>
parents: 46474
diff changeset
285 module_name = warning_tests.__name__
a859f835ef53 Fix the C implementation of 'warnings' to infer the filename of the module that
Brett Cannon <bcannon@gmail.com>
parents: 46474
diff changeset
286 argv = sys.argv
a859f835ef53 Fix the C implementation of 'warnings' to infer the filename of the module that
Brett Cannon <bcannon@gmail.com>
parents: 46474
diff changeset
287 try:
a859f835ef53 Fix the C implementation of 'warnings' to infer the filename of the module that
Brett Cannon <bcannon@gmail.com>
parents: 46474
diff changeset
288 del warning_tests.__file__
a859f835ef53 Fix the C implementation of 'warnings' to infer the filename of the module that
Brett Cannon <bcannon@gmail.com>
parents: 46474
diff changeset
289 warning_tests.__name__ = '__main__'
a859f835ef53 Fix the C implementation of 'warnings' to infer the filename of the module that
Brett Cannon <bcannon@gmail.com>
parents: 46474
diff changeset
290 del sys.argv
a859f835ef53 Fix the C implementation of 'warnings' to infer the filename of the module that
Brett Cannon <bcannon@gmail.com>
parents: 46474
diff changeset
291 with warnings_state(self.module):
49099
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
292 with original_warnings.catch_warnings(record=True,
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
293 module=self.module) as w:
46637
a859f835ef53 Fix the C implementation of 'warnings' to infer the filename of the module that
Brett Cannon <bcannon@gmail.com>
parents: 46474
diff changeset
294 warning_tests.inner('spam10', stacklevel=1)
49099
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
295 self.assertEqual(w[-1].filename, '__main__')
46637
a859f835ef53 Fix the C implementation of 'warnings' to infer the filename of the module that
Brett Cannon <bcannon@gmail.com>
parents: 46474
diff changeset
296 finally:
a859f835ef53 Fix the C implementation of 'warnings' to infer the filename of the module that
Brett Cannon <bcannon@gmail.com>
parents: 46474
diff changeset
297 warning_tests.__file__ = filename
a859f835ef53 Fix the C implementation of 'warnings' to infer the filename of the module that
Brett Cannon <bcannon@gmail.com>
parents: 46474
diff changeset
298 warning_tests.__name__ = module_name
a859f835ef53 Fix the C implementation of 'warnings' to infer the filename of the module that
Brett Cannon <bcannon@gmail.com>
parents: 46474
diff changeset
299 sys.argv = argv
a859f835ef53 Fix the C implementation of 'warnings' to infer the filename of the module that
Brett Cannon <bcannon@gmail.com>
parents: 46474
diff changeset
300
a859f835ef53 Fix the C implementation of 'warnings' to infer the filename of the module that
Brett Cannon <bcannon@gmail.com>
parents: 46474
diff changeset
301 def test_missing_filename_main_with_argv_empty_string(self):
a859f835ef53 Fix the C implementation of 'warnings' to infer the filename of the module that
Brett Cannon <bcannon@gmail.com>
parents: 46474
diff changeset
302 # If __file__ is not specified, the caller is __main__, and sys.argv[0]
a859f835ef53 Fix the C implementation of 'warnings' to infer the filename of the module that
Brett Cannon <bcannon@gmail.com>
parents: 46474
diff changeset
303 # is the empty string, then '__main__ is the file name.
a859f835ef53 Fix the C implementation of 'warnings' to infer the filename of the module that
Brett Cannon <bcannon@gmail.com>
parents: 46474
diff changeset
304 # Tests issue 2743.
a859f835ef53 Fix the C implementation of 'warnings' to infer the filename of the module that
Brett Cannon <bcannon@gmail.com>
parents: 46474
diff changeset
305 file_name = warning_tests.__file__
a859f835ef53 Fix the C implementation of 'warnings' to infer the filename of the module that
Brett Cannon <bcannon@gmail.com>
parents: 46474
diff changeset
306 module_name = warning_tests.__name__
a859f835ef53 Fix the C implementation of 'warnings' to infer the filename of the module that
Brett Cannon <bcannon@gmail.com>
parents: 46474
diff changeset
307 argv = sys.argv
a859f835ef53 Fix the C implementation of 'warnings' to infer the filename of the module that
Brett Cannon <bcannon@gmail.com>
parents: 46474
diff changeset
308 try:
a859f835ef53 Fix the C implementation of 'warnings' to infer the filename of the module that
Brett Cannon <bcannon@gmail.com>
parents: 46474
diff changeset
309 del warning_tests.__file__
a859f835ef53 Fix the C implementation of 'warnings' to infer the filename of the module that
Brett Cannon <bcannon@gmail.com>
parents: 46474
diff changeset
310 warning_tests.__name__ = '__main__'
a859f835ef53 Fix the C implementation of 'warnings' to infer the filename of the module that
Brett Cannon <bcannon@gmail.com>
parents: 46474
diff changeset
311 sys.argv = ['']
a859f835ef53 Fix the C implementation of 'warnings' to infer the filename of the module that
Brett Cannon <bcannon@gmail.com>
parents: 46474
diff changeset
312 with warnings_state(self.module):
49099
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
313 with original_warnings.catch_warnings(record=True,
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
314 module=self.module) as w:
46637
a859f835ef53 Fix the C implementation of 'warnings' to infer the filename of the module that
Brett Cannon <bcannon@gmail.com>
parents: 46474
diff changeset
315 warning_tests.inner('spam11', stacklevel=1)
49099
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
316 self.assertEqual(w[-1].filename, '__main__')
46637
a859f835ef53 Fix the C implementation of 'warnings' to infer the filename of the module that
Brett Cannon <bcannon@gmail.com>
parents: 46474
diff changeset
317 finally:
a859f835ef53 Fix the C implementation of 'warnings' to infer the filename of the module that
Brett Cannon <bcannon@gmail.com>
parents: 46474
diff changeset
318 warning_tests.__file__ = file_name
a859f835ef53 Fix the C implementation of 'warnings' to infer the filename of the module that
Brett Cannon <bcannon@gmail.com>
parents: 46474
diff changeset
319 warning_tests.__name__ = module_name
a859f835ef53 Fix the C implementation of 'warnings' to infer the filename of the module that
Brett Cannon <bcannon@gmail.com>
parents: 46474
diff changeset
320 sys.argv = argv
a859f835ef53 Fix the C implementation of 'warnings' to infer the filename of the module that
Brett Cannon <bcannon@gmail.com>
parents: 46474
diff changeset
321
47835
dc42efd728ee warnings.warn_explicit() did not have the proper TypeErrors in place to prevent
Brett Cannon <bcannon@gmail.com>
parents: 46993
diff changeset
322 def test_warn_explicit_type_errors(self):
68588
cd8d3c2d3bce #11565: Fix several typos. Patch by Piotr Kasprzyk.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 68256
diff changeset
323 # warn_explicit() should error out gracefully if it is given objects
47835
dc42efd728ee warnings.warn_explicit() did not have the proper TypeErrors in place to prevent
Brett Cannon <bcannon@gmail.com>
parents: 46993
diff changeset
324 # of the wrong types.
dc42efd728ee warnings.warn_explicit() did not have the proper TypeErrors in place to prevent
Brett Cannon <bcannon@gmail.com>
parents: 46993
diff changeset
325 # lineno is expected to be an integer.
dc42efd728ee warnings.warn_explicit() did not have the proper TypeErrors in place to prevent
Brett Cannon <bcannon@gmail.com>
parents: 46993
diff changeset
326 self.assertRaises(TypeError, self.module.warn_explicit,
dc42efd728ee warnings.warn_explicit() did not have the proper TypeErrors in place to prevent
Brett Cannon <bcannon@gmail.com>
parents: 46993
diff changeset
327 None, UserWarning, None, None)
dc42efd728ee warnings.warn_explicit() did not have the proper TypeErrors in place to prevent
Brett Cannon <bcannon@gmail.com>
parents: 46993
diff changeset
328 # Either 'message' needs to be an instance of Warning or 'category'
dc42efd728ee warnings.warn_explicit() did not have the proper TypeErrors in place to prevent
Brett Cannon <bcannon@gmail.com>
parents: 46993
diff changeset
329 # needs to be a subclass.
dc42efd728ee warnings.warn_explicit() did not have the proper TypeErrors in place to prevent
Brett Cannon <bcannon@gmail.com>
parents: 46993
diff changeset
330 self.assertRaises(TypeError, self.module.warn_explicit,
dc42efd728ee warnings.warn_explicit() did not have the proper TypeErrors in place to prevent
Brett Cannon <bcannon@gmail.com>
parents: 46993
diff changeset
331 None, None, None, 1)
dc42efd728ee warnings.warn_explicit() did not have the proper TypeErrors in place to prevent
Brett Cannon <bcannon@gmail.com>
parents: 46993
diff changeset
332 # 'registry' must be a dict or None.
dc42efd728ee warnings.warn_explicit() did not have the proper TypeErrors in place to prevent
Brett Cannon <bcannon@gmail.com>
parents: 46993
diff changeset
333 self.assertRaises((TypeError, AttributeError),
dc42efd728ee warnings.warn_explicit() did not have the proper TypeErrors in place to prevent
Brett Cannon <bcannon@gmail.com>
parents: 46993
diff changeset
334 self.module.warn_explicit,
dc42efd728ee warnings.warn_explicit() did not have the proper TypeErrors in place to prevent
Brett Cannon <bcannon@gmail.com>
parents: 46993
diff changeset
335 None, Warning, None, 1, registry=42)
dc42efd728ee warnings.warn_explicit() did not have the proper TypeErrors in place to prevent
Brett Cannon <bcannon@gmail.com>
parents: 46993
diff changeset
336
55289
130f1911d146 Merged revisions 74040,74042 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 49146
diff changeset
337 def test_bad_str(self):
130f1911d146 Merged revisions 74040,74042 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 49146
diff changeset
338 # issue 6415
130f1911d146 Merged revisions 74040,74042 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 49146
diff changeset
339 # Warnings instance with a bad format string for __str__ should not
130f1911d146 Merged revisions 74040,74042 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 49146
diff changeset
340 # trigger a bus error.
130f1911d146 Merged revisions 74040,74042 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 49146
diff changeset
341 class BadStrWarning(Warning):
130f1911d146 Merged revisions 74040,74042 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 49146
diff changeset
342 """Warning with a bad format string for __str__."""
130f1911d146 Merged revisions 74040,74042 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 49146
diff changeset
343 def __str__(self):
130f1911d146 Merged revisions 74040,74042 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 49146
diff changeset
344 return ("A bad formatted string %(err)" %
130f1911d146 Merged revisions 74040,74042 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 49146
diff changeset
345 {"err" : "there is no %(err)s"})
130f1911d146 Merged revisions 74040,74042 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 49146
diff changeset
346
55286
2dab8d63eb55 Issue #6415: Fixed warnings.warn sagfault on bad formatted string.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 55002
diff changeset
347 with self.assertRaises(ValueError):
2dab8d63eb55 Issue #6415: Fixed warnings.warn sagfault on bad formatted string.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 55002
diff changeset
348 self.module.warn(BadStrWarning())
55289
130f1911d146 Merged revisions 74040,74042 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 49146
diff changeset
349
46637
a859f835ef53 Fix the C implementation of 'warnings' to infer the filename of the module that
Brett Cannon <bcannon@gmail.com>
parents: 46474
diff changeset
350
46424
8f2b65ec7bba Fix test_warnings by making the state of things more consistent for each test
Brett Cannon <bcannon@gmail.com>
parents: 46414
diff changeset
351 class CWarnTests(BaseTest, WarnTests):
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
352 module = c_warnings
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
353
53048
fb41fe67e061 Issue 5354: Provide a standardised testing mechanism for doing fresh imports of modules, including the ability to block extension modules in order to test the pure Python fallbacks
Nick Coghlan <ncoghlan@gmail.com>
parents: 52669
diff changeset
354 # As an early adopter, we sanity check the
fb41fe67e061 Issue 5354: Provide a standardised testing mechanism for doing fresh imports of modules, including the ability to block extension modules in order to test the pure Python fallbacks
Nick Coghlan <ncoghlan@gmail.com>
parents: 52669
diff changeset
355 # test_support.import_fresh_module utility function
fb41fe67e061 Issue 5354: Provide a standardised testing mechanism for doing fresh imports of modules, including the ability to block extension modules in order to test the pure Python fallbacks
Nick Coghlan <ncoghlan@gmail.com>
parents: 52669
diff changeset
356 def test_accelerated(self):
fb41fe67e061 Issue 5354: Provide a standardised testing mechanism for doing fresh imports of modules, including the ability to block extension modules in order to test the pure Python fallbacks
Nick Coghlan <ncoghlan@gmail.com>
parents: 52669
diff changeset
357 self.assertFalse(original_warnings is self.module)
fb41fe67e061 Issue 5354: Provide a standardised testing mechanism for doing fresh imports of modules, including the ability to block extension modules in order to test the pure Python fallbacks
Nick Coghlan <ncoghlan@gmail.com>
parents: 52669
diff changeset
358 self.assertFalse(hasattr(self.module.warn, 'func_code'))
fb41fe67e061 Issue 5354: Provide a standardised testing mechanism for doing fresh imports of modules, including the ability to block extension modules in order to test the pure Python fallbacks
Nick Coghlan <ncoghlan@gmail.com>
parents: 52669
diff changeset
359
46424
8f2b65ec7bba Fix test_warnings by making the state of things more consistent for each test
Brett Cannon <bcannon@gmail.com>
parents: 46414
diff changeset
360 class PyWarnTests(BaseTest, WarnTests):
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
361 module = py_warnings
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
362
53048
fb41fe67e061 Issue 5354: Provide a standardised testing mechanism for doing fresh imports of modules, including the ability to block extension modules in order to test the pure Python fallbacks
Nick Coghlan <ncoghlan@gmail.com>
parents: 52669
diff changeset
363 # As an early adopter, we sanity check the
fb41fe67e061 Issue 5354: Provide a standardised testing mechanism for doing fresh imports of modules, including the ability to block extension modules in order to test the pure Python fallbacks
Nick Coghlan <ncoghlan@gmail.com>
parents: 52669
diff changeset
364 # test_support.import_fresh_module utility function
fb41fe67e061 Issue 5354: Provide a standardised testing mechanism for doing fresh imports of modules, including the ability to block extension modules in order to test the pure Python fallbacks
Nick Coghlan <ncoghlan@gmail.com>
parents: 52669
diff changeset
365 def test_pure_python(self):
fb41fe67e061 Issue 5354: Provide a standardised testing mechanism for doing fresh imports of modules, including the ability to block extension modules in order to test the pure Python fallbacks
Nick Coghlan <ncoghlan@gmail.com>
parents: 52669
diff changeset
366 self.assertFalse(original_warnings is self.module)
fb41fe67e061 Issue 5354: Provide a standardised testing mechanism for doing fresh imports of modules, including the ability to block extension modules in order to test the pure Python fallbacks
Nick Coghlan <ncoghlan@gmail.com>
parents: 52669
diff changeset
367 self.assertTrue(hasattr(self.module.warn, 'func_code'))
fb41fe67e061 Issue 5354: Provide a standardised testing mechanism for doing fresh imports of modules, including the ability to block extension modules in order to test the pure Python fallbacks
Nick Coghlan <ncoghlan@gmail.com>
parents: 52669
diff changeset
368
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
369
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
370 class WCmdLineTests(unittest.TestCase):
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
371
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
372 def test_improper_input(self):
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
373 # Uses the private _setoption() function to test the parsing
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
374 # of command-line warning arguments
49099
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
375 with original_warnings.catch_warnings(module=self.module):
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
376 self.assertRaises(self.module._OptionError,
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
377 self.module._setoption, '1:2:3:4:5:6')
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
378 self.assertRaises(self.module._OptionError,
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
379 self.module._setoption, 'bogus::Warning')
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
380 self.assertRaises(self.module._OptionError,
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
381 self.module._setoption, 'ignore:2::4:-5')
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
382 self.module._setoption('error::Warning::0')
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
383 self.assertRaises(UserWarning, self.module.warn, 'convert to error')
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
384
66139
74a5894b9513 I'm only backporting the tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60484
diff changeset
385 def test_improper_option(self):
74a5894b9513 I'm only backporting the tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60484
diff changeset
386 # Same as above, but check that the message is printed out when
74a5894b9513 I'm only backporting the tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60484
diff changeset
387 # the interpreter is executed. This also checks that options are
74a5894b9513 I'm only backporting the tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60484
diff changeset
388 # actually parsed at all.
74a5894b9513 I'm only backporting the tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60484
diff changeset
389 rc, out, err = assert_python_ok("-Wxxx", "-c", "pass")
74a5894b9513 I'm only backporting the tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60484
diff changeset
390 self.assertIn(b"Invalid -W option ignored: invalid action: 'xxx'", err)
74a5894b9513 I'm only backporting the tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60484
diff changeset
391
74a5894b9513 I'm only backporting the tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60484
diff changeset
392 def test_warnings_bootstrap(self):
74a5894b9513 I'm only backporting the tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60484
diff changeset
393 # Check that the warnings module does get loaded when -W<some option>
74a5894b9513 I'm only backporting the tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60484
diff changeset
394 # is used (see issue #10372 for an example of silent bootstrap failure).
74a5894b9513 I'm only backporting the tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60484
diff changeset
395 rc, out, err = assert_python_ok("-Wi", "-c",
74a5894b9513 I'm only backporting the tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60484
diff changeset
396 "import sys; sys.modules['warnings'].warn('foo', RuntimeWarning)")
74a5894b9513 I'm only backporting the tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60484
diff changeset
397 # '-Wi' was observed
74a5894b9513 I'm only backporting the tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60484
diff changeset
398 self.assertFalse(out.strip())
74a5894b9513 I'm only backporting the tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60484
diff changeset
399 self.assertNotIn(b'RuntimeWarning', err)
74a5894b9513 I'm only backporting the tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60484
diff changeset
400
46424
8f2b65ec7bba Fix test_warnings by making the state of things more consistent for each test
Brett Cannon <bcannon@gmail.com>
parents: 46414
diff changeset
401 class CWCmdLineTests(BaseTest, WCmdLineTests):
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
402 module = c_warnings
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
403
46424
8f2b65ec7bba Fix test_warnings by making the state of things more consistent for each test
Brett Cannon <bcannon@gmail.com>
parents: 46414
diff changeset
404 class PyWCmdLineTests(BaseTest, WCmdLineTests):
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
405 module = py_warnings
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
406
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
407
46424
8f2b65ec7bba Fix test_warnings by making the state of things more consistent for each test
Brett Cannon <bcannon@gmail.com>
parents: 46414
diff changeset
408 class _WarningsTests(BaseTest):
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
409
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
410 """Tests specific to the _warnings module."""
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
411
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
412 module = c_warnings
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
413
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
414 def test_filter(self):
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
415 # Everything should function even if 'filters' is not in warnings.
49099
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
416 with original_warnings.catch_warnings(module=self.module) as w:
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
417 self.module.filterwarnings("error", "", Warning, "", 0)
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
418 self.assertRaises(UserWarning, self.module.warn,
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
419 'convert to error')
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
420 del self.module.filters
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
421 self.assertRaises(UserWarning, self.module.warn,
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
422 'convert to error')
41373
ac255a42ec0e Add tests for the filename.
Walter Dörwald <walter@livinglogic.de>
parents: 41372
diff changeset
423
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
424 def test_onceregistry(self):
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
425 # Replacing or removing the onceregistry should be okay.
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
426 global __warningregistry__
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
427 message = UserWarning('onceregistry test')
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
428 try:
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
429 original_registry = self.module.onceregistry
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
430 __warningregistry__ = {}
49099
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
431 with original_warnings.catch_warnings(record=True,
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
432 module=self.module) as w:
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
433 self.module.resetwarnings()
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
434 self.module.filterwarnings("once", category=UserWarning)
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
435 self.module.warn_explicit(message, UserWarning, "file", 42)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 53315
diff changeset
436 self.assertEqual(w[-1].message, message)
49099
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
437 del w[:]
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
438 self.module.warn_explicit(message, UserWarning, "file", 42)
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 66139
diff changeset
439 self.assertEqual(len(w), 0)
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
440 # Test the resetting of onceregistry.
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
441 self.module.onceregistry = {}
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
442 __warningregistry__ = {}
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
443 self.module.warn('onceregistry test')
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 53315
diff changeset
444 self.assertEqual(w[-1].message.args, message.args)
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
445 # Removal of onceregistry is okay.
49099
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
446 del w[:]
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
447 del self.module.onceregistry
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
448 __warningregistry__ = {}
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
449 self.module.warn_explicit(message, UserWarning, "file", 42)
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 66139
diff changeset
450 self.assertEqual(len(w), 0)
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
451 finally:
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
452 self.module.onceregistry = original_registry
41373
ac255a42ec0e Add tests for the filename.
Walter Dörwald <walter@livinglogic.de>
parents: 41372
diff changeset
453
52658
b5d23fa9b8a4 _warnings was importing itself to get an attribute. That's bad if warnings gets
Brett Cannon <bcannon@gmail.com>
parents: 52125
diff changeset
454 def test_default_action(self):
b5d23fa9b8a4 _warnings was importing itself to get an attribute. That's bad if warnings gets
Brett Cannon <bcannon@gmail.com>
parents: 52125
diff changeset
455 # Replacing or removing defaultaction should be okay.
b5d23fa9b8a4 _warnings was importing itself to get an attribute. That's bad if warnings gets
Brett Cannon <bcannon@gmail.com>
parents: 52125
diff changeset
456 message = UserWarning("defaultaction test")
b5d23fa9b8a4 _warnings was importing itself to get an attribute. That's bad if warnings gets
Brett Cannon <bcannon@gmail.com>
parents: 52125
diff changeset
457 original = self.module.defaultaction
b5d23fa9b8a4 _warnings was importing itself to get an attribute. That's bad if warnings gets
Brett Cannon <bcannon@gmail.com>
parents: 52125
diff changeset
458 try:
b5d23fa9b8a4 _warnings was importing itself to get an attribute. That's bad if warnings gets
Brett Cannon <bcannon@gmail.com>
parents: 52125
diff changeset
459 with original_warnings.catch_warnings(record=True,
b5d23fa9b8a4 _warnings was importing itself to get an attribute. That's bad if warnings gets
Brett Cannon <bcannon@gmail.com>
parents: 52125
diff changeset
460 module=self.module) as w:
b5d23fa9b8a4 _warnings was importing itself to get an attribute. That's bad if warnings gets
Brett Cannon <bcannon@gmail.com>
parents: 52125
diff changeset
461 self.module.resetwarnings()
b5d23fa9b8a4 _warnings was importing itself to get an attribute. That's bad if warnings gets
Brett Cannon <bcannon@gmail.com>
parents: 52125
diff changeset
462 registry = {}
b5d23fa9b8a4 _warnings was importing itself to get an attribute. That's bad if warnings gets
Brett Cannon <bcannon@gmail.com>
parents: 52125
diff changeset
463 self.module.warn_explicit(message, UserWarning, "<test>", 42,
b5d23fa9b8a4 _warnings was importing itself to get an attribute. That's bad if warnings gets
Brett Cannon <bcannon@gmail.com>
parents: 52125
diff changeset
464 registry=registry)
b5d23fa9b8a4 _warnings was importing itself to get an attribute. That's bad if warnings gets
Brett Cannon <bcannon@gmail.com>
parents: 52125
diff changeset
465 self.assertEqual(w[-1].message, message)
b5d23fa9b8a4 _warnings was importing itself to get an attribute. That's bad if warnings gets
Brett Cannon <bcannon@gmail.com>
parents: 52125
diff changeset
466 self.assertEqual(len(w), 1)
b5d23fa9b8a4 _warnings was importing itself to get an attribute. That's bad if warnings gets
Brett Cannon <bcannon@gmail.com>
parents: 52125
diff changeset
467 self.assertEqual(len(registry), 1)
b5d23fa9b8a4 _warnings was importing itself to get an attribute. That's bad if warnings gets
Brett Cannon <bcannon@gmail.com>
parents: 52125
diff changeset
468 del w[:]
b5d23fa9b8a4 _warnings was importing itself to get an attribute. That's bad if warnings gets
Brett Cannon <bcannon@gmail.com>
parents: 52125
diff changeset
469 # Test removal.
b5d23fa9b8a4 _warnings was importing itself to get an attribute. That's bad if warnings gets
Brett Cannon <bcannon@gmail.com>
parents: 52125
diff changeset
470 del self.module.defaultaction
b5d23fa9b8a4 _warnings was importing itself to get an attribute. That's bad if warnings gets
Brett Cannon <bcannon@gmail.com>
parents: 52125
diff changeset
471 __warningregistry__ = {}
b5d23fa9b8a4 _warnings was importing itself to get an attribute. That's bad if warnings gets
Brett Cannon <bcannon@gmail.com>
parents: 52125
diff changeset
472 registry = {}
b5d23fa9b8a4 _warnings was importing itself to get an attribute. That's bad if warnings gets
Brett Cannon <bcannon@gmail.com>
parents: 52125
diff changeset
473 self.module.warn_explicit(message, UserWarning, "<test>", 43,
b5d23fa9b8a4 _warnings was importing itself to get an attribute. That's bad if warnings gets
Brett Cannon <bcannon@gmail.com>
parents: 52125
diff changeset
474 registry=registry)
b5d23fa9b8a4 _warnings was importing itself to get an attribute. That's bad if warnings gets
Brett Cannon <bcannon@gmail.com>
parents: 52125
diff changeset
475 self.assertEqual(w[-1].message, message)
b5d23fa9b8a4 _warnings was importing itself to get an attribute. That's bad if warnings gets
Brett Cannon <bcannon@gmail.com>
parents: 52125
diff changeset
476 self.assertEqual(len(w), 1)
b5d23fa9b8a4 _warnings was importing itself to get an attribute. That's bad if warnings gets
Brett Cannon <bcannon@gmail.com>
parents: 52125
diff changeset
477 self.assertEqual(len(registry), 1)
b5d23fa9b8a4 _warnings was importing itself to get an attribute. That's bad if warnings gets
Brett Cannon <bcannon@gmail.com>
parents: 52125
diff changeset
478 del w[:]
b5d23fa9b8a4 _warnings was importing itself to get an attribute. That's bad if warnings gets
Brett Cannon <bcannon@gmail.com>
parents: 52125
diff changeset
479 # Test setting.
b5d23fa9b8a4 _warnings was importing itself to get an attribute. That's bad if warnings gets
Brett Cannon <bcannon@gmail.com>
parents: 52125
diff changeset
480 self.module.defaultaction = "ignore"
b5d23fa9b8a4 _warnings was importing itself to get an attribute. That's bad if warnings gets
Brett Cannon <bcannon@gmail.com>
parents: 52125
diff changeset
481 __warningregistry__ = {}
b5d23fa9b8a4 _warnings was importing itself to get an attribute. That's bad if warnings gets
Brett Cannon <bcannon@gmail.com>
parents: 52125
diff changeset
482 registry = {}
b5d23fa9b8a4 _warnings was importing itself to get an attribute. That's bad if warnings gets
Brett Cannon <bcannon@gmail.com>
parents: 52125
diff changeset
483 self.module.warn_explicit(message, UserWarning, "<test>", 44,
b5d23fa9b8a4 _warnings was importing itself to get an attribute. That's bad if warnings gets
Brett Cannon <bcannon@gmail.com>
parents: 52125
diff changeset
484 registry=registry)
b5d23fa9b8a4 _warnings was importing itself to get an attribute. That's bad if warnings gets
Brett Cannon <bcannon@gmail.com>
parents: 52125
diff changeset
485 self.assertEqual(len(w), 0)
b5d23fa9b8a4 _warnings was importing itself to get an attribute. That's bad if warnings gets
Brett Cannon <bcannon@gmail.com>
parents: 52125
diff changeset
486 finally:
b5d23fa9b8a4 _warnings was importing itself to get an attribute. That's bad if warnings gets
Brett Cannon <bcannon@gmail.com>
parents: 52125
diff changeset
487 self.module.defaultaction = original
b5d23fa9b8a4 _warnings was importing itself to get an attribute. That's bad if warnings gets
Brett Cannon <bcannon@gmail.com>
parents: 52125
diff changeset
488
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
489 def test_showwarning_missing(self):
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
490 # Test that showwarning() missing is okay.
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
491 text = 'del showwarning test'
49099
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
492 with original_warnings.catch_warnings(module=self.module):
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
493 self.module.filterwarnings("always", category=UserWarning)
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
494 del self.module.showwarning
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
495 with test_support.captured_output('stderr') as stream:
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
496 self.module.warn(text)
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
497 result = stream.getvalue()
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55286
diff changeset
498 self.assertIn(text, result)
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
499
46696
9130b48a7130 Fix logic error in Python/_warnings.c and add a test to verify
Benjamin Peterson <benjamin@python.org>
parents: 46686
diff changeset
500 def test_showwarning_not_callable(self):
52669
7e2bbb4dbf87 test_warnings ironically had a single test that was not protecting the warnings
Brett Cannon <bcannon@gmail.com>
parents: 52658
diff changeset
501 with original_warnings.catch_warnings(module=self.module):
7e2bbb4dbf87 test_warnings ironically had a single test that was not protecting the warnings
Brett Cannon <bcannon@gmail.com>
parents: 52658
diff changeset
502 self.module.filterwarnings("always", category=UserWarning)
7e2bbb4dbf87 test_warnings ironically had a single test that was not protecting the warnings
Brett Cannon <bcannon@gmail.com>
parents: 52658
diff changeset
503 old_showwarning = self.module.showwarning
7e2bbb4dbf87 test_warnings ironically had a single test that was not protecting the warnings
Brett Cannon <bcannon@gmail.com>
parents: 52658
diff changeset
504 self.module.showwarning = 23
7e2bbb4dbf87 test_warnings ironically had a single test that was not protecting the warnings
Brett Cannon <bcannon@gmail.com>
parents: 52658
diff changeset
505 try:
7e2bbb4dbf87 test_warnings ironically had a single test that was not protecting the warnings
Brett Cannon <bcannon@gmail.com>
parents: 52658
diff changeset
506 self.assertRaises(TypeError, self.module.warn, "Warning!")
7e2bbb4dbf87 test_warnings ironically had a single test that was not protecting the warnings
Brett Cannon <bcannon@gmail.com>
parents: 52658
diff changeset
507 finally:
7e2bbb4dbf87 test_warnings ironically had a single test that was not protecting the warnings
Brett Cannon <bcannon@gmail.com>
parents: 52658
diff changeset
508 self.module.showwarning = old_showwarning
46696
9130b48a7130 Fix logic error in Python/_warnings.c and add a test to verify
Benjamin Peterson <benjamin@python.org>
parents: 46686
diff changeset
509
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
510 def test_show_warning_output(self):
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
511 # With showarning() missing, make sure that output is okay.
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
512 text = 'test show_warning'
49099
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
513 with original_warnings.catch_warnings(module=self.module):
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
514 self.module.filterwarnings("always", category=UserWarning)
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
515 del self.module.showwarning
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
516 with test_support.captured_output('stderr') as stream:
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
517 warning_tests.inner(text)
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
518 result = stream.getvalue()
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 53315
diff changeset
519 self.assertEqual(result.count('\n'), 2,
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
520 "Too many newlines in %r" % result)
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
521 first_line, second_line = result.split('\n', 1)
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
522 expected_file = os.path.splitext(warning_tests.__file__)[0] + '.py'
46432
4afdc68ea52e Fix spliting on colons on Windows machines with a file path by limiting the
Brett Cannon <bcannon@gmail.com>
parents: 46431
diff changeset
523 first_line_parts = first_line.rsplit(':', 3)
46431
028d8625409b Add an explicit check for output in a test to try to diagnose a failure on
Brett Cannon <bcannon@gmail.com>
parents: 46424
diff changeset
524 path, line, warning_class, message = first_line_parts
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
525 line = int(line)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 53315
diff changeset
526 self.assertEqual(expected_file, path)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 53315
diff changeset
527 self.assertEqual(warning_class, ' ' + UserWarning.__name__)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 53315
diff changeset
528 self.assertEqual(message, ' ' + text)
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
529 expected_line = ' ' + linecache.getline(path, line).strip() + '\n'
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
530 assert expected_line
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 53315
diff changeset
531 self.assertEqual(second_line, expected_line)
41373
ac255a42ec0e Add tests for the filename.
Walter Dörwald <walter@livinglogic.de>
parents: 41372
diff changeset
532
71175
fc46acf7a645 Issue #12467: warnings: fix a race condition if a warning is emitted at
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68588
diff changeset
533 def test_filename_none(self):
fc46acf7a645 Issue #12467: warnings: fix a race condition if a warning is emitted at
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68588
diff changeset
534 # issue #12467: race condition if a warning is emitted at shutdown
fc46acf7a645 Issue #12467: warnings: fix a race condition if a warning is emitted at
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68588
diff changeset
535 globals_dict = globals()
fc46acf7a645 Issue #12467: warnings: fix a race condition if a warning is emitted at
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68588
diff changeset
536 oldfile = globals_dict['__file__']
fc46acf7a645 Issue #12467: warnings: fix a race condition if a warning is emitted at
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68588
diff changeset
537 try:
fc46acf7a645 Issue #12467: warnings: fix a race condition if a warning is emitted at
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68588
diff changeset
538 with original_warnings.catch_warnings(module=self.module) as w:
fc46acf7a645 Issue #12467: warnings: fix a race condition if a warning is emitted at
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68588
diff changeset
539 self.module.filterwarnings("always", category=UserWarning)
fc46acf7a645 Issue #12467: warnings: fix a race condition if a warning is emitted at
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68588
diff changeset
540 globals_dict['__file__'] = None
fc46acf7a645 Issue #12467: warnings: fix a race condition if a warning is emitted at
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68588
diff changeset
541 self.module.warn('test', UserWarning)
fc46acf7a645 Issue #12467: warnings: fix a race condition if a warning is emitted at
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68588
diff changeset
542 finally:
fc46acf7a645 Issue #12467: warnings: fix a race condition if a warning is emitted at
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68588
diff changeset
543 globals_dict['__file__'] = oldfile
fc46acf7a645 Issue #12467: warnings: fix a race condition if a warning is emitted at
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68588
diff changeset
544
38912
58034084ef44 'warning's was improperly requiring that a command-line Warning category be
Brett Cannon <bcannon@gmail.com>
parents: 37767
diff changeset
545
44359
bb716ebcb082 Add tests for the warnings module; specifically formatwarning and showwarning.
Brett Cannon <bcannon@gmail.com>
parents: 42659
diff changeset
546 class WarningsDisplayTests(unittest.TestCase):
bb716ebcb082 Add tests for the warnings module; specifically formatwarning and showwarning.
Brett Cannon <bcannon@gmail.com>
parents: 42659
diff changeset
547
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
548 """Test the displaying of warnings and the ability to overload functions
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
549 related to displaying warnings."""
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
550
44359
bb716ebcb082 Add tests for the warnings module; specifically formatwarning and showwarning.
Brett Cannon <bcannon@gmail.com>
parents: 42659
diff changeset
551 def test_formatwarning(self):
bb716ebcb082 Add tests for the warnings module; specifically formatwarning and showwarning.
Brett Cannon <bcannon@gmail.com>
parents: 42659
diff changeset
552 message = "msg"
bb716ebcb082 Add tests for the warnings module; specifically formatwarning and showwarning.
Brett Cannon <bcannon@gmail.com>
parents: 42659
diff changeset
553 category = Warning
bb716ebcb082 Add tests for the warnings module; specifically formatwarning and showwarning.
Brett Cannon <bcannon@gmail.com>
parents: 42659
diff changeset
554 file_name = os.path.splitext(warning_tests.__file__)[0] + '.py'
bb716ebcb082 Add tests for the warnings module; specifically formatwarning and showwarning.
Brett Cannon <bcannon@gmail.com>
parents: 42659
diff changeset
555 line_num = 3
bb716ebcb082 Add tests for the warnings module; specifically formatwarning and showwarning.
Brett Cannon <bcannon@gmail.com>
parents: 42659
diff changeset
556 file_line = linecache.getline(file_name, line_num).strip()
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
557 format = "%s:%s: %s: %s\n %s\n"
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
558 expect = format % (file_name, line_num, category.__name__, message,
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
559 file_line)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 53315
diff changeset
560 self.assertEqual(expect, self.module.formatwarning(message,
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
561 category, file_name, line_num))
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
562 # Test the 'line' argument.
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
563 file_line += " for the win!"
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
564 expect = format % (file_name, line_num, category.__name__, message,
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
565 file_line)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 53315
diff changeset
566 self.assertEqual(expect, self.module.formatwarning(message,
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
567 category, file_name, line_num, file_line))
44359
bb716ebcb082 Add tests for the warnings module; specifically formatwarning and showwarning.
Brett Cannon <bcannon@gmail.com>
parents: 42659
diff changeset
568
bb716ebcb082 Add tests for the warnings module; specifically formatwarning and showwarning.
Brett Cannon <bcannon@gmail.com>
parents: 42659
diff changeset
569 def test_showwarning(self):
bb716ebcb082 Add tests for the warnings module; specifically formatwarning and showwarning.
Brett Cannon <bcannon@gmail.com>
parents: 42659
diff changeset
570 file_name = os.path.splitext(warning_tests.__file__)[0] + '.py'
bb716ebcb082 Add tests for the warnings module; specifically formatwarning and showwarning.
Brett Cannon <bcannon@gmail.com>
parents: 42659
diff changeset
571 line_num = 3
bb716ebcb082 Add tests for the warnings module; specifically formatwarning and showwarning.
Brett Cannon <bcannon@gmail.com>
parents: 42659
diff changeset
572 expected_file_line = linecache.getline(file_name, line_num).strip()
bb716ebcb082 Add tests for the warnings module; specifically formatwarning and showwarning.
Brett Cannon <bcannon@gmail.com>
parents: 42659
diff changeset
573 message = 'msg'
bb716ebcb082 Add tests for the warnings module; specifically formatwarning and showwarning.
Brett Cannon <bcannon@gmail.com>
parents: 42659
diff changeset
574 category = Warning
bb716ebcb082 Add tests for the warnings module; specifically formatwarning and showwarning.
Brett Cannon <bcannon@gmail.com>
parents: 42659
diff changeset
575 file_object = StringIO.StringIO()
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
576 expect = self.module.formatwarning(message, category, file_name,
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
577 line_num)
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
578 self.module.showwarning(message, category, file_name, line_num,
44359
bb716ebcb082 Add tests for the warnings module; specifically formatwarning and showwarning.
Brett Cannon <bcannon@gmail.com>
parents: 42659
diff changeset
579 file_object)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 53315
diff changeset
580 self.assertEqual(file_object.getvalue(), expect)
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
581 # Test 'line' argument.
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
582 expected_file_line += "for the win!"
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
583 expect = self.module.formatwarning(message, category, file_name,
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
584 line_num, expected_file_line)
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
585 file_object = StringIO.StringIO()
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
586 self.module.showwarning(message, category, file_name, line_num,
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
587 file_object, expected_file_line)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 53315
diff changeset
588 self.assertEqual(expect, file_object.getvalue())
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
589
46424
8f2b65ec7bba Fix test_warnings by making the state of things more consistent for each test
Brett Cannon <bcannon@gmail.com>
parents: 46414
diff changeset
590 class CWarningsDisplayTests(BaseTest, WarningsDisplayTests):
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
591 module = c_warnings
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
592
46424
8f2b65ec7bba Fix test_warnings by making the state of things more consistent for each test
Brett Cannon <bcannon@gmail.com>
parents: 46414
diff changeset
593 class PyWarningsDisplayTests(BaseTest, WarningsDisplayTests):
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
594 module = py_warnings
44359
bb716ebcb082 Add tests for the warnings module; specifically formatwarning and showwarning.
Brett Cannon <bcannon@gmail.com>
parents: 42659
diff changeset
595
bb716ebcb082 Add tests for the warnings module; specifically formatwarning and showwarning.
Brett Cannon <bcannon@gmail.com>
parents: 42659
diff changeset
596
48948
d563621d76db Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents: 47942
diff changeset
597 class CatchWarningTests(BaseTest):
47942
b35c8b1940af Make test.test_support.catch_warnings more robust as discussed on python-dev. Also add explicit tests for itto test_warnings.
Nick Coghlan <ncoghlan@gmail.com>
parents: 47835
diff changeset
598
48948
d563621d76db Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents: 47942
diff changeset
599 """Test catch_warnings()."""
d563621d76db Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents: 47942
diff changeset
600
d563621d76db Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents: 47942
diff changeset
601 def test_catch_warnings_restore(self):
47942
b35c8b1940af Make test.test_support.catch_warnings more robust as discussed on python-dev. Also add explicit tests for itto test_warnings.
Nick Coghlan <ncoghlan@gmail.com>
parents: 47835
diff changeset
602 wmod = self.module
b35c8b1940af Make test.test_support.catch_warnings more robust as discussed on python-dev. Also add explicit tests for itto test_warnings.
Nick Coghlan <ncoghlan@gmail.com>
parents: 47835
diff changeset
603 orig_filters = wmod.filters
b35c8b1940af Make test.test_support.catch_warnings more robust as discussed on python-dev. Also add explicit tests for itto test_warnings.
Nick Coghlan <ncoghlan@gmail.com>
parents: 47835
diff changeset
604 orig_showwarning = wmod.showwarning
49146
f28b999a69f8 Issue #3781: Final cleanup of warnings.catch_warnings and its usage in the test suite. Closes issue w.r.t. 2.6 (R: Brett Cannon)
Nick Coghlan <ncoghlan@gmail.com>
parents: 49099
diff changeset
605 # Ensure both showwarning and filters are restored when recording
f28b999a69f8 Issue #3781: Final cleanup of warnings.catch_warnings and its usage in the test suite. Closes issue w.r.t. 2.6 (R: Brett Cannon)
Nick Coghlan <ncoghlan@gmail.com>
parents: 49099
diff changeset
606 with wmod.catch_warnings(module=wmod, record=True):
47942
b35c8b1940af Make test.test_support.catch_warnings more robust as discussed on python-dev. Also add explicit tests for itto test_warnings.
Nick Coghlan <ncoghlan@gmail.com>
parents: 47835
diff changeset
607 wmod.filters = wmod.showwarning = object()
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 53315
diff changeset
608 self.assertTrue(wmod.filters is orig_filters)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 53315
diff changeset
609 self.assertTrue(wmod.showwarning is orig_showwarning)
49146
f28b999a69f8 Issue #3781: Final cleanup of warnings.catch_warnings and its usage in the test suite. Closes issue w.r.t. 2.6 (R: Brett Cannon)
Nick Coghlan <ncoghlan@gmail.com>
parents: 49099
diff changeset
610 # Same test, but with recording disabled
48948
d563621d76db Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents: 47942
diff changeset
611 with wmod.catch_warnings(module=wmod, record=False):
47942
b35c8b1940af Make test.test_support.catch_warnings more robust as discussed on python-dev. Also add explicit tests for itto test_warnings.
Nick Coghlan <ncoghlan@gmail.com>
parents: 47835
diff changeset
612 wmod.filters = wmod.showwarning = object()
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 53315
diff changeset
613 self.assertTrue(wmod.filters is orig_filters)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 53315
diff changeset
614 self.assertTrue(wmod.showwarning is orig_showwarning)
47942
b35c8b1940af Make test.test_support.catch_warnings more robust as discussed on python-dev. Also add explicit tests for itto test_warnings.
Nick Coghlan <ncoghlan@gmail.com>
parents: 47835
diff changeset
615
48948
d563621d76db Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents: 47942
diff changeset
616 def test_catch_warnings_recording(self):
47942
b35c8b1940af Make test.test_support.catch_warnings more robust as discussed on python-dev. Also add explicit tests for itto test_warnings.
Nick Coghlan <ncoghlan@gmail.com>
parents: 47835
diff changeset
617 wmod = self.module
49146
f28b999a69f8 Issue #3781: Final cleanup of warnings.catch_warnings and its usage in the test suite. Closes issue w.r.t. 2.6 (R: Brett Cannon)
Nick Coghlan <ncoghlan@gmail.com>
parents: 49099
diff changeset
618 # Ensure warnings are recorded when requested
48948
d563621d76db Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents: 47942
diff changeset
619 with wmod.catch_warnings(module=wmod, record=True) as w:
d563621d76db Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents: 47942
diff changeset
620 self.assertEqual(w, [])
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 53315
diff changeset
621 self.assertTrue(type(w) is list)
47942
b35c8b1940af Make test.test_support.catch_warnings more robust as discussed on python-dev. Also add explicit tests for itto test_warnings.
Nick Coghlan <ncoghlan@gmail.com>
parents: 47835
diff changeset
622 wmod.simplefilter("always")
b35c8b1940af Make test.test_support.catch_warnings more robust as discussed on python-dev. Also add explicit tests for itto test_warnings.
Nick Coghlan <ncoghlan@gmail.com>
parents: 47835
diff changeset
623 wmod.warn("foo")
49099
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
624 self.assertEqual(str(w[-1].message), "foo")
47942
b35c8b1940af Make test.test_support.catch_warnings more robust as discussed on python-dev. Also add explicit tests for itto test_warnings.
Nick Coghlan <ncoghlan@gmail.com>
parents: 47835
diff changeset
625 wmod.warn("bar")
49099
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
626 self.assertEqual(str(w[-1].message), "bar")
48948
d563621d76db Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents: 47942
diff changeset
627 self.assertEqual(str(w[0].message), "foo")
d563621d76db Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents: 47942
diff changeset
628 self.assertEqual(str(w[1].message), "bar")
49099
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
629 del w[:]
48948
d563621d76db Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents: 47942
diff changeset
630 self.assertEqual(w, [])
49146
f28b999a69f8 Issue #3781: Final cleanup of warnings.catch_warnings and its usage in the test suite. Closes issue w.r.t. 2.6 (R: Brett Cannon)
Nick Coghlan <ncoghlan@gmail.com>
parents: 49099
diff changeset
631 # Ensure warnings are not recorded when not requested
47942
b35c8b1940af Make test.test_support.catch_warnings more robust as discussed on python-dev. Also add explicit tests for itto test_warnings.
Nick Coghlan <ncoghlan@gmail.com>
parents: 47835
diff changeset
632 orig_showwarning = wmod.showwarning
48948
d563621d76db Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents: 47942
diff changeset
633 with wmod.catch_warnings(module=wmod, record=False) as w:
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 53315
diff changeset
634 self.assertTrue(w is None)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 53315
diff changeset
635 self.assertTrue(wmod.showwarning is orig_showwarning)
47942
b35c8b1940af Make test.test_support.catch_warnings more robust as discussed on python-dev. Also add explicit tests for itto test_warnings.
Nick Coghlan <ncoghlan@gmail.com>
parents: 47835
diff changeset
636
49146
f28b999a69f8 Issue #3781: Final cleanup of warnings.catch_warnings and its usage in the test suite. Closes issue w.r.t. 2.6 (R: Brett Cannon)
Nick Coghlan <ncoghlan@gmail.com>
parents: 49099
diff changeset
637 def test_catch_warnings_reentry_guard(self):
f28b999a69f8 Issue #3781: Final cleanup of warnings.catch_warnings and its usage in the test suite. Closes issue w.r.t. 2.6 (R: Brett Cannon)
Nick Coghlan <ncoghlan@gmail.com>
parents: 49099
diff changeset
638 wmod = self.module
f28b999a69f8 Issue #3781: Final cleanup of warnings.catch_warnings and its usage in the test suite. Closes issue w.r.t. 2.6 (R: Brett Cannon)
Nick Coghlan <ncoghlan@gmail.com>
parents: 49099
diff changeset
639 # Ensure catch_warnings is protected against incorrect usage
f28b999a69f8 Issue #3781: Final cleanup of warnings.catch_warnings and its usage in the test suite. Closes issue w.r.t. 2.6 (R: Brett Cannon)
Nick Coghlan <ncoghlan@gmail.com>
parents: 49099
diff changeset
640 x = wmod.catch_warnings(module=wmod, record=True)
f28b999a69f8 Issue #3781: Final cleanup of warnings.catch_warnings and its usage in the test suite. Closes issue w.r.t. 2.6 (R: Brett Cannon)
Nick Coghlan <ncoghlan@gmail.com>
parents: 49099
diff changeset
641 self.assertRaises(RuntimeError, x.__exit__)
f28b999a69f8 Issue #3781: Final cleanup of warnings.catch_warnings and its usage in the test suite. Closes issue w.r.t. 2.6 (R: Brett Cannon)
Nick Coghlan <ncoghlan@gmail.com>
parents: 49099
diff changeset
642 with x:
f28b999a69f8 Issue #3781: Final cleanup of warnings.catch_warnings and its usage in the test suite. Closes issue w.r.t. 2.6 (R: Brett Cannon)
Nick Coghlan <ncoghlan@gmail.com>
parents: 49099
diff changeset
643 self.assertRaises(RuntimeError, x.__enter__)
f28b999a69f8 Issue #3781: Final cleanup of warnings.catch_warnings and its usage in the test suite. Closes issue w.r.t. 2.6 (R: Brett Cannon)
Nick Coghlan <ncoghlan@gmail.com>
parents: 49099
diff changeset
644 # Same test, but with recording disabled
f28b999a69f8 Issue #3781: Final cleanup of warnings.catch_warnings and its usage in the test suite. Closes issue w.r.t. 2.6 (R: Brett Cannon)
Nick Coghlan <ncoghlan@gmail.com>
parents: 49099
diff changeset
645 x = wmod.catch_warnings(module=wmod, record=False)
f28b999a69f8 Issue #3781: Final cleanup of warnings.catch_warnings and its usage in the test suite. Closes issue w.r.t. 2.6 (R: Brett Cannon)
Nick Coghlan <ncoghlan@gmail.com>
parents: 49099
diff changeset
646 self.assertRaises(RuntimeError, x.__exit__)
f28b999a69f8 Issue #3781: Final cleanup of warnings.catch_warnings and its usage in the test suite. Closes issue w.r.t. 2.6 (R: Brett Cannon)
Nick Coghlan <ncoghlan@gmail.com>
parents: 49099
diff changeset
647 with x:
f28b999a69f8 Issue #3781: Final cleanup of warnings.catch_warnings and its usage in the test suite. Closes issue w.r.t. 2.6 (R: Brett Cannon)
Nick Coghlan <ncoghlan@gmail.com>
parents: 49099
diff changeset
648 self.assertRaises(RuntimeError, x.__enter__)
f28b999a69f8 Issue #3781: Final cleanup of warnings.catch_warnings and its usage in the test suite. Closes issue w.r.t. 2.6 (R: Brett Cannon)
Nick Coghlan <ncoghlan@gmail.com>
parents: 49099
diff changeset
649
f28b999a69f8 Issue #3781: Final cleanup of warnings.catch_warnings and its usage in the test suite. Closes issue w.r.t. 2.6 (R: Brett Cannon)
Nick Coghlan <ncoghlan@gmail.com>
parents: 49099
diff changeset
650 def test_catch_warnings_defaults(self):
f28b999a69f8 Issue #3781: Final cleanup of warnings.catch_warnings and its usage in the test suite. Closes issue w.r.t. 2.6 (R: Brett Cannon)
Nick Coghlan <ncoghlan@gmail.com>
parents: 49099
diff changeset
651 wmod = self.module
f28b999a69f8 Issue #3781: Final cleanup of warnings.catch_warnings and its usage in the test suite. Closes issue w.r.t. 2.6 (R: Brett Cannon)
Nick Coghlan <ncoghlan@gmail.com>
parents: 49099
diff changeset
652 orig_filters = wmod.filters
f28b999a69f8 Issue #3781: Final cleanup of warnings.catch_warnings and its usage in the test suite. Closes issue w.r.t. 2.6 (R: Brett Cannon)
Nick Coghlan <ncoghlan@gmail.com>
parents: 49099
diff changeset
653 orig_showwarning = wmod.showwarning
f28b999a69f8 Issue #3781: Final cleanup of warnings.catch_warnings and its usage in the test suite. Closes issue w.r.t. 2.6 (R: Brett Cannon)
Nick Coghlan <ncoghlan@gmail.com>
parents: 49099
diff changeset
654 # Ensure default behaviour is not to record warnings
f28b999a69f8 Issue #3781: Final cleanup of warnings.catch_warnings and its usage in the test suite. Closes issue w.r.t. 2.6 (R: Brett Cannon)
Nick Coghlan <ncoghlan@gmail.com>
parents: 49099
diff changeset
655 with wmod.catch_warnings(module=wmod) as w:
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 53315
diff changeset
656 self.assertTrue(w is None)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 53315
diff changeset
657 self.assertTrue(wmod.showwarning is orig_showwarning)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 53315
diff changeset
658 self.assertTrue(wmod.filters is not orig_filters)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 53315
diff changeset
659 self.assertTrue(wmod.filters is orig_filters)
49146
f28b999a69f8 Issue #3781: Final cleanup of warnings.catch_warnings and its usage in the test suite. Closes issue w.r.t. 2.6 (R: Brett Cannon)
Nick Coghlan <ncoghlan@gmail.com>
parents: 49099
diff changeset
660 if wmod is sys.modules['warnings']:
f28b999a69f8 Issue #3781: Final cleanup of warnings.catch_warnings and its usage in the test suite. Closes issue w.r.t. 2.6 (R: Brett Cannon)
Nick Coghlan <ncoghlan@gmail.com>
parents: 49099
diff changeset
661 # Ensure the default module is this one
f28b999a69f8 Issue #3781: Final cleanup of warnings.catch_warnings and its usage in the test suite. Closes issue w.r.t. 2.6 (R: Brett Cannon)
Nick Coghlan <ncoghlan@gmail.com>
parents: 49099
diff changeset
662 with wmod.catch_warnings() as w:
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 53315
diff changeset
663 self.assertTrue(w is None)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 53315
diff changeset
664 self.assertTrue(wmod.showwarning is orig_showwarning)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 53315
diff changeset
665 self.assertTrue(wmod.filters is not orig_filters)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 53315
diff changeset
666 self.assertTrue(wmod.filters is orig_filters)
49146
f28b999a69f8 Issue #3781: Final cleanup of warnings.catch_warnings and its usage in the test suite. Closes issue w.r.t. 2.6 (R: Brett Cannon)
Nick Coghlan <ncoghlan@gmail.com>
parents: 49099
diff changeset
667
f28b999a69f8 Issue #3781: Final cleanup of warnings.catch_warnings and its usage in the test suite. Closes issue w.r.t. 2.6 (R: Brett Cannon)
Nick Coghlan <ncoghlan@gmail.com>
parents: 49099
diff changeset
668 def test_check_warnings(self):
f28b999a69f8 Issue #3781: Final cleanup of warnings.catch_warnings and its usage in the test suite. Closes issue w.r.t. 2.6 (R: Brett Cannon)
Nick Coghlan <ncoghlan@gmail.com>
parents: 49099
diff changeset
669 # Explicit tests for the test_support convenience wrapper
f28b999a69f8 Issue #3781: Final cleanup of warnings.catch_warnings and its usage in the test suite. Closes issue w.r.t. 2.6 (R: Brett Cannon)
Nick Coghlan <ncoghlan@gmail.com>
parents: 49099
diff changeset
670 wmod = self.module
63619
a2f013021e1d Merged revisions 78769,79049 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60060
diff changeset
671 if wmod is not sys.modules['warnings']:
a2f013021e1d Merged revisions 78769,79049 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60060
diff changeset
672 return
a2f013021e1d Merged revisions 78769,79049 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60060
diff changeset
673 with test_support.check_warnings(quiet=False) as w:
a2f013021e1d Merged revisions 78769,79049 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60060
diff changeset
674 self.assertEqual(w.warnings, [])
a2f013021e1d Merged revisions 78769,79049 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60060
diff changeset
675 wmod.simplefilter("always")
a2f013021e1d Merged revisions 78769,79049 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60060
diff changeset
676 wmod.warn("foo")
a2f013021e1d Merged revisions 78769,79049 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60060
diff changeset
677 self.assertEqual(str(w.message), "foo")
a2f013021e1d Merged revisions 78769,79049 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60060
diff changeset
678 wmod.warn("bar")
a2f013021e1d Merged revisions 78769,79049 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60060
diff changeset
679 self.assertEqual(str(w.message), "bar")
a2f013021e1d Merged revisions 78769,79049 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60060
diff changeset
680 self.assertEqual(str(w.warnings[0].message), "foo")
a2f013021e1d Merged revisions 78769,79049 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60060
diff changeset
681 self.assertEqual(str(w.warnings[1].message), "bar")
a2f013021e1d Merged revisions 78769,79049 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60060
diff changeset
682 w.reset()
a2f013021e1d Merged revisions 78769,79049 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60060
diff changeset
683 self.assertEqual(w.warnings, [])
a2f013021e1d Merged revisions 78769,79049 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60060
diff changeset
684
a2f013021e1d Merged revisions 78769,79049 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60060
diff changeset
685 with test_support.check_warnings():
a2f013021e1d Merged revisions 78769,79049 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60060
diff changeset
686 # defaults to quiet=True without argument
a2f013021e1d Merged revisions 78769,79049 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60060
diff changeset
687 pass
a2f013021e1d Merged revisions 78769,79049 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60060
diff changeset
688 with test_support.check_warnings(('foo', UserWarning)):
a2f013021e1d Merged revisions 78769,79049 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60060
diff changeset
689 wmod.warn("foo")
a2f013021e1d Merged revisions 78769,79049 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60060
diff changeset
690
59683
76f9e3b6c584 #8155: Preserve backward compatibility for test_support.check_warnings(). Add regression tests.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58460
diff changeset
691 with self.assertRaises(AssertionError):
63619
a2f013021e1d Merged revisions 78769,79049 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60060
diff changeset
692 with test_support.check_warnings(('', RuntimeWarning)):
a2f013021e1d Merged revisions 78769,79049 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60060
diff changeset
693 # defaults to quiet=False with argument
a2f013021e1d Merged revisions 78769,79049 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60060
diff changeset
694 pass
59683
76f9e3b6c584 #8155: Preserve backward compatibility for test_support.check_warnings(). Add regression tests.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58460
diff changeset
695 with self.assertRaises(AssertionError):
63619
a2f013021e1d Merged revisions 78769,79049 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60060
diff changeset
696 with test_support.check_warnings(('foo', RuntimeWarning)):
49146
f28b999a69f8 Issue #3781: Final cleanup of warnings.catch_warnings and its usage in the test suite. Closes issue w.r.t. 2.6 (R: Brett Cannon)
Nick Coghlan <ncoghlan@gmail.com>
parents: 49099
diff changeset
697 wmod.warn("foo")
f28b999a69f8 Issue #3781: Final cleanup of warnings.catch_warnings and its usage in the test suite. Closes issue w.r.t. 2.6 (R: Brett Cannon)
Nick Coghlan <ncoghlan@gmail.com>
parents: 49099
diff changeset
698
f28b999a69f8 Issue #3781: Final cleanup of warnings.catch_warnings and its usage in the test suite. Closes issue w.r.t. 2.6 (R: Brett Cannon)
Nick Coghlan <ncoghlan@gmail.com>
parents: 49099
diff changeset
699
48948
d563621d76db Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents: 47942
diff changeset
700 class CCatchWarningTests(CatchWarningTests):
47942
b35c8b1940af Make test.test_support.catch_warnings more robust as discussed on python-dev. Also add explicit tests for itto test_warnings.
Nick Coghlan <ncoghlan@gmail.com>
parents: 47835
diff changeset
701 module = c_warnings
b35c8b1940af Make test.test_support.catch_warnings more robust as discussed on python-dev. Also add explicit tests for itto test_warnings.
Nick Coghlan <ncoghlan@gmail.com>
parents: 47835
diff changeset
702
48948
d563621d76db Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents: 47942
diff changeset
703 class PyCatchWarningTests(CatchWarningTests):
47942
b35c8b1940af Make test.test_support.catch_warnings more robust as discussed on python-dev. Also add explicit tests for itto test_warnings.
Nick Coghlan <ncoghlan@gmail.com>
parents: 47835
diff changeset
704 module = py_warnings
b35c8b1940af Make test.test_support.catch_warnings more robust as discussed on python-dev. Also add explicit tests for itto test_warnings.
Nick Coghlan <ncoghlan@gmail.com>
parents: 47835
diff changeset
705
b35c8b1940af Make test.test_support.catch_warnings more robust as discussed on python-dev. Also add explicit tests for itto test_warnings.
Nick Coghlan <ncoghlan@gmail.com>
parents: 47835
diff changeset
706
60433
c0957dada5c2 #7301: add the environment variable $PYTHONWARNINGS to supplement the -W
Philip Jenvey <pjenvey@underboss.org>
parents: 60020
diff changeset
707 class EnvironmentVariableTests(BaseTest):
46667
d9071fa8244a Add a DeprecationWarning for when warnings.showwarning() is set to a function
Brett Cannon <bcannon@gmail.com>
parents: 46637
diff changeset
708
60433
c0957dada5c2 #7301: add the environment variable $PYTHONWARNINGS to supplement the -W
Philip Jenvey <pjenvey@underboss.org>
parents: 60020
diff changeset
709 def test_single_warning(self):
c0957dada5c2 #7301: add the environment variable $PYTHONWARNINGS to supplement the -W
Philip Jenvey <pjenvey@underboss.org>
parents: 60020
diff changeset
710 newenv = os.environ.copy()
c0957dada5c2 #7301: add the environment variable $PYTHONWARNINGS to supplement the -W
Philip Jenvey <pjenvey@underboss.org>
parents: 60020
diff changeset
711 newenv["PYTHONWARNINGS"] = "ignore::DeprecationWarning"
c0957dada5c2 #7301: add the environment variable $PYTHONWARNINGS to supplement the -W
Philip Jenvey <pjenvey@underboss.org>
parents: 60020
diff changeset
712 p = subprocess.Popen([sys.executable,
c0957dada5c2 #7301: add the environment variable $PYTHONWARNINGS to supplement the -W
Philip Jenvey <pjenvey@underboss.org>
parents: 60020
diff changeset
713 "-c", "import sys; sys.stdout.write(str(sys.warnoptions))"],
c0957dada5c2 #7301: add the environment variable $PYTHONWARNINGS to supplement the -W
Philip Jenvey <pjenvey@underboss.org>
parents: 60020
diff changeset
714 stdout=subprocess.PIPE, env=newenv)
60484
f34cb501f8f7 fix PYTHONWARNINGS handling to not modify the original env value and improve
Philip Jenvey <pjenvey@underboss.org>
parents: 60433
diff changeset
715 self.assertEqual(p.communicate()[0], "['ignore::DeprecationWarning']")
f34cb501f8f7 fix PYTHONWARNINGS handling to not modify the original env value and improve
Philip Jenvey <pjenvey@underboss.org>
parents: 60433
diff changeset
716 self.assertEqual(p.wait(), 0)
48948
d563621d76db Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents: 47942
diff changeset
717
60433
c0957dada5c2 #7301: add the environment variable $PYTHONWARNINGS to supplement the -W
Philip Jenvey <pjenvey@underboss.org>
parents: 60020
diff changeset
718 def test_comma_separated_warnings(self):
c0957dada5c2 #7301: add the environment variable $PYTHONWARNINGS to supplement the -W
Philip Jenvey <pjenvey@underboss.org>
parents: 60020
diff changeset
719 newenv = os.environ.copy()
c0957dada5c2 #7301: add the environment variable $PYTHONWARNINGS to supplement the -W
Philip Jenvey <pjenvey@underboss.org>
parents: 60020
diff changeset
720 newenv["PYTHONWARNINGS"] = ("ignore::DeprecationWarning,"
c0957dada5c2 #7301: add the environment variable $PYTHONWARNINGS to supplement the -W
Philip Jenvey <pjenvey@underboss.org>
parents: 60020
diff changeset
721 "ignore::UnicodeWarning")
c0957dada5c2 #7301: add the environment variable $PYTHONWARNINGS to supplement the -W
Philip Jenvey <pjenvey@underboss.org>
parents: 60020
diff changeset
722 p = subprocess.Popen([sys.executable,
c0957dada5c2 #7301: add the environment variable $PYTHONWARNINGS to supplement the -W
Philip Jenvey <pjenvey@underboss.org>
parents: 60020
diff changeset
723 "-c", "import sys; sys.stdout.write(str(sys.warnoptions))"],
c0957dada5c2 #7301: add the environment variable $PYTHONWARNINGS to supplement the -W
Philip Jenvey <pjenvey@underboss.org>
parents: 60020
diff changeset
724 stdout=subprocess.PIPE, env=newenv)
60484
f34cb501f8f7 fix PYTHONWARNINGS handling to not modify the original env value and improve
Philip Jenvey <pjenvey@underboss.org>
parents: 60433
diff changeset
725 self.assertEqual(p.communicate()[0],
60433
c0957dada5c2 #7301: add the environment variable $PYTHONWARNINGS to supplement the -W
Philip Jenvey <pjenvey@underboss.org>
parents: 60020
diff changeset
726 "['ignore::DeprecationWarning', 'ignore::UnicodeWarning']")
60484
f34cb501f8f7 fix PYTHONWARNINGS handling to not modify the original env value and improve
Philip Jenvey <pjenvey@underboss.org>
parents: 60433
diff changeset
727 self.assertEqual(p.wait(), 0)
46667
d9071fa8244a Add a DeprecationWarning for when warnings.showwarning() is set to a function
Brett Cannon <bcannon@gmail.com>
parents: 46637
diff changeset
728
60433
c0957dada5c2 #7301: add the environment variable $PYTHONWARNINGS to supplement the -W
Philip Jenvey <pjenvey@underboss.org>
parents: 60020
diff changeset
729 def test_envvar_and_command_line(self):
c0957dada5c2 #7301: add the environment variable $PYTHONWARNINGS to supplement the -W
Philip Jenvey <pjenvey@underboss.org>
parents: 60020
diff changeset
730 newenv = os.environ.copy()
c0957dada5c2 #7301: add the environment variable $PYTHONWARNINGS to supplement the -W
Philip Jenvey <pjenvey@underboss.org>
parents: 60020
diff changeset
731 newenv["PYTHONWARNINGS"] = "ignore::DeprecationWarning"
c0957dada5c2 #7301: add the environment variable $PYTHONWARNINGS to supplement the -W
Philip Jenvey <pjenvey@underboss.org>
parents: 60020
diff changeset
732 p = subprocess.Popen([sys.executable, "-W" "ignore::UnicodeWarning",
c0957dada5c2 #7301: add the environment variable $PYTHONWARNINGS to supplement the -W
Philip Jenvey <pjenvey@underboss.org>
parents: 60020
diff changeset
733 "-c", "import sys; sys.stdout.write(str(sys.warnoptions))"],
c0957dada5c2 #7301: add the environment variable $PYTHONWARNINGS to supplement the -W
Philip Jenvey <pjenvey@underboss.org>
parents: 60020
diff changeset
734 stdout=subprocess.PIPE, env=newenv)
60484
f34cb501f8f7 fix PYTHONWARNINGS handling to not modify the original env value and improve
Philip Jenvey <pjenvey@underboss.org>
parents: 60433
diff changeset
735 self.assertEqual(p.communicate()[0],
60433
c0957dada5c2 #7301: add the environment variable $PYTHONWARNINGS to supplement the -W
Philip Jenvey <pjenvey@underboss.org>
parents: 60020
diff changeset
736 "['ignore::UnicodeWarning', 'ignore::DeprecationWarning']")
60484
f34cb501f8f7 fix PYTHONWARNINGS handling to not modify the original env value and improve
Philip Jenvey <pjenvey@underboss.org>
parents: 60433
diff changeset
737 self.assertEqual(p.wait(), 0)
60433
c0957dada5c2 #7301: add the environment variable $PYTHONWARNINGS to supplement the -W
Philip Jenvey <pjenvey@underboss.org>
parents: 60020
diff changeset
738
c0957dada5c2 #7301: add the environment variable $PYTHONWARNINGS to supplement the -W
Philip Jenvey <pjenvey@underboss.org>
parents: 60020
diff changeset
739 class CEnvironmentVariableTests(EnvironmentVariableTests):
46667
d9071fa8244a Add a DeprecationWarning for when warnings.showwarning() is set to a function
Brett Cannon <bcannon@gmail.com>
parents: 46637
diff changeset
740 module = c_warnings
d9071fa8244a Add a DeprecationWarning for when warnings.showwarning() is set to a function
Brett Cannon <bcannon@gmail.com>
parents: 46637
diff changeset
741
60433
c0957dada5c2 #7301: add the environment variable $PYTHONWARNINGS to supplement the -W
Philip Jenvey <pjenvey@underboss.org>
parents: 60020
diff changeset
742 class PyEnvironmentVariableTests(EnvironmentVariableTests):
46667
d9071fa8244a Add a DeprecationWarning for when warnings.showwarning() is set to a function
Brett Cannon <bcannon@gmail.com>
parents: 46637
diff changeset
743 module = py_warnings
d9071fa8244a Add a DeprecationWarning for when warnings.showwarning() is set to a function
Brett Cannon <bcannon@gmail.com>
parents: 46637
diff changeset
744
d9071fa8244a Add a DeprecationWarning for when warnings.showwarning() is set to a function
Brett Cannon <bcannon@gmail.com>
parents: 46637
diff changeset
745
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
746 def test_main():
46474
5c43159f5ad0 Some tests did not pass on repeated calls (regrtest -R::)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46432
diff changeset
747 py_warnings.onceregistry.clear()
5c43159f5ad0 Some tests did not pass on repeated calls (regrtest -R::)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46432
diff changeset
748 c_warnings.onceregistry.clear()
48948
d563621d76db Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents: 47942
diff changeset
749 test_support.run_unittest(CFilterTests, PyFilterTests,
d563621d76db Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents: 47942
diff changeset
750 CWarnTests, PyWarnTests,
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
751 CWCmdLineTests, PyWCmdLineTests,
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
752 _WarningsTests,
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
753 CWarningsDisplayTests, PyWarningsDisplayTests,
48948
d563621d76db Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents: 47942
diff changeset
754 CCatchWarningTests, PyCatchWarningTests,
60433
c0957dada5c2 #7301: add the environment variable $PYTHONWARNINGS to supplement the -W
Philip Jenvey <pjenvey@underboss.org>
parents: 60020
diff changeset
755 CEnvironmentVariableTests,
c0957dada5c2 #7301: add the environment variable $PYTHONWARNINGS to supplement the -W
Philip Jenvey <pjenvey@underboss.org>
parents: 60020
diff changeset
756 PyEnvironmentVariableTests
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
757 )
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
758
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
759
29837
64194e775b0b Reworked test_warnings.py:
Raymond Hettinger <python@rcn.com>
parents: 29836
diff changeset
760 if __name__ == "__main__":
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
761 test_main()