blob: da222566a47963f883b6c44360d544cba0588877 [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 Peterson440847c2009-11-19 23:01:36 +000034 def test_paren_arg_names(self):
35 expected = 'parenthesized argument names are invalid in 3.x'
36 def check(s):
37 exec s in {}
38 self.assertWarning(None, w, expected)
39 with check_warnings() as w:
40 check("def f((x)): pass")
41 check("def f((((x))), (y)): pass")
42 check("def f((x), (((y))), m=32): pass")
43 # Something like def f((a, (b))): pass will raise the tuple
44 # unpacking warning.
45
Benjamin Petersond5efd202008-06-08 22:52:37 +000046 def test_bool_assign(self):
47 # So we don't screw up our globals
48 def safe_exec(expr):
49 def f(**kwargs): pass
50 exec expr in {'f' : f}
51
52 expected = "assignment to True or False is forbidden in 3.x"
Nick Coghland2e09382008-09-11 12:11:06 +000053 with check_warnings() as w:
Benjamin Petersond5efd202008-06-08 22:52:37 +000054 safe_exec("True = False")
55 self.assertWarning(None, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +000056 w.reset()
Benjamin Petersond5efd202008-06-08 22:52:37 +000057 safe_exec("False = True")
58 self.assertWarning(None, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +000059 w.reset()
Benjamin Petersond5efd202008-06-08 22:52:37 +000060 try:
61 safe_exec("obj.False = True")
62 except NameError: pass
63 self.assertWarning(None, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +000064 w.reset()
Benjamin Petersond5efd202008-06-08 22:52:37 +000065 try:
66 safe_exec("obj.True = False")
67 except NameError: 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 False(): 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 True(): 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("class False: pass")
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("class True: pass")
80 self.assertWarning(None, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +000081 w.reset()
Benjamin Petersond5efd202008-06-08 22:52:37 +000082 safe_exec("def f(True=43): pass")
83 self.assertWarning(None, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +000084 w.reset()
Benjamin Petersond5efd202008-06-08 22:52:37 +000085 safe_exec("def f(False=None): pass")
86 self.assertWarning(None, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +000087 w.reset()
Benjamin Petersond5efd202008-06-08 22:52:37 +000088 safe_exec("f(False=True)")
89 self.assertWarning(None, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +000090 w.reset()
Benjamin Petersond5efd202008-06-08 22:52:37 +000091 safe_exec("f(True=1)")
92 self.assertWarning(None, w, expected)
93
94
Steven Bethardae42f332008-03-18 17:26:10 +000095 def test_type_inequality_comparisons(self):
Georg Brandld5b635f2008-03-25 08:29:14 +000096 expected = 'type inequality comparisons not supported in 3.x'
Nick Coghland2e09382008-09-11 12:11:06 +000097 with check_warnings() as w:
Steven Bethardae42f332008-03-18 17:26:10 +000098 self.assertWarning(int < str, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +000099 w.reset()
Steven Bethardae42f332008-03-18 17:26:10 +0000100 self.assertWarning(type < object, w, expected)
101
102 def test_object_inequality_comparisons(self):
Georg Brandld5b635f2008-03-25 08:29:14 +0000103 expected = 'comparing unequal types not supported in 3.x'
Nick Coghland2e09382008-09-11 12:11:06 +0000104 with check_warnings() as w:
Steven Bethardae42f332008-03-18 17:26:10 +0000105 self.assertWarning(str < [], w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000106 w.reset()
Steven Bethardae42f332008-03-18 17:26:10 +0000107 self.assertWarning(object() < (1, 2), w, expected)
108
109 def test_dict_inequality_comparisons(self):
Georg Brandld5b635f2008-03-25 08:29:14 +0000110 expected = 'dict inequality comparisons not supported in 3.x'
Nick Coghland2e09382008-09-11 12:11:06 +0000111 with check_warnings() as w:
Steven Bethardae42f332008-03-18 17:26:10 +0000112 self.assertWarning({} < {2:3}, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000113 w.reset()
Steven Bethardae42f332008-03-18 17:26:10 +0000114 self.assertWarning({} <= {}, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000115 w.reset()
Steven Bethardae42f332008-03-18 17:26:10 +0000116 self.assertWarning({} > {2:3}, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000117 w.reset()
Steven Bethardae42f332008-03-18 17:26:10 +0000118 self.assertWarning({2:3} >= {}, w, expected)
119
120 def test_cell_inequality_comparisons(self):
Georg Brandld5b635f2008-03-25 08:29:14 +0000121 expected = 'cell comparisons not supported in 3.x'
Steven Bethardae42f332008-03-18 17:26:10 +0000122 def f(x):
123 def g():
124 return x
125 return g
126 cell0, = f(0).func_closure
127 cell1, = f(1).func_closure
Nick Coghland2e09382008-09-11 12:11:06 +0000128 with check_warnings() as w:
Steven Bethardae42f332008-03-18 17:26:10 +0000129 self.assertWarning(cell0 == cell1, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000130 w.reset()
Steven Bethardae42f332008-03-18 17:26:10 +0000131 self.assertWarning(cell0 < cell1, w, expected)
132
Steven Bethard6a644f92008-03-18 22:08:20 +0000133 def test_code_inequality_comparisons(self):
Georg Brandld5b635f2008-03-25 08:29:14 +0000134 expected = 'code inequality comparisons not supported in 3.x'
Steven Bethard6a644f92008-03-18 22:08:20 +0000135 def f(x):
136 pass
137 def g(x):
138 pass
Nick Coghland2e09382008-09-11 12:11:06 +0000139 with check_warnings() as w:
Steven Bethard6a644f92008-03-18 22:08:20 +0000140 self.assertWarning(f.func_code < g.func_code, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000141 w.reset()
Steven Bethard6a644f92008-03-18 22:08:20 +0000142 self.assertWarning(f.func_code <= g.func_code, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000143 w.reset()
Steven Bethard6a644f92008-03-18 22:08:20 +0000144 self.assertWarning(f.func_code >= g.func_code, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000145 w.reset()
Steven Bethard6a644f92008-03-18 22:08:20 +0000146 self.assertWarning(f.func_code > g.func_code, w, expected)
147
148 def test_builtin_function_or_method_comparisons(self):
149 expected = ('builtin_function_or_method '
Benjamin Petersonf9214692009-07-02 17:19:22 +0000150 'order comparisons not supported in 3.x')
Steven Bethard6a644f92008-03-18 22:08:20 +0000151 func = eval
152 meth = {}.get
Nick Coghland2e09382008-09-11 12:11:06 +0000153 with check_warnings() as w:
Steven Bethard6a644f92008-03-18 22:08:20 +0000154 self.assertWarning(func < meth, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000155 w.reset()
Steven Bethard6a644f92008-03-18 22:08:20 +0000156 self.assertWarning(func > meth, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000157 w.reset()
Steven Bethard6a644f92008-03-18 22:08:20 +0000158 self.assertWarning(meth <= func, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000159 w.reset()
Steven Bethard6a644f92008-03-18 22:08:20 +0000160 self.assertWarning(meth >= func, w, expected)
Benjamin Petersonf9214692009-07-02 17:19:22 +0000161 w.reset()
162 self.assertNoWarning(meth == func, w)
163 self.assertNoWarning(meth != func, w)
164 lam = lambda x: x
165 self.assertNoWarning(lam == func, w)
166 self.assertNoWarning(lam != func, w)
Steven Bethard6a644f92008-03-18 22:08:20 +0000167
Raymond Hettinger05387862008-03-19 17:45:19 +0000168 def test_sort_cmp_arg(self):
Georg Brandld5b635f2008-03-25 08:29:14 +0000169 expected = "the cmp argument is not supported in 3.x"
Raymond Hettinger05387862008-03-19 17:45:19 +0000170 lst = range(5)
171 cmp = lambda x,y: -1
172
Nick Coghland2e09382008-09-11 12:11:06 +0000173 with check_warnings() as w:
Raymond Hettinger05387862008-03-19 17:45:19 +0000174 self.assertWarning(lst.sort(cmp=cmp), w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000175 w.reset()
Raymond Hettinger05387862008-03-19 17:45:19 +0000176 self.assertWarning(sorted(lst, cmp=cmp), w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000177 w.reset()
Raymond Hettinger05387862008-03-19 17:45:19 +0000178 self.assertWarning(lst.sort(cmp), w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000179 w.reset()
Raymond Hettinger05387862008-03-19 17:45:19 +0000180 self.assertWarning(sorted(lst, cmp), w, expected)
181
Georg Brandl5a444242008-03-21 20:11:46 +0000182 def test_sys_exc_clear(self):
Georg Brandld5b635f2008-03-25 08:29:14 +0000183 expected = 'sys.exc_clear() not supported in 3.x; use except clauses'
Nick Coghland2e09382008-09-11 12:11:06 +0000184 with check_warnings() as w:
Georg Brandl5a444242008-03-21 20:11:46 +0000185 self.assertWarning(sys.exc_clear(), w, expected)
186
Georg Brandl07e56812008-03-21 20:21:46 +0000187 def test_methods_members(self):
188 expected = '__members__ and __methods__ not supported in 3.x'
189 class C:
190 __methods__ = ['a']
191 __members__ = ['b']
192 c = C()
Nick Coghland2e09382008-09-11 12:11:06 +0000193 with check_warnings() as w:
Georg Brandl07e56812008-03-21 20:21:46 +0000194 self.assertWarning(dir(c), w, expected)
195
Georg Brandl65bb42d2008-03-21 20:38:24 +0000196 def test_softspace(self):
197 expected = 'file.softspace not supported in 3.x'
198 with file(__file__) as f:
Nick Coghland2e09382008-09-11 12:11:06 +0000199 with check_warnings() as w:
Georg Brandl65bb42d2008-03-21 20:38:24 +0000200 self.assertWarning(f.softspace, w, expected)
201 def set():
202 f.softspace = 0
Nick Coghland2e09382008-09-11 12:11:06 +0000203 with check_warnings() as w:
Georg Brandl65bb42d2008-03-21 20:38:24 +0000204 self.assertWarning(set(), w, expected)
205
Benjamin Peterson712ee922008-08-24 18:10:20 +0000206 def test_slice_methods(self):
207 class Spam(object):
208 def __getslice__(self, i, j): pass
209 def __setslice__(self, i, j, what): pass
210 def __delslice__(self, i, j): pass
211 class Egg:
212 def __getslice__(self, i, h): pass
213 def __setslice__(self, i, j, what): pass
214 def __delslice__(self, i, j): pass
215
216 expected = "in 3.x, __{0}slice__ has been removed; use __{0}item__"
217
218 for obj in (Spam(), Egg()):
Nick Coghland2e09382008-09-11 12:11:06 +0000219 with check_warnings() as w:
Benjamin Peterson712ee922008-08-24 18:10:20 +0000220 self.assertWarning(obj[1:2], w, expected.format('get'))
Nick Coghland2e09382008-09-11 12:11:06 +0000221 w.reset()
Benjamin Peterson712ee922008-08-24 18:10:20 +0000222 del obj[3:4]
223 self.assertWarning(None, w, expected.format('del'))
Nick Coghland2e09382008-09-11 12:11:06 +0000224 w.reset()
Benjamin Peterson712ee922008-08-24 18:10:20 +0000225 obj[4:5] = "eggs"
226 self.assertWarning(None, w, expected.format('set'))
227
Benjamin Petersonf4fcdb62008-06-08 23:00:00 +0000228 def test_tuple_parameter_unpacking(self):
229 expected = "tuple parameter unpacking has been removed in 3.x"
Nick Coghland2e09382008-09-11 12:11:06 +0000230 with check_warnings() as w:
Benjamin Petersonf4fcdb62008-06-08 23:00:00 +0000231 exec "def f((a, b)): pass"
232 self.assertWarning(None, w, expected)
233
Georg Brandl80055f62008-03-25 07:56:27 +0000234 def test_buffer(self):
Nick Coghlan48361f52008-08-11 15:45:58 +0000235 expected = 'buffer() not supported in 3.x'
Nick Coghland2e09382008-09-11 12:11:06 +0000236 with check_warnings() as w:
Georg Brandl80055f62008-03-25 07:56:27 +0000237 self.assertWarning(buffer('a'), w, expected)
238
Georg Brandla9916b52008-05-17 22:11:54 +0000239 def test_file_xreadlines(self):
240 expected = ("f.xreadlines() not supported in 3.x, "
241 "try 'for line in f' instead")
242 with file(__file__) as f:
Nick Coghland2e09382008-09-11 12:11:06 +0000243 with check_warnings() as w:
Georg Brandla9916b52008-05-17 22:11:54 +0000244 self.assertWarning(f.xreadlines(), w, expected)
245
Nick Coghlan48361f52008-08-11 15:45:58 +0000246 def test_hash_inheritance(self):
Nick Coghland2e09382008-09-11 12:11:06 +0000247 with check_warnings() as w:
Nick Coghlan48361f52008-08-11 15:45:58 +0000248 # With object as the base class
249 class WarnOnlyCmp(object):
250 def __cmp__(self, other): pass
Mark Dickinson3f918112010-06-05 13:24:04 +0000251 self.assertEqual(len(w.warnings), 0)
Nick Coghland2e09382008-09-11 12:11:06 +0000252 w.reset()
Nick Coghlan48361f52008-08-11 15:45:58 +0000253 class WarnOnlyEq(object):
254 def __eq__(self, other): pass
Nick Coghland2e09382008-09-11 12:11:06 +0000255 self.assertEqual(len(w.warnings), 1)
Nick Coghlan48361f52008-08-11 15:45:58 +0000256 self.assertWarning(None, w,
257 "Overriding __eq__ blocks inheritance of __hash__ in 3.x")
Nick Coghland2e09382008-09-11 12:11:06 +0000258 w.reset()
Nick Coghlan48361f52008-08-11 15:45:58 +0000259 class WarnCmpAndEq(object):
260 def __cmp__(self, other): pass
261 def __eq__(self, other): pass
Mark Dickinson3f918112010-06-05 13:24:04 +0000262 self.assertEqual(len(w.warnings), 1)
Nick Coghlan48361f52008-08-11 15:45:58 +0000263 self.assertWarning(None, w,
264 "Overriding __eq__ blocks inheritance of __hash__ in 3.x")
Nick Coghland2e09382008-09-11 12:11:06 +0000265 w.reset()
Nick Coghlan48361f52008-08-11 15:45:58 +0000266 class NoWarningOnlyHash(object):
267 def __hash__(self): pass
Nick Coghland2e09382008-09-11 12:11:06 +0000268 self.assertEqual(len(w.warnings), 0)
Nick Coghlan48361f52008-08-11 15:45:58 +0000269 # With an intermediate class in the heirarchy
270 class DefinesAllThree(object):
271 def __cmp__(self, other): pass
272 def __eq__(self, other): pass
273 def __hash__(self): pass
274 class WarnOnlyCmp(DefinesAllThree):
275 def __cmp__(self, other): pass
Mark Dickinson3f918112010-06-05 13:24:04 +0000276 self.assertEqual(len(w.warnings), 0)
Nick Coghland2e09382008-09-11 12:11:06 +0000277 w.reset()
Nick Coghlan48361f52008-08-11 15:45:58 +0000278 class WarnOnlyEq(DefinesAllThree):
279 def __eq__(self, other): pass
Nick Coghland2e09382008-09-11 12:11:06 +0000280 self.assertEqual(len(w.warnings), 1)
Nick Coghlan48361f52008-08-11 15:45:58 +0000281 self.assertWarning(None, w,
282 "Overriding __eq__ blocks inheritance of __hash__ in 3.x")
Nick Coghland2e09382008-09-11 12:11:06 +0000283 w.reset()
Nick Coghlan48361f52008-08-11 15:45:58 +0000284 class WarnCmpAndEq(DefinesAllThree):
285 def __cmp__(self, other): pass
286 def __eq__(self, other): pass
Mark Dickinson3f918112010-06-05 13:24:04 +0000287 self.assertEqual(len(w.warnings), 1)
Nick Coghlan48361f52008-08-11 15:45:58 +0000288 self.assertWarning(None, w,
289 "Overriding __eq__ blocks inheritance of __hash__ in 3.x")
Nick Coghland2e09382008-09-11 12:11:06 +0000290 w.reset()
Nick Coghlan48361f52008-08-11 15:45:58 +0000291 class NoWarningOnlyHash(DefinesAllThree):
292 def __hash__(self): pass
Nick Coghland2e09382008-09-11 12:11:06 +0000293 self.assertEqual(len(w.warnings), 0)
Nick Coghlan48361f52008-08-11 15:45:58 +0000294
Georg Brandl07e56812008-03-21 20:21:46 +0000295
Brett Cannone5d2cba2008-05-06 23:23:34 +0000296class TestStdlibRemovals(unittest.TestCase):
297
Brett Cannon3c759142008-05-09 05:25:37 +0000298 # test.testall not tested as it executes all unit tests as an
299 # import side-effect.
Brett Cannon4c1f8812008-05-10 02:27:04 +0000300 all_platforms = ('audiodev', 'imputil', 'mutex', 'user', 'new', 'rexec',
Brett Cannon1e8fba72008-07-18 19:30:22 +0000301 'Bastion', 'compiler', 'dircache', 'mimetools',
302 'fpformat', 'ihooks', 'mhlib', 'statvfs', 'htmllib',
303 'sgmllib', 'rfc822', 'sunaudio')
Brett Cannon54c77aa2008-05-14 21:08:41 +0000304 inclusive_platforms = {'irix' : ('pure', 'AL', 'al', 'CD', 'cd', 'cddb',
Brett Cannon044616a2008-05-15 02:33:55 +0000305 'cdplayer', 'CL', 'cl', 'DEVICE', 'GL',
Brett Cannon75ba4652008-05-15 03:23:17 +0000306 'gl', 'ERRNO', 'FILE', 'FL', 'flp', 'fl',
Brett Cannond8c41ec2008-05-15 03:41:55 +0000307 'fm', 'GET', 'GLWS', 'imgfile', 'IN',
Brett Cannoncd2de082008-05-15 03:51:21 +0000308 'IOCTL', 'jpeg', 'panel', 'panelparser',
Brett Cannon74a596c2008-05-15 04:17:35 +0000309 'readcd', 'SV', 'torgb', 'WAIT'),
Benjamin Peterson23681932008-05-12 21:42:13 +0000310 'darwin' : ('autoGIL', 'Carbon', 'OSATerminology',
Brett Cannonea785fb2008-05-14 01:09:40 +0000311 'icglue', 'Nav', 'MacOS', 'aepack',
312 'aetools', 'aetypes', 'applesingle',
313 'appletrawmain', 'appletrunner',
314 'argvemulator', 'bgenlocations',
Benjamin Peterson23681932008-05-12 21:42:13 +0000315 'EasyDialogs', 'macerrors', 'macostools',
316 'findertools', 'FrameWork', 'ic',
317 'gensuitemodule', 'icopen', 'macresource',
318 'MiniAEFrame', 'pimp', 'PixMapWrapper',
Brett Cannonea785fb2008-05-14 01:09:40 +0000319 'terminalcommand', 'videoreader',
320 '_builtinSuites', 'CodeWarrior',
321 'Explorer', 'Finder', 'Netscape',
322 'StdSuites', 'SystemEvents', 'Terminal',
323 'cfmfile', 'bundlebuilder', 'buildtools',
Benjamin Petersona6864e02008-07-14 17:42:17 +0000324 'ColorPicker', 'Audio_mac'),
Brett Cannon22248172008-05-16 00:10:24 +0000325 'sunos5' : ('sunaudiodev', 'SUNAUDIODEV'),
326 }
Brett Cannonac861b52008-05-12 03:45:59 +0000327 optional_modules = ('bsddb185', 'Canvas', 'dl', 'linuxaudiodev', 'imageop',
Antoine Pitrou0b074572010-01-08 19:21:34 +0000328 'sv', 'bsddb', 'dbhash')
Brett Cannone5d2cba2008-05-06 23:23:34 +0000329
Brett Cannon9ac39742008-05-09 22:51:58 +0000330 def check_removal(self, module_name, optional=False):
Brett Cannone5d2cba2008-05-06 23:23:34 +0000331 """Make sure the specified module, when imported, raises a
332 DeprecationWarning and specifies itself in the message."""
Brett Cannon672237d2008-09-09 00:49:16 +0000333 with nested(CleanImport(module_name), warnings.catch_warnings()):
Nick Coghland2e09382008-09-11 12:11:06 +0000334 # XXX: This is not quite enough for extension modules - those
335 # won't rerun their init code even with CleanImport.
336 # You can see this easily by running the whole test suite with -3
Benjamin Petersona6864e02008-07-14 17:42:17 +0000337 warnings.filterwarnings("error", ".+ removed",
338 DeprecationWarning, __name__)
339 try:
340 __import__(module_name, level=0)
341 except DeprecationWarning as exc:
342 self.assert_(module_name in exc.args[0],
343 "%s warning didn't contain module name"
344 % module_name)
345 except ImportError:
346 if not optional:
347 self.fail("Non-optional module {0} raised an "
348 "ImportError.".format(module_name))
349 else:
350 self.fail("DeprecationWarning not raised for {0}"
351 .format(module_name))
Brett Cannone5d2cba2008-05-06 23:23:34 +0000352
353 def test_platform_independent_removals(self):
354 # Make sure that the modules that are available on all platforms raise
355 # the proper DeprecationWarning.
356 for module_name in self.all_platforms:
357 self.check_removal(module_name)
358
Brett Cannon9ac39742008-05-09 22:51:58 +0000359 def test_platform_specific_removals(self):
360 # Test the removal of platform-specific modules.
361 for module_name in self.inclusive_platforms.get(sys.platform, []):
362 self.check_removal(module_name, optional=True)
363
Brett Cannon768d44f2008-05-10 02:47:54 +0000364 def test_optional_module_removals(self):
365 # Test the removal of modules that may or may not be built.
366 for module_name in self.optional_modules:
367 self.check_removal(module_name, optional=True)
368
Benjamin Peterson0893a0a2008-05-09 00:27:01 +0000369 def test_os_path_walk(self):
370 msg = "In 3.x, os.path.walk is removed in favor of os.walk."
371 def dumbo(where, names, args): pass
372 for path_mod in ("ntpath", "macpath", "os2emxpath", "posixpath"):
373 mod = __import__(path_mod)
Nick Coghland2e09382008-09-11 12:11:06 +0000374 reset_module_registry(mod)
375 with check_warnings() as w:
Benjamin Peterson1d310232008-05-27 01:42:29 +0000376 mod.walk("crashers", dumbo, None)
Nick Coghland2e09382008-09-11 12:11:06 +0000377 self.assertEquals(str(w.message), msg)
Benjamin Peterson0893a0a2008-05-09 00:27:01 +0000378
Benjamin Peterson3aa84a72008-05-26 19:41:53 +0000379 def test_commands_members(self):
380 import commands
Nick Coghland2e09382008-09-11 12:11:06 +0000381 # commands module tests may have already triggered this warning
382 reset_module_registry(commands)
Benjamin Peterson3aa84a72008-05-26 19:41:53 +0000383 members = {"mk2arg" : 2, "mkarg" : 1, "getstatus" : 1}
384 for name, arg_count in members.items():
Brett Cannon672237d2008-09-09 00:49:16 +0000385 with warnings.catch_warnings():
Benjamin Peterson3aa84a72008-05-26 19:41:53 +0000386 warnings.filterwarnings("error")
387 func = getattr(commands, name)
388 self.assertRaises(DeprecationWarning, func, *([None]*arg_count))
389
Benjamin Peterson541f7da2008-08-18 02:12:23 +0000390 def test_reduce_move(self):
391 from operator import add
Nick Coghland2e09382008-09-11 12:11:06 +0000392 # reduce tests may have already triggered this warning
393 reset_module_registry(unittest)
Brett Cannon672237d2008-09-09 00:49:16 +0000394 with warnings.catch_warnings():
Benjamin Peterson541f7da2008-08-18 02:12:23 +0000395 warnings.filterwarnings("error", "reduce")
396 self.assertRaises(DeprecationWarning, reduce, add, range(10))
397
Brett Cannonabb34fe2008-05-29 05:08:50 +0000398 def test_mutablestring_removal(self):
399 # UserString.MutableString has been removed in 3.0.
400 import UserString
Nick Coghland2e09382008-09-11 12:11:06 +0000401 # UserString tests may have already triggered this warning
402 reset_module_registry(UserString)
Brett Cannon672237d2008-09-09 00:49:16 +0000403 with warnings.catch_warnings():
Brett Cannonabb34fe2008-05-29 05:08:50 +0000404 warnings.filterwarnings("error", ".*MutableString",
405 DeprecationWarning)
406 self.assertRaises(DeprecationWarning, UserString.MutableString)
407
Brett Cannone5d2cba2008-05-06 23:23:34 +0000408
Steven Bethardae42f332008-03-18 17:26:10 +0000409def test_main():
Nick Coghland2e09382008-09-11 12:11:06 +0000410 with check_warnings():
Benjamin Peterson1d310232008-05-27 01:42:29 +0000411 warnings.simplefilter("always")
412 run_unittest(TestPy3KWarnings,
413 TestStdlibRemovals)
Steven Bethardae42f332008-03-18 17:26:10 +0000414
415if __name__ == '__main__':
416 test_main()