annotate Lib/test/test_warnings.py @ 54294:dba767bc3155 legacy-trunk

remove mention of old ctypes version
author Benjamin Peterson <benjamin@python.org>
date Sat, 23 May 2009 20:59:09 +0000
parents e9595cb5c786
children 6768b2350c57
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
64194e775b0b Reworked test_warnings.py:
Raymond Hettinger <python@rcn.com>
parents: 29836
diff changeset
7 from test import test_support
29813
7be24cc6c28d Change warnings to avoid importing re module during startup.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
8
41373
ac255a42ec0e Add tests for the filename.
Walter Dörwald <walter@livinglogic.de>
parents: 41372
diff changeset
9 import warning_tests
ac255a42ec0e Add tests for the filename.
Walter Dörwald <walter@livinglogic.de>
parents: 41372
diff changeset
10
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
11 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
12
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
13 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
14 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
15
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
16 @contextmanager
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
17 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
18 """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
19 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
20 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
21 try:
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
22 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
23 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
24 pass
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
25 try:
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
26 __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
27 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
28 pass
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
29 original_warnings = warning_tests.warnings
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
30 try:
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
31 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
32 yield
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
33 finally:
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
34 warning_tests.warnings = original_warnings
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
35
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
36
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
37 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
38
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
39 """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
40
29837
64194e775b0b Reworked test_warnings.py:
Raymond Hettinger <python@rcn.com>
parents: 29836
diff changeset
41 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
42 # 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
43 # 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
44 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
45 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
46 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
47 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
48 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
49 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
50 # 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
51 # 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
52 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
53 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
54
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 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
56 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
57 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
58
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
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 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
61
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 """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
63
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
64 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
65 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
66 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
67 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
68 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
69 "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
70
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
71 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
72 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
73 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
74 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
75 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
76 self.module.warn("FilterTests.test_ignore", UserWarning)
48948
d563621d76db Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents: 47942
diff changeset
77 self.assertEquals(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
78
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
79 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
80 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
81 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
82 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
83 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
84 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
85 self.module.warn(message, UserWarning)
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 self.assert_(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
87 self.module.warn(message, UserWarning)
49099
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
88 self.assert_(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
89
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
90 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
91 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
92 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
93 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
94 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
95 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
96 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
97 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
98 if x == 0:
49099
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
99 self.assertEquals(w[-1].message, message)
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
100 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
101 elif x == 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
102 self.assertEquals(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
103 else:
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
104 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
105
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
106 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
107 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
108 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
109 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
110 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
111 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
112 self.module.warn(message, UserWarning)
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 self.assertEquals(w[-1].message, message)
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
114 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
115 self.module.warn(message, UserWarning)
49099
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
116 self.assertEquals(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
117
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
118 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
119 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
120 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
121 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
122 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
123 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
124 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
125 42)
49099
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
126 self.assertEquals(w[-1].message, message)
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
127 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
128 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
129 13)
48948
d563621d76db Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents: 47942
diff changeset
130 self.assertEquals(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
131 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
132 42)
48948
d563621d76db Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents: 47942
diff changeset
133 self.assertEquals(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
134
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
135 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
136 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
137 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
138 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
139 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
140 "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
141
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
142 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
143 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
144 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
145 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
146 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
147 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
148 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
149 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
150 try:
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.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
152 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
153 self.fail("order handling for actions failed")
48948
d563621d76db Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents: 47942
diff changeset
154 self.assertEquals(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
155
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
156 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
157 # 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
158 # 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
159 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
160 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
161 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
162 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
163
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
164 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
165 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
166 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
167 self.assertEqual(str(w[-1].message), text)
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
168 self.assert_(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
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.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
171 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
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.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
174
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
175 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
176 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
177 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
178 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
179 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
180 self.assertEqual(str(w[-1].message), text)
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
181 self.assert_(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
182
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
183 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
184 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
185
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
186 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
187 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
188
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
189
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
190 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
191
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
192 """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
193
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
194 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
195 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
196 module=self.module) as w:
41375
cb6cce4d4232 Implement a contextmanager test.test_support.catch_warning that can
Walter Dörwald <walter@livinglogic.de>
parents: 41373
diff changeset
197 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
198 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
199 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
200 self.assertEqual(str(w[-1].message), text)
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
201 self.assert_(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
202
41373
ac255a42ec0e Add tests for the filename.
Walter Dörwald <walter@livinglogic.de>
parents: 41372
diff changeset
203 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
204 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
205 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
206 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
207 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
208 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
209 "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
210 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
211 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
212 "warning_tests.py")
41373
ac255a42ec0e Add tests for the filename.
Walter Dörwald <walter@livinglogic.de>
parents: 41372
diff changeset
213
ac255a42ec0e Add tests for the filename.
Walter Dörwald <walter@livinglogic.de>
parents: 41372
diff changeset
214 def test_stacklevel(self):
ac255a42ec0e Add tests for the filename.
Walter Dörwald <walter@livinglogic.de>
parents: 41372
diff changeset
215 # Test stacklevel argument
ac255a42ec0e Add tests for the filename.
Walter Dörwald <walter@livinglogic.de>
parents: 41372
diff changeset
216 # 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
217 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
218 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
219 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
220 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
221 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
222 "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
223 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
224 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
225 "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
226
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("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
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 "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
230 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
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")
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
233 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
234 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
235 "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
236
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.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
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 "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
240
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
241 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
242 # 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
243 # 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
244 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
245 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
246 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
247 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
248 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
249 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
250 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
251 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
252 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
253 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
254
a859f835ef53 Fix the C implementation of 'warnings' to infer the filename of the module that
Brett Cannon <bcannon@gmail.com>
parents: 46474
diff changeset
255 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
256 # 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
257 # 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
258 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
259 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
260 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
261 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
262 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
263 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
264 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
265 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
266 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
267 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
268 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
269 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
270 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
271 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
272 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
273
a859f835ef53 Fix the C implementation of 'warnings' to infer the filename of the module that
Brett Cannon <bcannon@gmail.com>
parents: 46474
diff changeset
274 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
275 # 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
276 # 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
277 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
278 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
279 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
280 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
281 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
282 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
283 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
284 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
285 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
286 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
287 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
288 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
289 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
290 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
291 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
292 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
293
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 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
295 # 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
296 # 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
297 # 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
298 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
299 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
300 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
301 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
302 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
303 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
304 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
305 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
306 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
307 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
308 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
309 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
310 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
311 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
312 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
313 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
314
47835
dc42efd728ee warnings.warn_explicit() did not have the proper TypeErrors in place to prevent
Brett Cannon <bcannon@gmail.com>
parents: 46993
diff changeset
315 def test_warn_explicit_type_errors(self):
dc42efd728ee warnings.warn_explicit() did not have the proper TypeErrors in place to prevent
Brett Cannon <bcannon@gmail.com>
parents: 46993
diff changeset
316 # warn_explicit() shoud error out gracefully if it is given objects
dc42efd728ee warnings.warn_explicit() did not have the proper TypeErrors in place to prevent
Brett Cannon <bcannon@gmail.com>
parents: 46993
diff changeset
317 # 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
318 # 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
319 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
320 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
321 # 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
322 # 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
323 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
324 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
325 # '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
326 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
327 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
328 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
329
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
330
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
331 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
332 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
333
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
334 # 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
335 # 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
336 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
337 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
338 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
339
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
340 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
341 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
342
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
343 # 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
344 # 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
345 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
346 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
347 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
348
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
349
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
350 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
351
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
352 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
353 # 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
354 # 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
355 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
356 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
357 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
358 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
359 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
360 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
361 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
362 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
363 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
364
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
365 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
366 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
367
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
368 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
369 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
370
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
371
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
372 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
373
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
374 """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
375
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
376 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
377
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
378 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
379 # 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
380 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
381 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
382 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
383 '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 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
385 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
386 'convert to error')
41373
ac255a42ec0e Add tests for the filename.
Walter Dörwald <walter@livinglogic.de>
parents: 41372
diff changeset
387
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
388 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
389 # 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
390 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
391 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
392 try:
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
393 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
394 __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
395 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
396 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
397 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
398 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
399 self.module.warn_explicit(message, UserWarning, "file", 42)
49099
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
400 self.failUnlessEqual(w[-1].message, message)
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 49028
diff changeset
401 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
402 self.module.warn_explicit(message, UserWarning, "file", 42)
48948
d563621d76db Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents: 47942
diff changeset
403 self.assertEquals(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
404 # 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
405 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
406 __warningregistry__ = {}
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
407 self.module.warn('onceregistry 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
408 self.failUnlessEqual(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
409 # 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
410 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
411 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
412 __warningregistry__ = {}
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
413 self.module.warn_explicit(message, UserWarning, "file", 42)
48948
d563621d76db Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents: 47942
diff changeset
414 self.assertEquals(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
415 finally:
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
416 self.module.onceregistry = original_registry
41373
ac255a42ec0e Add tests for the filename.
Walter Dörwald <walter@livinglogic.de>
parents: 41372
diff changeset
417
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
418 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
419 # 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
420 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
421 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
422 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
423 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
424 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
425 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
426 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
427 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
428 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
429 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
430 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
431 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
432 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
433 # 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
434 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
435 __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
436 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
437 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
438 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
439 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
440 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
441 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
442 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
443 # 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
444 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
445 __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
446 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
447 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
448 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
449 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
450 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
451 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
452
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
453 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
454 # 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
455 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
456 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
457 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
458 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
459 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
460 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
461 result = stream.getvalue()
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
462 self.failUnless(text in result)
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
463
46696
9130b48a7130 Fix logic error in Python/_warnings.c and add a test to verify
Benjamin Peterson <benjamin@python.org>
parents: 46686
diff changeset
464 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
465 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
466 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
467 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
468 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
469 try:
7e2bbb4dbf87 test_warnings ironically had a single test that was not protecting the warnings
Brett Cannon <bcannon@gmail.com>
parents: 52658
diff changeset
470 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
471 finally:
7e2bbb4dbf87 test_warnings ironically had a single test that was not protecting the warnings
Brett Cannon <bcannon@gmail.com>
parents: 52658
diff changeset
472 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
473
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
474 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
475 # 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
476 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
477 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
478 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
479 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
480 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
481 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
482 result = stream.getvalue()
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
483 self.failUnlessEqual(result.count('\n'), 2,
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
484 "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
485 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
486 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
487 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
488 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
489 line = int(line)
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
490 self.failUnlessEqual(expected_file, path)
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
491 self.failUnlessEqual(warning_class, ' ' + UserWarning.__name__)
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
492 self.failUnlessEqual(message, ' ' + text)
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
493 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
494 assert expected_line
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
495 self.failUnlessEqual(second_line, expected_line)
41373
ac255a42ec0e Add tests for the filename.
Walter Dörwald <walter@livinglogic.de>
parents: 41372
diff changeset
496
38912
58034084ef44 'warning's was improperly requiring that a command-line Warning category be
Brett Cannon <bcannon@gmail.com>
parents: 37767
diff changeset
497
44359
bb716ebcb082 Add tests for the warnings module; specifically formatwarning and showwarning.
Brett Cannon <bcannon@gmail.com>
parents: 42659
diff changeset
498 class WarningsDisplayTests(unittest.TestCase):
bb716ebcb082 Add tests for the warnings module; specifically formatwarning and showwarning.
Brett Cannon <bcannon@gmail.com>
parents: 42659
diff changeset
499
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
500 """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
501 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
502
44359
bb716ebcb082 Add tests for the warnings module; specifically formatwarning and showwarning.
Brett Cannon <bcannon@gmail.com>
parents: 42659
diff changeset
503 def test_formatwarning(self):
bb716ebcb082 Add tests for the warnings module; specifically formatwarning and showwarning.
Brett Cannon <bcannon@gmail.com>
parents: 42659
diff changeset
504 message = "msg"
bb716ebcb082 Add tests for the warnings module; specifically formatwarning and showwarning.
Brett Cannon <bcannon@gmail.com>
parents: 42659
diff changeset
505 category = Warning
bb716ebcb082 Add tests for the warnings module; specifically formatwarning and showwarning.
Brett Cannon <bcannon@gmail.com>
parents: 42659
diff changeset
506 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
507 line_num = 3
bb716ebcb082 Add tests for the warnings module; specifically formatwarning and showwarning.
Brett Cannon <bcannon@gmail.com>
parents: 42659
diff changeset
508 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
509 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
510 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
511 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
512 self.failUnlessEqual(expect, self.module.formatwarning(message,
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
513 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
514 # 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
515 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
516 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
517 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
518 self.failUnlessEqual(expect, self.module.formatwarning(message,
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
519 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
520
bb716ebcb082 Add tests for the warnings module; specifically formatwarning and showwarning.
Brett Cannon <bcannon@gmail.com>
parents: 42659
diff changeset
521 def test_showwarning(self):
bb716ebcb082 Add tests for the warnings module; specifically formatwarning and showwarning.
Brett Cannon <bcannon@gmail.com>
parents: 42659
diff changeset
522 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
523 line_num = 3
bb716ebcb082 Add tests for the warnings module; specifically formatwarning and showwarning.
Brett Cannon <bcannon@gmail.com>
parents: 42659
diff changeset
524 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
525 message = 'msg'
bb716ebcb082 Add tests for the warnings module; specifically formatwarning and showwarning.
Brett Cannon <bcannon@gmail.com>
parents: 42659
diff changeset
526 category = Warning
bb716ebcb082 Add tests for the warnings module; specifically formatwarning and showwarning.
Brett Cannon <bcannon@gmail.com>
parents: 42659
diff changeset
527 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
528 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
529 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
530 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
531 file_object)
bb716ebcb082 Add tests for the warnings module; specifically formatwarning and showwarning.
Brett Cannon <bcannon@gmail.com>
parents: 42659
diff changeset
532 self.failUnlessEqual(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
533 # 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
534 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
535 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
536 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
537 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
538 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
539 file_object, 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
540 self.failUnlessEqual(expect, file_object.getvalue())
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
541
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
542 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
543 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
544
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
545 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
546 module = py_warnings
44359
bb716ebcb082 Add tests for the warnings module; specifically formatwarning and showwarning.
Brett Cannon <bcannon@gmail.com>
parents: 42659
diff changeset
547
bb716ebcb082 Add tests for the warnings module; specifically formatwarning and showwarning.
Brett Cannon <bcannon@gmail.com>
parents: 42659
diff changeset
548
48948
d563621d76db Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents: 47942
diff changeset
549 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
550
48948
d563621d76db Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents: 47942
diff changeset
551 """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
552
d563621d76db Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents: 47942
diff changeset
553 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
554 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
555 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
556 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
557 # 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
558 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
559 wmod.filters = wmod.showwarning = object()
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
560 self.assert_(wmod.filters is orig_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
561 self.assert_(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
562 # 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
563 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
564 wmod.filters = wmod.showwarning = object()
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
565 self.assert_(wmod.filters is orig_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
566 self.assert_(wmod.showwarning is orig_showwarning)
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
567
48948
d563621d76db Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents: 47942
diff changeset
568 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
569 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
570 # 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
571 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
572 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
573 self.assert_(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
574 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
575 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
576 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
577 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
578 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
579 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
580 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
581 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
582 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
583 # 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
584 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
585 with wmod.catch_warnings(module=wmod, record=False) as w:
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
586 self.assert_(w is None)
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
587 self.assert_(wmod.showwarning is orig_showwarning)
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
588
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
589 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
590 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
591 # 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
592 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
593 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
594 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
595 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
596 # 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
597 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
598 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
599 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
600 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
601
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
602 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
603 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
604 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
605 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
606 # 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
607 with wmod.catch_warnings(module=wmod) as w:
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
608 self.assert_(w is None)
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
609 self.assert_(wmod.showwarning is orig_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
610 self.assert_(wmod.filters is not orig_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
611 self.assert_(wmod.filters is orig_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
612 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
613 # 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
614 with wmod.catch_warnings() as w:
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
615 self.assert_(w is None)
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
616 self.assert_(wmod.showwarning is orig_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
617 self.assert_(wmod.filters is not orig_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
618 self.assert_(wmod.filters is orig_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
619
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
620 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
621 # 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
622 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
623 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
624 with test_support.check_warnings() as w:
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
625 self.assertEqual(w.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
626 wmod.simplefilter("always")
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
627 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
628 self.assertEqual(str(w.message), "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
629 wmod.warn("bar")
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
630 self.assertEqual(str(w.message), "bar")
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 self.assertEqual(str(w.warnings[0].message), "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
632 self.assertEqual(str(w.warnings[1].message), "bar")
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
633 w.reset()
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
634 self.assertEqual(w.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
635
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
636
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
48948
d563621d76db Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents: 47942
diff changeset
638 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
639 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
640
48948
d563621d76db Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents: 47942
diff changeset
641 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
642 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
643
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
644
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
645 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
646 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
647 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
648 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
649 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
650 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
651 _WarningsTests,
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
652 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
653 CCatchWarningTests, PyCatchWarningTests,
46414
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
654 )
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
655
840064802952 Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents: 44359
diff changeset
656
29837
64194e775b0b Reworked test_warnings.py:
Raymond Hettinger <python@rcn.com>
parents: 29836
diff changeset
657 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
658 test_main()