blob: 6d8863ad7b50faa15e0c952f5b22fc1d62fe562a [file] [log] [blame]
Steven Bethardae42f332008-03-18 17:26:10 +00001import unittest
Brett Cannon977eb022008-03-19 17:37:43 +00002import sys
Nick Coghland2e09382008-09-11 12:11:06 +00003from test.test_support import (check_warnings, CleanImport,
4 TestSkipped, run_unittest)
Steven Bethardae42f332008-03-18 17:26:10 +00005import warnings
6
Benjamin Petersona6864e02008-07-14 17:42:17 +00007from contextlib import nested
8
Brett Cannon977eb022008-03-19 17:37:43 +00009if not sys.py3kwarning:
10 raise TestSkipped('%s must be run with the -3 flag' % __name__)
11
Nick Coghland2e09382008-09-11 12:11:06 +000012def reset_module_registry(module):
13 try:
14 registry = module.__warningregistry__
15 except AttributeError:
16 pass
17 else:
18 registry.clear()
Steven Bethardae42f332008-03-18 17:26:10 +000019
20class TestPy3KWarnings(unittest.TestCase):
21
Nick Coghlan48361f52008-08-11 15:45:58 +000022 def assertWarning(self, _, warning, expected_message):
Nick Coghland2e09382008-09-11 12:11:06 +000023 self.assertEqual(str(warning.message), expected_message)
Nick Coghlan48361f52008-08-11 15:45:58 +000024
Benjamin Petersonf9214692009-07-02 17:19:22 +000025 def assertNoWarning(self, _, recorder):
26 self.assertEqual(len(recorder.warnings), 0)
27
Benjamin Peterson2fe3ef82008-06-08 02:05:33 +000028 def test_backquote(self):
29 expected = 'backquote not supported in 3.x; use repr()'
Nick Coghland2e09382008-09-11 12:11:06 +000030 with check_warnings() as w:
Benjamin Peterson2fe3ef82008-06-08 02:05:33 +000031 exec "`2`" in {}
32 self.assertWarning(None, w, expected)
33
Benjamin Petersond5efd202008-06-08 22:52:37 +000034 def test_bool_assign(self):
35 # So we don't screw up our globals
36 def safe_exec(expr):
37 def f(**kwargs): pass
38 exec expr in {'f' : f}
39
40 expected = "assignment to True or False is forbidden in 3.x"
Nick Coghland2e09382008-09-11 12:11:06 +000041 with check_warnings() as w:
Benjamin Petersond5efd202008-06-08 22:52:37 +000042 safe_exec("True = False")
43 self.assertWarning(None, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +000044 w.reset()
Benjamin Petersond5efd202008-06-08 22:52:37 +000045 safe_exec("False = True")
46 self.assertWarning(None, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +000047 w.reset()
Benjamin Petersond5efd202008-06-08 22:52:37 +000048 try:
49 safe_exec("obj.False = True")
50 except NameError: pass
51 self.assertWarning(None, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +000052 w.reset()
Benjamin Petersond5efd202008-06-08 22:52:37 +000053 try:
54 safe_exec("obj.True = False")
55 except NameError: pass
56 self.assertWarning(None, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +000057 w.reset()
Benjamin Petersond5efd202008-06-08 22:52:37 +000058 safe_exec("def False(): pass")
59 self.assertWarning(None, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +000060 w.reset()
Benjamin Petersond5efd202008-06-08 22:52:37 +000061 safe_exec("def True(): pass")
62 self.assertWarning(None, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +000063 w.reset()
Benjamin Petersond5efd202008-06-08 22:52:37 +000064 safe_exec("class False: pass")
65 self.assertWarning(None, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +000066 w.reset()
Benjamin Petersond5efd202008-06-08 22:52:37 +000067 safe_exec("class True: pass")
68 self.assertWarning(None, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +000069 w.reset()
Benjamin Petersond5efd202008-06-08 22:52:37 +000070 safe_exec("def f(True=43): pass")
71 self.assertWarning(None, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +000072 w.reset()
Benjamin Petersond5efd202008-06-08 22:52:37 +000073 safe_exec("def f(False=None): pass")
74 self.assertWarning(None, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +000075 w.reset()
Benjamin Petersond5efd202008-06-08 22:52:37 +000076 safe_exec("f(False=True)")
77 self.assertWarning(None, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +000078 w.reset()
Benjamin Petersond5efd202008-06-08 22:52:37 +000079 safe_exec("f(True=1)")
80 self.assertWarning(None, w, expected)
81
82
Steven Bethardae42f332008-03-18 17:26:10 +000083 def test_type_inequality_comparisons(self):
Georg Brandld5b635f2008-03-25 08:29:14 +000084 expected = 'type inequality comparisons not supported in 3.x'
Nick Coghland2e09382008-09-11 12:11:06 +000085 with check_warnings() as w:
Steven Bethardae42f332008-03-18 17:26:10 +000086 self.assertWarning(int < str, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +000087 w.reset()
Steven Bethardae42f332008-03-18 17:26:10 +000088 self.assertWarning(type < object, w, expected)
89
90 def test_object_inequality_comparisons(self):
Georg Brandld5b635f2008-03-25 08:29:14 +000091 expected = 'comparing unequal types not supported in 3.x'
Nick Coghland2e09382008-09-11 12:11:06 +000092 with check_warnings() as w:
Steven Bethardae42f332008-03-18 17:26:10 +000093 self.assertWarning(str < [], w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +000094 w.reset()
Steven Bethardae42f332008-03-18 17:26:10 +000095 self.assertWarning(object() < (1, 2), w, expected)
96
97 def test_dict_inequality_comparisons(self):
Georg Brandld5b635f2008-03-25 08:29:14 +000098 expected = 'dict inequality comparisons not supported in 3.x'
Nick Coghland2e09382008-09-11 12:11:06 +000099 with check_warnings() as w:
Steven Bethardae42f332008-03-18 17:26:10 +0000100 self.assertWarning({} < {2:3}, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000101 w.reset()
Steven Bethardae42f332008-03-18 17:26:10 +0000102 self.assertWarning({} <= {}, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000103 w.reset()
Steven Bethardae42f332008-03-18 17:26:10 +0000104 self.assertWarning({} > {2:3}, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000105 w.reset()
Steven Bethardae42f332008-03-18 17:26:10 +0000106 self.assertWarning({2:3} >= {}, w, expected)
107
108 def test_cell_inequality_comparisons(self):
Georg Brandld5b635f2008-03-25 08:29:14 +0000109 expected = 'cell comparisons not supported in 3.x'
Steven Bethardae42f332008-03-18 17:26:10 +0000110 def f(x):
111 def g():
112 return x
113 return g
114 cell0, = f(0).func_closure
115 cell1, = f(1).func_closure
Nick Coghland2e09382008-09-11 12:11:06 +0000116 with check_warnings() as w:
Steven Bethardae42f332008-03-18 17:26:10 +0000117 self.assertWarning(cell0 == cell1, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000118 w.reset()
Steven Bethardae42f332008-03-18 17:26:10 +0000119 self.assertWarning(cell0 < cell1, w, expected)
120
Steven Bethard6a644f92008-03-18 22:08:20 +0000121 def test_code_inequality_comparisons(self):
Georg Brandld5b635f2008-03-25 08:29:14 +0000122 expected = 'code inequality comparisons not supported in 3.x'
Steven Bethard6a644f92008-03-18 22:08:20 +0000123 def f(x):
124 pass
125 def g(x):
126 pass
Nick Coghland2e09382008-09-11 12:11:06 +0000127 with check_warnings() as w:
Steven Bethard6a644f92008-03-18 22:08:20 +0000128 self.assertWarning(f.func_code < g.func_code, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000129 w.reset()
Steven Bethard6a644f92008-03-18 22:08:20 +0000130 self.assertWarning(f.func_code <= g.func_code, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000131 w.reset()
Steven Bethard6a644f92008-03-18 22:08:20 +0000132 self.assertWarning(f.func_code >= g.func_code, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000133 w.reset()
Steven Bethard6a644f92008-03-18 22:08:20 +0000134 self.assertWarning(f.func_code > g.func_code, w, expected)
135
136 def test_builtin_function_or_method_comparisons(self):
137 expected = ('builtin_function_or_method '
Benjamin Petersonf9214692009-07-02 17:19:22 +0000138 'order comparisons not supported in 3.x')
Steven Bethard6a644f92008-03-18 22:08:20 +0000139 func = eval
140 meth = {}.get
Nick Coghland2e09382008-09-11 12:11:06 +0000141 with check_warnings() as w:
Steven Bethard6a644f92008-03-18 22:08:20 +0000142 self.assertWarning(func < meth, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000143 w.reset()
Steven Bethard6a644f92008-03-18 22:08:20 +0000144 self.assertWarning(func > meth, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000145 w.reset()
Steven Bethard6a644f92008-03-18 22:08:20 +0000146 self.assertWarning(meth <= func, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000147 w.reset()
Steven Bethard6a644f92008-03-18 22:08:20 +0000148 self.assertWarning(meth >= func, w, expected)
Benjamin Petersonf9214692009-07-02 17:19:22 +0000149 w.reset()
150 self.assertNoWarning(meth == func, w)
151 self.assertNoWarning(meth != func, w)
152 lam = lambda x: x
153 self.assertNoWarning(lam == func, w)
154 self.assertNoWarning(lam != func, w)
Steven Bethard6a644f92008-03-18 22:08:20 +0000155
Raymond Hettinger05387862008-03-19 17:45:19 +0000156 def test_sort_cmp_arg(self):
Georg Brandld5b635f2008-03-25 08:29:14 +0000157 expected = "the cmp argument is not supported in 3.x"
Raymond Hettinger05387862008-03-19 17:45:19 +0000158 lst = range(5)
159 cmp = lambda x,y: -1
160
Nick Coghland2e09382008-09-11 12:11:06 +0000161 with check_warnings() as w:
Raymond Hettinger05387862008-03-19 17:45:19 +0000162 self.assertWarning(lst.sort(cmp=cmp), w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000163 w.reset()
Raymond Hettinger05387862008-03-19 17:45:19 +0000164 self.assertWarning(sorted(lst, cmp=cmp), w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000165 w.reset()
Raymond Hettinger05387862008-03-19 17:45:19 +0000166 self.assertWarning(lst.sort(cmp), w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000167 w.reset()
Raymond Hettinger05387862008-03-19 17:45:19 +0000168 self.assertWarning(sorted(lst, cmp), w, expected)
169
Georg Brandl5a444242008-03-21 20:11:46 +0000170 def test_sys_exc_clear(self):
Georg Brandld5b635f2008-03-25 08:29:14 +0000171 expected = 'sys.exc_clear() not supported in 3.x; use except clauses'
Nick Coghland2e09382008-09-11 12:11:06 +0000172 with check_warnings() as w:
Georg Brandl5a444242008-03-21 20:11:46 +0000173 self.assertWarning(sys.exc_clear(), w, expected)
174
Georg Brandl07e56812008-03-21 20:21:46 +0000175 def test_methods_members(self):
176 expected = '__members__ and __methods__ not supported in 3.x'
177 class C:
178 __methods__ = ['a']
179 __members__ = ['b']
180 c = C()
Nick Coghland2e09382008-09-11 12:11:06 +0000181 with check_warnings() as w:
Georg Brandl07e56812008-03-21 20:21:46 +0000182 self.assertWarning(dir(c), w, expected)
183
Georg Brandl65bb42d2008-03-21 20:38:24 +0000184 def test_softspace(self):
185 expected = 'file.softspace not supported in 3.x'
186 with file(__file__) as f:
Nick Coghland2e09382008-09-11 12:11:06 +0000187 with check_warnings() as w:
Georg Brandl65bb42d2008-03-21 20:38:24 +0000188 self.assertWarning(f.softspace, w, expected)
189 def set():
190 f.softspace = 0
Nick Coghland2e09382008-09-11 12:11:06 +0000191 with check_warnings() as w:
Georg Brandl65bb42d2008-03-21 20:38:24 +0000192 self.assertWarning(set(), w, expected)
193
Benjamin Peterson712ee922008-08-24 18:10:20 +0000194 def test_slice_methods(self):
195 class Spam(object):
196 def __getslice__(self, i, j): pass
197 def __setslice__(self, i, j, what): pass
198 def __delslice__(self, i, j): pass
199 class Egg:
200 def __getslice__(self, i, h): pass
201 def __setslice__(self, i, j, what): pass
202 def __delslice__(self, i, j): pass
203
204 expected = "in 3.x, __{0}slice__ has been removed; use __{0}item__"
205
206 for obj in (Spam(), Egg()):
Nick Coghland2e09382008-09-11 12:11:06 +0000207 with check_warnings() as w:
Benjamin Peterson712ee922008-08-24 18:10:20 +0000208 self.assertWarning(obj[1:2], w, expected.format('get'))
Nick Coghland2e09382008-09-11 12:11:06 +0000209 w.reset()
Benjamin Peterson712ee922008-08-24 18:10:20 +0000210 del obj[3:4]
211 self.assertWarning(None, w, expected.format('del'))
Nick Coghland2e09382008-09-11 12:11:06 +0000212 w.reset()
Benjamin Peterson712ee922008-08-24 18:10:20 +0000213 obj[4:5] = "eggs"
214 self.assertWarning(None, w, expected.format('set'))
215
Benjamin Petersonf4fcdb62008-06-08 23:00:00 +0000216 def test_tuple_parameter_unpacking(self):
217 expected = "tuple parameter unpacking has been removed in 3.x"
Nick Coghland2e09382008-09-11 12:11:06 +0000218 with check_warnings() as w:
Benjamin Petersonf4fcdb62008-06-08 23:00:00 +0000219 exec "def f((a, b)): pass"
220 self.assertWarning(None, w, expected)
221
Georg Brandl80055f62008-03-25 07:56:27 +0000222 def test_buffer(self):
Nick Coghlan48361f52008-08-11 15:45:58 +0000223 expected = 'buffer() not supported in 3.x'
Nick Coghland2e09382008-09-11 12:11:06 +0000224 with check_warnings() as w:
Georg Brandl80055f62008-03-25 07:56:27 +0000225 self.assertWarning(buffer('a'), w, expected)
226
Georg Brandla9916b52008-05-17 22:11:54 +0000227 def test_file_xreadlines(self):
228 expected = ("f.xreadlines() not supported in 3.x, "
229 "try 'for line in f' instead")
230 with file(__file__) as f:
Nick Coghland2e09382008-09-11 12:11:06 +0000231 with check_warnings() as w:
Georg Brandla9916b52008-05-17 22:11:54 +0000232 self.assertWarning(f.xreadlines(), w, expected)
233
Nick Coghlan48361f52008-08-11 15:45:58 +0000234 def test_hash_inheritance(self):
Nick Coghland2e09382008-09-11 12:11:06 +0000235 with check_warnings() as w:
Nick Coghlan48361f52008-08-11 15:45:58 +0000236 # With object as the base class
237 class WarnOnlyCmp(object):
238 def __cmp__(self, other): pass
Nick Coghland2e09382008-09-11 12:11:06 +0000239 self.assertEqual(len(w.warnings), 1)
Nick Coghlan48361f52008-08-11 15:45:58 +0000240 self.assertWarning(None, w,
241 "Overriding __cmp__ blocks inheritance of __hash__ in 3.x")
Nick Coghland2e09382008-09-11 12:11:06 +0000242 w.reset()
Nick Coghlan48361f52008-08-11 15:45:58 +0000243 class WarnOnlyEq(object):
244 def __eq__(self, other): pass
Nick Coghland2e09382008-09-11 12:11:06 +0000245 self.assertEqual(len(w.warnings), 1)
Nick Coghlan48361f52008-08-11 15:45:58 +0000246 self.assertWarning(None, w,
247 "Overriding __eq__ blocks inheritance of __hash__ in 3.x")
Nick Coghland2e09382008-09-11 12:11:06 +0000248 w.reset()
Nick Coghlan48361f52008-08-11 15:45:58 +0000249 class WarnCmpAndEq(object):
250 def __cmp__(self, other): pass
251 def __eq__(self, other): pass
Nick Coghland2e09382008-09-11 12:11:06 +0000252 self.assertEqual(len(w.warnings), 2)
253 self.assertWarning(None, w.warnings[0],
Nick Coghlan48361f52008-08-11 15:45:58 +0000254 "Overriding __cmp__ blocks inheritance of __hash__ in 3.x")
255 self.assertWarning(None, w,
256 "Overriding __eq__ blocks inheritance of __hash__ in 3.x")
Nick Coghland2e09382008-09-11 12:11:06 +0000257 w.reset()
Nick Coghlan48361f52008-08-11 15:45:58 +0000258 class NoWarningOnlyHash(object):
259 def __hash__(self): pass
Nick Coghland2e09382008-09-11 12:11:06 +0000260 self.assertEqual(len(w.warnings), 0)
Nick Coghlan48361f52008-08-11 15:45:58 +0000261 # With an intermediate class in the heirarchy
262 class DefinesAllThree(object):
263 def __cmp__(self, other): pass
264 def __eq__(self, other): pass
265 def __hash__(self): pass
266 class WarnOnlyCmp(DefinesAllThree):
267 def __cmp__(self, other): pass
Nick Coghland2e09382008-09-11 12:11:06 +0000268 self.assertEqual(len(w.warnings), 1)
Nick Coghlan48361f52008-08-11 15:45:58 +0000269 self.assertWarning(None, w,
270 "Overriding __cmp__ blocks inheritance of __hash__ in 3.x")
Nick Coghland2e09382008-09-11 12:11:06 +0000271 w.reset()
Nick Coghlan48361f52008-08-11 15:45:58 +0000272 class WarnOnlyEq(DefinesAllThree):
273 def __eq__(self, other): pass
Nick Coghland2e09382008-09-11 12:11:06 +0000274 self.assertEqual(len(w.warnings), 1)
Nick Coghlan48361f52008-08-11 15:45:58 +0000275 self.assertWarning(None, w,
276 "Overriding __eq__ blocks inheritance of __hash__ in 3.x")
Nick Coghland2e09382008-09-11 12:11:06 +0000277 w.reset()
Nick Coghlan48361f52008-08-11 15:45:58 +0000278 class WarnCmpAndEq(DefinesAllThree):
279 def __cmp__(self, other): pass
280 def __eq__(self, other): pass
Nick Coghland2e09382008-09-11 12:11:06 +0000281 self.assertEqual(len(w.warnings), 2)
282 self.assertWarning(None, w.warnings[0],
Nick Coghlan48361f52008-08-11 15:45:58 +0000283 "Overriding __cmp__ blocks inheritance of __hash__ in 3.x")
284 self.assertWarning(None, w,
285 "Overriding __eq__ blocks inheritance of __hash__ in 3.x")
Nick Coghland2e09382008-09-11 12:11:06 +0000286 w.reset()
Nick Coghlan48361f52008-08-11 15:45:58 +0000287 class NoWarningOnlyHash(DefinesAllThree):
288 def __hash__(self): pass
Nick Coghland2e09382008-09-11 12:11:06 +0000289 self.assertEqual(len(w.warnings), 0)
Nick Coghlan48361f52008-08-11 15:45:58 +0000290
Georg Brandl07e56812008-03-21 20:21:46 +0000291
Brett Cannone5d2cba2008-05-06 23:23:34 +0000292class TestStdlibRemovals(unittest.TestCase):
293
Brett Cannon3c759142008-05-09 05:25:37 +0000294 # test.testall not tested as it executes all unit tests as an
295 # import side-effect.
Brett Cannon4c1f8812008-05-10 02:27:04 +0000296 all_platforms = ('audiodev', 'imputil', 'mutex', 'user', 'new', 'rexec',
Brett Cannon1e8fba72008-07-18 19:30:22 +0000297 'Bastion', 'compiler', 'dircache', 'mimetools',
298 'fpformat', 'ihooks', 'mhlib', 'statvfs', 'htmllib',
299 'sgmllib', 'rfc822', 'sunaudio')
Brett Cannon54c77aa2008-05-14 21:08:41 +0000300 inclusive_platforms = {'irix' : ('pure', 'AL', 'al', 'CD', 'cd', 'cddb',
Brett Cannon044616a2008-05-15 02:33:55 +0000301 'cdplayer', 'CL', 'cl', 'DEVICE', 'GL',
Brett Cannon75ba4652008-05-15 03:23:17 +0000302 'gl', 'ERRNO', 'FILE', 'FL', 'flp', 'fl',
Brett Cannond8c41ec2008-05-15 03:41:55 +0000303 'fm', 'GET', 'GLWS', 'imgfile', 'IN',
Brett Cannoncd2de082008-05-15 03:51:21 +0000304 'IOCTL', 'jpeg', 'panel', 'panelparser',
Brett Cannon74a596c2008-05-15 04:17:35 +0000305 'readcd', 'SV', 'torgb', 'WAIT'),
Benjamin Peterson23681932008-05-12 21:42:13 +0000306 'darwin' : ('autoGIL', 'Carbon', 'OSATerminology',
Brett Cannonea785fb2008-05-14 01:09:40 +0000307 'icglue', 'Nav', 'MacOS', 'aepack',
308 'aetools', 'aetypes', 'applesingle',
309 'appletrawmain', 'appletrunner',
310 'argvemulator', 'bgenlocations',
Benjamin Peterson23681932008-05-12 21:42:13 +0000311 'EasyDialogs', 'macerrors', 'macostools',
312 'findertools', 'FrameWork', 'ic',
313 'gensuitemodule', 'icopen', 'macresource',
314 'MiniAEFrame', 'pimp', 'PixMapWrapper',
Brett Cannonea785fb2008-05-14 01:09:40 +0000315 'terminalcommand', 'videoreader',
316 '_builtinSuites', 'CodeWarrior',
317 'Explorer', 'Finder', 'Netscape',
318 'StdSuites', 'SystemEvents', 'Terminal',
319 'cfmfile', 'bundlebuilder', 'buildtools',
Benjamin Petersona6864e02008-07-14 17:42:17 +0000320 'ColorPicker', 'Audio_mac'),
Brett Cannon22248172008-05-16 00:10:24 +0000321 'sunos5' : ('sunaudiodev', 'SUNAUDIODEV'),
322 }
Brett Cannonac861b52008-05-12 03:45:59 +0000323 optional_modules = ('bsddb185', 'Canvas', 'dl', 'linuxaudiodev', 'imageop',
Brett Cannon32476fc2008-09-05 18:33:51 +0000324 'sv', 'cPickle', 'bsddb', 'dbhash')
Brett Cannone5d2cba2008-05-06 23:23:34 +0000325
Brett Cannon9ac39742008-05-09 22:51:58 +0000326 def check_removal(self, module_name, optional=False):
Brett Cannone5d2cba2008-05-06 23:23:34 +0000327 """Make sure the specified module, when imported, raises a
328 DeprecationWarning and specifies itself in the message."""
Brett Cannon672237d2008-09-09 00:49:16 +0000329 with nested(CleanImport(module_name), warnings.catch_warnings()):
Nick Coghland2e09382008-09-11 12:11:06 +0000330 # XXX: This is not quite enough for extension modules - those
331 # won't rerun their init code even with CleanImport.
332 # You can see this easily by running the whole test suite with -3
Benjamin Petersona6864e02008-07-14 17:42:17 +0000333 warnings.filterwarnings("error", ".+ removed",
334 DeprecationWarning, __name__)
335 try:
336 __import__(module_name, level=0)
337 except DeprecationWarning as exc:
338 self.assert_(module_name in exc.args[0],
339 "%s warning didn't contain module name"
340 % module_name)
341 except ImportError:
342 if not optional:
343 self.fail("Non-optional module {0} raised an "
344 "ImportError.".format(module_name))
345 else:
346 self.fail("DeprecationWarning not raised for {0}"
347 .format(module_name))
Brett Cannone5d2cba2008-05-06 23:23:34 +0000348
349 def test_platform_independent_removals(self):
350 # Make sure that the modules that are available on all platforms raise
351 # the proper DeprecationWarning.
352 for module_name in self.all_platforms:
353 self.check_removal(module_name)
354
Brett Cannon9ac39742008-05-09 22:51:58 +0000355 def test_platform_specific_removals(self):
356 # Test the removal of platform-specific modules.
357 for module_name in self.inclusive_platforms.get(sys.platform, []):
358 self.check_removal(module_name, optional=True)
359
Brett Cannon768d44f2008-05-10 02:47:54 +0000360 def test_optional_module_removals(self):
361 # Test the removal of modules that may or may not be built.
362 for module_name in self.optional_modules:
363 self.check_removal(module_name, optional=True)
364
Benjamin Peterson0893a0a2008-05-09 00:27:01 +0000365 def test_os_path_walk(self):
366 msg = "In 3.x, os.path.walk is removed in favor of os.walk."
367 def dumbo(where, names, args): pass
368 for path_mod in ("ntpath", "macpath", "os2emxpath", "posixpath"):
369 mod = __import__(path_mod)
Nick Coghland2e09382008-09-11 12:11:06 +0000370 reset_module_registry(mod)
371 with check_warnings() as w:
Benjamin Peterson1d310232008-05-27 01:42:29 +0000372 mod.walk("crashers", dumbo, None)
Nick Coghland2e09382008-09-11 12:11:06 +0000373 self.assertEquals(str(w.message), msg)
Benjamin Peterson0893a0a2008-05-09 00:27:01 +0000374
Benjamin Peterson3aa84a72008-05-26 19:41:53 +0000375 def test_commands_members(self):
376 import commands
Nick Coghland2e09382008-09-11 12:11:06 +0000377 # commands module tests may have already triggered this warning
378 reset_module_registry(commands)
Benjamin Peterson3aa84a72008-05-26 19:41:53 +0000379 members = {"mk2arg" : 2, "mkarg" : 1, "getstatus" : 1}
380 for name, arg_count in members.items():
Brett Cannon672237d2008-09-09 00:49:16 +0000381 with warnings.catch_warnings():
Benjamin Peterson3aa84a72008-05-26 19:41:53 +0000382 warnings.filterwarnings("error")
383 func = getattr(commands, name)
384 self.assertRaises(DeprecationWarning, func, *([None]*arg_count))
385
Benjamin Peterson541f7da2008-08-18 02:12:23 +0000386 def test_reduce_move(self):
387 from operator import add
Nick Coghland2e09382008-09-11 12:11:06 +0000388 # reduce tests may have already triggered this warning
389 reset_module_registry(unittest)
Brett Cannon672237d2008-09-09 00:49:16 +0000390 with warnings.catch_warnings():
Benjamin Peterson541f7da2008-08-18 02:12:23 +0000391 warnings.filterwarnings("error", "reduce")
392 self.assertRaises(DeprecationWarning, reduce, add, range(10))
393
Brett Cannonabb34fe2008-05-29 05:08:50 +0000394 def test_mutablestring_removal(self):
395 # UserString.MutableString has been removed in 3.0.
396 import UserString
Nick Coghland2e09382008-09-11 12:11:06 +0000397 # UserString tests may have already triggered this warning
398 reset_module_registry(UserString)
Brett Cannon672237d2008-09-09 00:49:16 +0000399 with warnings.catch_warnings():
Brett Cannonabb34fe2008-05-29 05:08:50 +0000400 warnings.filterwarnings("error", ".*MutableString",
401 DeprecationWarning)
402 self.assertRaises(DeprecationWarning, UserString.MutableString)
403
Brett Cannone5d2cba2008-05-06 23:23:34 +0000404
Steven Bethardae42f332008-03-18 17:26:10 +0000405def test_main():
Nick Coghland2e09382008-09-11 12:11:06 +0000406 with check_warnings():
Benjamin Peterson1d310232008-05-27 01:42:29 +0000407 warnings.simplefilter("always")
408 run_unittest(TestPy3KWarnings,
409 TestStdlibRemovals)
Steven Bethardae42f332008-03-18 17:26:10 +0000410
411if __name__ == '__main__':
412 test_main()